将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);
}