简体   繁体   English

分组查询时无法使用ExecuteReader()检索数据

[英]Can't retrieve data using ExecuteReader() when grouping queries

I need to retrieve the information stored in a database of some thousand items. 我需要检索存储在数千个项目的数据库中的信息。 If I go one by one by this way it takes a large amount of time (tac is a 8-character string): 如果我通过这种方式逐个进行,则需要花费大量时间(tac是8个字符的字符串):

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DataBase\IMEIDB.accdb";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
    connection.Open();
    using (OleDbCommand command = connection.CreateCommand())
    {
        OleDbDataReader reader;
        command.CommandText = "SELECT TAC, Name, Model, Year, Manufacturer, LTE FROM Terminales WHERE TAC = @tac";
        command.Parameters.AddWithValue("@tac", tac);
        reader = command.ExecuteReader();
        while (reader.Read())
        {
            ulong tac = Convert.ToUInt64(reader.GetString(0));
            if (this.DiccionarioTerminales.ContainsKey(tac))
            {
                DiccionarioTerminales[tac].inDB = true;
                DiccionarioTerminales[tac].Name = reader.GetValue(1).ToString();
                DiccionarioTerminales[tac].Manufacturer = reader.GetValue(2).ToString();
                DiccionarioTerminales[tac].Model = reader.GetValue(3).ToString();
                DiccionarioTerminales[tac].Year = reader.GetValue(4).ToString();
                DiccionarioTerminales[tac].LTE = reader.GetValue(5).ToString();
            }
        }
        command.Dispose();
    }
    connection.Dispose();
}

It works well (I know I must use ExecuteNonQuery() if it's only one record, but this example is only a test), but if I try to group the tac 10 by 10 (now tac is a string like 'xxxxxxxx','xxxxxxxx','xxxxxxxx','xxxxxxxx'... ) with the next changes in my code... 它运行良好(我知道我必须使用ExecuteNonQuery()如果它只有一个记录,但这个例子只是一个测试),但是如果我尝试将tac 10分组10(现在tac是一个像'xxxxxxxx','xxxxxxxx','xxxxxxxx','xxxxxxxx'...那样的字符串'xxxxxxxx','xxxxxxxx','xxxxxxxx','xxxxxxxx'... )我的代码中的下一个更改...

OleDbDataReader reader;
command.CommandText = "SELECT TAC, Name, Model, Year, Manufacturer, LTE FROM Terminales WHERE TAC IN (@tac)";
command.Parameters.AddWithValue("@tac", tac);

It doesn't enter in the while loop and I don't know why... 它没有进入while循环,我不知道为什么......

Is there something that am I missing or maybe I need to use another method to retrieve those data? 有什么东西我缺少或者我可能需要使用另一种方法来检索这些数据?

EDIT : Change the format due to the Soner Gönül answer 编辑 :根据SonerGönül答案更改格式

Because when you use this string in IN clause, it will seems like; 因为当你在IN子句中使用这个字符串时,它似乎是;

IN (xxxxxxxx,xxxxxxxx,xxxxxxxx,xxxxxxxx)

but the right syntax should be 正确的语法应该是

IN ('xxxxxxxx','xxxxxxxx','xxxxxxxx','xxxxxxxx')

That's why it doesn't work. 这就是为什么它不起作用。 One solution might be, you can split your string with , , format them with using single quotes and join them , again. 一种解决方案可能是,你可以分割你的字符串,与使用单引号格式化并加入他们的行列,再次。

var str = "xxxxxxxx,xxxxxxxx,xxxxxxxx,xxxxxxxx";
var result = string.Join(",", str.Split(',').Select(s => string.Format("'{0}'", s)));

result will be 'xxxxxxxx','xxxxxxxx','xxxxxxxx','xxxxxxxx' and you can use it in your IN clause like; result将是'xxxxxxxx','xxxxxxxx','xxxxxxxx','xxxxxxxx' ,你可以在你的IN子句中使用它;

...TAC IN (@tac)

and

command.Parameters.AddWithValue("@tac", result);

Also don't use AddWithValue as much as you can. 也不要尽可能多地使用AddWithValue It may generate unexpected and surprising results sometimes . 有时它可能会产生意想不到的惊人结果 Use Add method overload to specify your parameter type and it's size. 使用Add方法重载来指定参数类型及其大小。

Related: Parameterize an SQL IN clause 相关: 参数化SQL IN子句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM