C#Microsoft Access参数化查询无法正常工作
我已经对此进行了研究,虽然下面的问题很相似,但我已经尝试了所有这些,但似乎都没有解决我的问题。
从Access数据库获取数据的正确方法
使用参数将数据插入访问数据库
通过单击按钮将数据从Access获取到C#中的文本框
Access数据库的UPDATE查询不能正常工作C#.NET
传递参数以从c#访问查询
用于插入值的参数化查询
以下是相关代码的一部分:
private void LoadDetails(int index) { try { connection.Open(); command = new OleDbCommand("SELECT * from tagsTbl WHERE ID=@1", connection); command.Parameters.AddWithValue("@1", index); reader = command.ExecuteReader(); while (reader.Read()) { nameTextBox.Text = reader["leName"].ToString(); altTextBox.Text = reader["altName"].ToString(); unitTextBox.Text = reader["currUnit"].ToString(); tagTextBox.Text = reader["currTag"].ToString(); oldTextBox.Text = reader["oldTag"].ToString(); descTextBox.Text = reader["currDesc"].ToString(); } connection.Close(); } catch { connection.Close(); MessageBox.Show(errortxt); Application.Exit(); } } private void testWin_Load(object sender, EventArgs e) { loadFileDialog.ShowDialog(); connection = new OleDbConnection(strConn); if (!blnLoaded) Application.Exit(); else { errortxt = "Attempt to establish connection to database failed!"; LoadDetails(testInt); this.Show(); } } private void loadFileDialog_FileOk(object sender, CancelEventArgs e) { strConnPath = loadFileDialog.FileName; strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strConnPath; blnLoaded = true; } private void prevButton_Click(object sender, EventArgs e) { if (testInt > 1) testInt--; LoadDetails(testInt); gotoNumericUpDown.Value = testInt; } private void nextButton_Click(object sender, EventArgs e) { testInt++; errortxt = "You cannot go higher than that!"; try { LoadDetails(testInt); gotoNumericUpDown.Value = testInt; } catch { testInt--; } } private void gotoButton_Click(object sender, EventArgs e) { try { testInt = (int)gotoNumericUpDown.Value; LoadDetails(testInt); } catch { } } private void nameSearchButton_Click(object sender, EventArgs e) { try { connection.Open(); command = new OleDbCommand("SELECT * from tagsTbl WHERE leName='@name'", connection); command.CommandType = CommandType.Text; command.Parameters.AddWithValue("@name", namesTextBox.Text); reader = command.ExecuteReader(); while (reader.Read()) { nameTextBox.Text = reader["leName"].ToString(); altTextBox.Text = reader["altName"].ToString(); unitTextBox.Text = reader["currUnit"].ToString(); tagTextBox.Text = reader["currTag"].ToString(); oldTextBox.Text = reader["oldTag"].ToString(); descTextBox.Text = reader["currDesc"].ToString(); } connection.Close(); } catch { connection.Close(); } } private void tagSearchButton_Click(object sender, EventArgs e) { try { command = new OleDbCommand("SELECT * from tagsTbl WHERE currTag='@1'", connection); command.Parameters.AddWithValue("@1", tagsTextBox.Text); connection.Open(); MessageBox.Show(command.CommandText); reader = command.ExecuteReader(); while (reader.Read()) { nameTextBox.Text = reader["leName"].ToString(); altTextBox.Text = reader["altName"].ToString(); unitTextBox.Text = reader["currUnit"].ToString(); tagTextBox.Text = reader["currTag"].ToString(); oldTextBox.Text = reader["oldTag"].ToString(); descTextBox.Text = reader["currDesc"].ToString(); } connection.Close(); } catch { connection.Close(); } }
我已相应地声明了以下类变量:
private string strConnPath = ""; private string strConn = ""; private bool blnLoaded = false; OleDbConnection connection; OleDbDataReader reader; OleDbCommand command; private string errortxt = ""; int testInt = 1;
这是我目前的情况:
问题是, LoadDetails()中的代码工作得很好,并且它在表单加载时运行。 该代码可以一次又一次地使用而没有问题,但是当我尝试运行其他查询时,它们“失败”。 不像完全失败并抛出exception,而是我在该区 域内 使用的参数化查询 ( tagSearchButton_Click和nameSearchButton_Click ) 不会替换参数。
这让我很困惑,因为它在LoadDetails()方法中正在做它应该做的事情。 如果我在调试期间手动更改了命令文本,通过手动替换参数值,程序将按原样运行,并返回使用该语句返回的值。
是什么导致OleDbCommand.Parameter.AddWithValue函数无法正常工作?
更多细节:
例如,我正在使用这一行:
command = new OleDbCommand("SELECT * from tagsTbl WHERE leName='@name'", connection);
因此它现在会给这个字符串命令:
SELECT * from tagsTbl WHERE leName =’@ name’
参数化查询应该做的是将@name更改为namesTextBox中的内容 ,如下所示:
command.Parameters.AddWithValue("@name", namesTextBox.Text);
假设我给文本框输入了“ Jane_Smith ”的值。 因此它应该将命令更改为:
SELECT * from tagsTbl WHERE leName =’Jane_Smith’
但它没有做任何事情,所以命令仍然是:
SELECT * from tagsTbl WHERE leName =’@ name’
其他可能相关的信息:
我也刚刚读过这个问题 ,但这不是我面临的问题。 我正在使用Microsoft Access 2013.我选择为我的程序使用MS Access数据库,因为它更容易运行“独立”,客户端只需要安装免费的Access数据库引擎,如果他们不已安装MS Office。 该程序脱机工作。
解决问题感谢Rumit Parakhiya ! (我还了解到MySQL查询格式和MS Access的OleDb查询格式也不同。我最初使用的是MySQL查询格式。)
您不需要在查询中将'
(单引号)放在命名参数周围。 指定它,CLR将其视为字符串。 只需删除@name
周围的引号,它应该按预期工作。