问题
I have already done research into this, and though the below questions are similar, I have tried them all, but none seems to solve my issue.
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
Here is the part of the code that is relevant:
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();
}
}
I have declared the following class variables accordingly:
private string strConnPath = "";
private string strConn = "";
private bool blnLoaded = false;
OleDbConnection connection;
OleDbDataReader reader;
OleDbCommand command;
private string errortxt = "";
int testInt = 1;
This is my current situation:
The issue is, that the code within LoadDetails() works just fine, and it is run upon the form loading. That code can be used again and again as of now without issues, but when I try to run the other queries, they "fail". Not as in failing outright and throwing exceptions, but rather, the Parameterized Queries I am using within that area (tagSearchButton_Click and nameSearchButton_Click), wouldn't replace the parameters.
This has me confused, since it is doing what it should in LoadDetails() method. If I manually changed the command text during debugging, by replacing the parameter manually with the value, then the program works as it should, and returns the values returned by using the statement.
What is causing the OleDbCommand.Parameter.AddWithValue function to not work as intended?
More Details:
For example, I am using this line:
command = new OleDbCommand("SELECT * from tagsTbl WHERE leName='@name'", connection);
Therefore it would give the command this string for now:
SELECT * from tagsTbl WHERE leName='@name'
What the Parameterized Query should be doing is to change the @name to what is in the namesTextBox, as below:
command.Parameters.AddWithValue("@name", namesTextBox.Text);
Let's say I gave the textbox an input value of "Jane_Smith". Therefore it should change the command to:
SELECT * from tagsTbl WHERE leName='Jane_Smith'
But it instead does nothing, so the command is still:
SELECT * from tagsTbl WHERE leName='@name'
Other possibly relevant information:
I have also just read this question, but it is not the issue I am facing. I am using Microsoft Access 2013. I am opting to use a MS Access database for my program in view of the fact it is easier to run "standalone", with the client only requiring to install a free Access Database Engine, if they do not have MS Office installed. This program works offline.
Problem Solved Thanks to Rumit Parakhiya! (I have also learnt that MySQL query format and the OleDb query format for MS Access is different too. I was using the MySQL query format originally.)
回答1:
You don't need to put '
(single quotes) around named parameter in your query. Specifying it, CLR considers it as a string. Just remove those quotes around @name
and it should be working as expected.
来源:https://stackoverflow.com/questions/25029197/c-sharp-microsoft-access-parameterized-queries-not-doing-its-job