我想使用DataAdapter填充DataGridView控件。但我不知道该怎么做,因为我使用带参数的存储过程。 有人可以举个例子吗?如何通过存储过程和参数使用DataAdapter
回答
我知道了...嘿嘿
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;
}
+1为了得到它的工作。 – Garett 2010-08-20 14:33:42
更清洁的方法是将IDisposable资源包装在使用子句中,而不是最后尝试 – cweston 2011-01-11 14:23:09
^更清洁地使用'使用'块 – IteratioN7T 2017-12-11 05:07:20
也许你的代码是缺少从Microsoft比如这一行:!
MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
SqlConnection con = new SqlConnection(@"Some Connection String");
SqlDataAdapter da = new SqlDataAdapter("ParaEmp_Select",con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add("@Contactid", SqlDbType.Int).Value = 123;
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
+1以获得更好的解决方案 – 2013-07-06 19:02:49
在这里,我们走了,
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con; //database connection
cmd.CommandText = "WRITE_STORED_PROC_NAME"; // Stored procedure name
cmd.CommandType = CommandType.StoredProcedure; // set it to stored proc
//add parameter if necessary
cmd.Parameters.Add("@userId", SqlDbType.Int).Value = courseid;
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(ds, "Course");
return ds;
SqlConnection con = new SqlConnection(@"Some Connection String");//connection object
SqlDataAdapter da = new SqlDataAdapter("ParaEmp_Select",con);//SqlDataAdapter class object
da.SelectCommand.CommandType = CommandType.StoredProcedure; //command sype
da.SelectCommand.Parameters.Add("@Contactid", SqlDbType.Int).Value = 123; //pass perametter
DataTable dt = new DataTable(); //dataset class object
da.Fill(dt); //call the stored producer
public DataSet Myfunction(string Myparameter)
{
config.cmd.Connection = config.cnx;
config.cmd.CommandText = "ProcName";
config.cmd.CommandType = CommandType.StoredProcedure;
config.cmd.Parameters.Add("parameter", SqlDbType.VarChar, 10);
config.cmd.Parameters["parameter"].Value = Myparameter;
config.dRadio = new SqlDataAdapter(config.cmd);
config.dRadio.Fill(config.ds,"Table");
return config.ds;
}
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = <sql server name>;
builder.UserID = <user id>; //User id used to login into SQL
builder.Password = <password>; //password used to login into SQL
builder.InitialCatalog = <database name>; //Name of Database
DataTable orderTable = new DataTable();
//<sp name> stored procedute name which you want to exceute
using (var con = new SqlConnection(builder.ConnectionString))
using (SqlCommand cmd = new SqlCommand(<sp name>, con))
using (var da = new SqlDataAdapter(cmd))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
//Data adapter(da) fills the data retuned from stored procedure
//into orderTable
da.Fill(orderTable);
}
- 1. 通过存储过程传递参数
- 2. 通过存储过程订购参数
- 3. 如何使用datetime参数调用存储过程中的存储过程
- 4. 如何在使用CRUD的存储过程时映射存储过程参数?
- 5. 使用odbc将参数设置为通过addwithvalue存储过程
- 6. 如何通过C#中的存储过程传递参数
- 7. 如何通过在存储过程中多值参数在SSRS
- 8. 如何通过存储过程的参数为EXECUTE语句
- 9. 如何通过项目作为参数列表存储过程
- 10. 如何通过参数为存储过程指定列?
- 11. 使用参数调用存储过程
- 12. 如何使用参数和存储过程
- 13. 使存储过程通用
- 14. 使用存储过程输出参数
- 15. 使用参数的存储过程
- 16. 使用位参数的存储过程
- 17. 使用参数运行存储过程
- 18. 如何使存储过程使用参数传递“数据集”?
- 19. 通过使用存储过程
- 20. 如何在存储过程中调用存储过程(带有2个参数)?
- 21. 参数化存储过程
- 22. 存储过程NULL参数
- 23. 存储过程的参数
- 24. DbNull.Value存储过程参数?
- 25. 存储过程,空参数
- 26. 存储过程与参数
- 27. MySQL存储过程参数
- 28. 存储过程与参数
- 29. 在存储过程参数
- 30. 存储过程在参数
这是一个Microsoft [文章](http://support.microsoft.com/kb/306574),它提供了一个这样做的例子。 – Garett 2010-08-20 05:10:28