Re: [問題] 想傳一個參數陣列到函式.
Sorry
我只有看你的中文敘述
沒注意到你的程式碼
已經使用sqlparameter
我貼上兩種ADO.NET常用的存取資料庫方式
第一種使用SQLCommand
也就是利用你傳入的Parameter去存取 修改資料庫
我的動作都在btnFind_Click裡面完成
應該跟你的Edit method差不多
稍微修改一下 就變成了可以傳入SQLParameter了吧
private void btnFind_Click(object sender, System.EventArgs e)
{
// The SqlDataReader class provides a way to read a forward-only
// stream of rows from a Sql Server database
SqlDataReader drEmployee = null;
try
{
// Open connection to the database
sqlConnection1.Open();
// Fill the parameter with the value retrieved from the text field
sqlCommand1.Parameters["@Find"].Value = txtFind.Text;
// Execute the query
drEmployee = sqlCommand1.ExecuteReader();
// Fill the list box with the values retrieved
lbFound.Items.Clear();
while(drEmployee.Read())
{
lbFound.Items.Add(drEmployee["FirstName"].ToString() + " " +
drEmployee["LastName"].ToString());
}
}
catch(Exception ex)
{
// Print error message
MessageBox.Show(ex.Message);
}
finally
{
// Close data reader object and database connection
if (drEmployee != null)
drEmployee.Close();
if (sqlConnection1.State != ConnectionState.Closed)
sqlConnection1.Close();
}
}
第二種方式
是利用Dataset & Data Adaptor
來修改資料庫
// Change the value in the DataSet
ds.Tables[0].Rows[8]["FirstName"] = "Anna";
// Re-connect to the data source
cn.Open();
// Create an OleDbCommandBuilder object, passing the DataAdapter
// into the constructor
OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(da);
// Display the UpdateCommand
Console.WriteLine(cmdBuilder.GetUpdateCommand().CommandText);
// Update the data source
da.Update(ds.Tables[0]);
// Close the connection again
cn.Close();
如果你可以使用ADO.NET 2.0
那就可以使用最方便的第三種方式
使用SQLDataSource
搭配ASP.NET & Visual Studio .NET 2005
可以不用寫任何一行程式碼
就完成修改 存取資料庫
若是要自己操作SQLDataSource
一樣也很簡單
※ 引述《klein (愛新覺羅)》之銘言:
: ※ 引述《seagal (待救的小米)》之銘言:
: : 為什麼不使用ADO.NET or ADO.NET 2.0完成呢?
: : 我們可以先來討論看看
: : 哪些事情是沒辦法用ADO.NET完成
: : 再來研究看看如何做吧
: 可以說的更清楚一點嗎? @@
: 用sqlparameter不是就是ADO.NET嗎?
--
生物資訊研究室:每天都會更新文件,提供生物資訊教學,生物字典。
http://www.bioinformatic.idv.tw
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.109.16.141
推
163.13.11.122 11/26, , 1F
163.13.11.122 11/26, 1F
討論串 (同標題文章)
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章