将sqlsever中的数据分别以XML和JSON数据返回(webservice)

public XmlDocument GetAllByXML ()
{
DataSet data = new DataSet();
string sql = @"select * from T_AgvInfo order by ip";
string connectionString = @"user id = sa; password =123456;initial catalog=数据库名;Server=服务器名";
using (SqlConnection sqlConn = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(sql, sqlConn))
{
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command);
sqlDataAdapter.Fill(data);
}
}
XmlDocument doc;
doc = new XmlDataDocument(data);
return doc;
}

[WebMethod(Description = "This is AGV information by Json")]
public string GetAllByJSON()
{
DataSet data=new DataSet();
string sql = @"select * from T_AgvInfo order by ip";
string connectionString= @"user id = sa; password =123456;initial catalog=数据库名;Server=服务器名";
using(SqlConnection sqlConn=new SqlConnection(connectionString))
{
using(SqlCommand command=new SqlCommand(sql,sqlConn))
{
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command);
sqlDataAdapter.Fill(data);
}
}
return Newtonsoft.Json.JsonConvert.SerializeObject(data);
}

posted @ 2020-08-24 14:09  苏州の酱醋茶  阅读(192)  评论(0编辑  收藏  举报