如何使用DataAdapter与存储过程和参数 [英] How to use a DataAdapter with stored procedure and parameter
本文介绍了如何使用DataAdapter与存储过程和参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要填写使用DataAdapter的DataGridView控件。但我不知道如何,因为我使用的是带有参数的存储过程来做到这一点。
有人能举个例子吗?
I want to fill a DataGridView control using DataAdapter. But I don't know how to do it since I'm using a stored procedure with parameter. Can someone cite an example please?
推荐答案
我知道了!...嘿嘿
protected DataTable RetrieveEmployeeSubInfo(string employeeNo)
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
try
{
cmd = new SqlCommand("RETRIEVE_EMPLOYEE", pl.ConnOpen());
cmd.Parameters.Add(new SqlParameter("@EMPLOYEENO", employeeNo));
cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = cmd;
da.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
cmd.Dispose();
pl.MySQLConn.Close();
}
return dt;
}
这篇关于如何使用DataAdapter与存储过程和参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文