C# 用于MS Access更新表的OleDbDataAdapter,为什么我的更新不起作用?
我不确定是什么原因导致我在这里做得不正确-在调试器中,对文件名所做的更改正确地对我从中提取更新命令的数据集进行了更改,但是当我检查完数据库后,没有进行任何更改。。。所以我有点困惑C# 用于MS Access更新表的OleDbDataAdapter,为什么我的更新不起作用?,c#,C#,我不确定是什么原因导致我在这里做得不正确-在调试器中,对文件名所做的更改正确地对我从中提取更新命令的数据集进行了更改,但是当我检查完数据库后,没有进行任何更改。。。所以我有点困惑 using (System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data
using (System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=J:\\Physics.mdb"))
{
using (OleDbDataAdapter dbAdapter = new OleDbDataAdapter("select thesisID, filename FROM Theses", con))
{
DataSet ds = new DataSet();
con.Open();
dbAdapter.Fill(ds);
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
ds.Tables[0].Rows[j]["filename"] = ds.Tables[0].Rows[j]["filename"].ToString().Replace(',', '_');
string newFileName = ds.Tables[0].Rows[j]["filename"].ToString();
int ID = Convert.ToInt32(ds.Tables[0].Rows[j]["thesisID"].ToString());
using (OleDbCommand updateCommand = con.CreateCommand())
{
updateCommand.CommandText = "update theses set filename = @newFileName where thesisID = @ID";
updateCommand.Parameters.AddWithValue("@ID", ID);
updateCommand.Parameters.AddWithValue("@newFileName", newFileName);
updateCommand.ExecuteNonQuery();
}
}
con.Close();
}
}
尝试颠倒添加参数的顺序:
using (OleDbCommand updateCommand = con.CreateCommand())
{
updateCommand.CommandType = CommandType.Text;
updateCommand.CommandText = "update theses set filename = @newFileName where thesisID = @ID";
updateCommand.Parameters.AddWithValue("@newFileName", newFileName);
updateCommand.Parameters.AddWithValue("@ID", ID);
updateCommand.ExecuteNonQuery();
}
原因是,所以添加它们的顺序很重要
请注意,OleDb查询通常以这种方式表示:
using (OleDbCommand updateCommand = con.CreateCommand())
{
updateCommand.CommandType = CommandType.Text;
updateCommand.CommandText = "update theses set filename = ? where thesisID = ?";
updateCommand.Parameters.Add(new OleDbParameter("", "", ""...));
updateCommand.Parameters.Add(new OleDbParameter("", "", ""...));
updateCommand.ExecuteNonQuery();
}
这强调了顺序的重要性-问号只是占位符,它们会按照向命令中添加参数的顺序被替换。尝试颠倒添加参数的顺序:
using (OleDbCommand updateCommand = con.CreateCommand())
{
updateCommand.CommandType = CommandType.Text;
updateCommand.CommandText = "update theses set filename = @newFileName where thesisID = @ID";
updateCommand.Parameters.AddWithValue("@newFileName", newFileName);
updateCommand.Parameters.AddWithValue("@ID", ID);
updateCommand.ExecuteNonQuery();
}
原因是,所以添加它们的顺序很重要
请注意,OleDb查询通常以这种方式表示:
using (OleDbCommand updateCommand = con.CreateCommand())
{
updateCommand.CommandType = CommandType.Text;
updateCommand.CommandText = "update theses set filename = ? where thesisID = ?";
updateCommand.Parameters.Add(new OleDbParameter("", "", ""...));
updateCommand.Parameters.Add(new OleDbParameter("", "", ""...));
updateCommand.ExecuteNonQuery();
}
这强调了顺序的重要性-问号仅是占位符,按参数添加到命令的顺序替换。颠倒参数添加的顺序!恐怕这是OLEDB通信的本质——明白了。其他数据提供程序,如SqlClient,支持命名参数,您的代码就可以了。谢谢@MichaelTodd的建议。我不知道封闭式问题仍然可以访问,这与添加参数的顺序相反!恐怕这是OLEDB通信的本质——明白了。其他数据提供程序,如SqlClient,支持命名参数,您的代码就可以了。谢谢@MichaelTodd的建议。我不知道封闭式问题仍然可以访问