C#的Microsoft Access参数化查询没有做的工作 [英] C# Microsoft Access Parameterized Queries not doing its job

查看:356
本文介绍了C#的Microsoft Access参数化查询没有做的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了研究这个,虽然下面的问题是相似的,我把它们都试过,但没有一个似乎解决我的问题。



<一个HREF =http://stackoverflow.com/questions/15148588/proper-way-of-getting-a-data-from-an-access-database>从Access数据库得到一个数据,有道



使用参数插入数据转换成access数据库



从访问到C#中的文本框中获取数据通过点击按钮


$ b $在访问数据库b

UPDATE查询工作不C#.NET



传递参数从C#



访问查询的插入值



下面是相关的代码部分参数化查询:



 私人无效LoadDetails(INT指数)
{

{
connection.Open();
命令=新的OleDbCommand(SELECT * FROM tagsTbl WHERE ID = @ 1,连接);
command.Parameters.AddWithValue(@ 1,指数);
=读者Command.ExecuteReader却();
,而(reader.Read())
{
nameTextBox.Text =读卡器[leName]的ToString()。
altTextBox.Text =读卡器[altName是]的ToString()。
unitTextBox.Text =读卡器[currUnit]的ToString()。
tagTextBox.Text =读卡器[currTag]的ToString()。
oldTextBox.Text =读卡器[oldTag]的ToString()。
descTextBox.Text =读卡器[currDesc]的ToString()。
}
的Connection.close();
}

{
的Connection.close();
MessageBox.Show(errortxt);
Application.Exit();
}
}

私人无效testWin_Load(对象发件人,EventArgs五)
{
loadFileDialog.ShowDialog();
连接=新的OleDbConnection(创建失败);
如果(blnLoaded!)
Application.Exit();
,否则
{
errortxt =尝试建立连接失败的数据库!;
LoadDetails(testInt);
this.Show();
}
}

私人无效loadFileDialog_FileOk(对象发件人,发送CancelEventArgs E)
{
strConnPath = loadFileDialog.FileName;
创建失败= @供应商= Microsoft.ACE.OLEDB.12.0;数据源=+ strConnPath;
blnLoaded = TRUE;
}

私人无效prevButton_Click(对象发件人,EventArgs五)
{
如果(testInt→1)
testInt--;
LoadDetails(testInt);
gotoNumericUpDown.Value = testInt;
}

私人无效nextButton_Click(对象发件人,EventArgs五)
{
testInt ++;
errortxt =你不能高于!;

{
LoadDetails(testInt);
gotoNumericUpDown.Value = testInt;
}

{
testInt--;
}
}

私人无效gotoButton_Click(对象发件人,EventArgs五)
{

{
testInt =( INT)gotoNumericUpDown.Value;
LoadDetails(testInt);
}

{

}
}

私人无效nameSearchButton_Click(对象发件人,EventArgs五)
{

{
connection.Open();
命令=新的OleDbCommand(SELECT * FROM tagsTbl WHERE leName ='@名字',连接);
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue(@名,namesTextBox.Text);
=读者Command.ExecuteReader却();
,而(reader.Read())
{
nameTextBox.Text =读卡器[leName]的ToString()。
altTextBox.Text =读卡器[altName是]的ToString()。
unitTextBox.Text =读卡器[currUnit]的ToString()。
tagTextBox.Text =读卡器[currTag]的ToString()。
oldTextBox.Text =读卡器[oldTag]的ToString()。
descTextBox.Text =读卡器[currDesc]的ToString()。
}
的Connection.close();
}

{
的Connection.close();
}
}

私人无效tagSearchButton_Click(对象发件人,EventArgs五)
{

{
命令=新的OleDbCommand(SELECT * FROM tagsTbl WHERE currTag =@ 1',连接);
command.Parameters.AddWithValue(@ 1,tagsTextBox.Text);
connection.Open();
MessageBox.Show(command.CommandText);
=读者Command.ExecuteReader却();
,而(reader.Read())
{
nameTextBox.Text =读卡器[leName]的ToString()。
altTextBox.Text =读卡器[altName是]的ToString()。
unitTextBox.Text =读卡器[currUnit]的ToString()。
tagTextBox.Text =读卡器[currTag]的ToString()。
oldTextBox.Text =读卡器[oldTag]的ToString()。
descTextBox.Text =读卡器[currDesc]的ToString()。
}
的Connection.close();
}

{
的Connection.close();
}
}



我相应声明以下类变量:



 私人字符串strConnPath =; 
私人字符串创建失败=;
私人布尔blnLoaded = FALSE;
OleDbConnection的连接;
OleDbDataReader阅读器;
OleDbCommand的命令;
私人字符串errortxt =;
INT testInt = 1;



这是我目前的状况:




问题是, LoadDetails中的代码()的工作得很好,它是建立在形式加载运行。该代码可以被反复使用,截至目前没有问题,但是当我尝试运行其他查询,他们不及格。还不如在彻底失败,抛出异常,而是在参数化查询我使用的区域内 tagSearchButton_Click 的和的 nameSearchButton_Click 的)的 ,不会取代参数。




这有我困惑,因为它是做什么它应该在的 LoadDetails()的方法。如果我在调试过程中手动修改命令文本,通过数值手动替换参数,那么程序工作,因为它应该,并返回使用的语句返回的值。



< BLOCKQUOTE>

是什么原因造成的的 OleDbCommand.Parameter.AddWithValue 的功能并不如预期?



工作



更多详情:



例如,我用这行:

 命令=新的OleDbCommand(SELECT * FROM tagsTbl WHERE leName ='@名字',连接); 



因此它会给命令该字符串现在:




SELECT * FROM tagsTbl WHERE leName ='@名字




什么是参数化查询应该做的是改变的 @name 的什么是在 namesTextBox 的,如下:

  command.Parameters.AddWithValue(@名,namesTextBox.Text); 

比方说,我给了文本框的输入值 Jane_Smith 的。因此,应改变命令:




SELECT * FROM tagsTbl WHERE leName ='Jane_Smith




不过它,而不是什么都不做,因此命令仍然是:




SELECT * FROM tagsTbl WHERE leName ='@名字




其他可能相关的信息:



我也刚看完的这个问题,但它不是我所面临的问题。我使用的Microsoft Access 2013年我选择加入鉴于它更易于运行独立使用MS Access数据库为我的方案,客户端只需要安装一个免费的Access数据库引擎,如果他们不这样做安装了微软Office。这个程序可以离线工作。



问题解决了由于 Rumit Parakhiya ! (我还了解到,MySQL查询格式的MS Access使用OLEDB查询格式也不同,我用的是MySQL查询格式原本。)


解决方案

您不需要把 (单引号)左右命名的参数在查询中。指定它,CLR认为它作为一个字符串。只是删除周围 @name 这些报价并应工作正常。


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.)

解决方案

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.

这篇关于C#的Microsoft Access参数化查询没有做的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆