我已经对此进行了研究,虽然下面的问题很相似,但我已经尝试了所有这些,但似乎都没有解决我的问题。
Proper way of getting a data from an Access Database
using parameters inserting data into access database
Getting Data from Access into a text box in C# by clicking a button
UPDATE query on Access Database not working C#.NET
passing parameter to access query from c#
Parameterized query for inserting values
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()中的代码运行正常,并且在表单加载时运行。到目前为止,该代码可以一次又一次地使用而没有问题,但是当我尝试运行其他查询时,它们会失败并且#34;。不是完全失败并抛出异常,而是我在该区域内使用的参数化查询( 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'
我刚刚阅读this question,但这不是我面临的问题。我正在使用Microsoft Access 2013.我选择为我的程序使用MS Access数据库,因为它更容易运行" standalone",客户端只需要安装免费的Access数据库引擎,如果他们没有安装MS Office。该程序可以脱机工作。
问题解决了Rumit Parakhiya! (我还了解到MySQL查询格式和MS Access的OleDb查询格式也不同。我最初使用的是MySQL查询格式。)
答案 0 :(得分:5)
您不需要在查询中将'
(单引号)放在命名参数周围。指定它,CLR将其视为字符串。只需移除@name
周围的引号,它就应该按预期工作。