作者seagal (待救的小米)
看板C_Sharp
标题Re: [问题] 想传一个参数阵列到函式.
时间Fri Nov 26 11:07:53 2004
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
1F:推 klein:真的是相当的谢谢你..:) 163.13.11.122 11/26