我想使用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