前期准备工作请看(webservice基本操作-1和2)
1.在webservice中写入:
[WebMethod(EnableSession = true, Description = "sql指sql语句,返回DataTable的值")]
public DataSet GetDataSet(string sql)
{
SqlConnection conn = new SqlConnection(ConnString-数据库连接字符串);
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
conn.Open();
da.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
2.winform中添加datagridview控件
按钮事件代码:
private void button2_Click(object sender, EventArgs e)
{
localhost.master st = new localhost.master();
DataSet ds = st.GetDataSet("select * from xxx");//db是服务代理类实例
dataGridView1.DataSource = ds.Tables[0];
}
3.执行效果: