DataTable Select 参数化,类似于 SQL Select,其中 x = @param
获取与此 SQL 查询等效的 DataTable 行的查询语法是什么?
SQLiteConnection cnn = new SQLiteConnection(System.String.Format("Data Source={0}", fpath));
cnn.Open();
DataTable primaryfeed = new DataTable();
using (SQLiteTransaction dbTrans = cnn.BeginTransaction())
{
using (SQLiteCommand cmd = cnn.CreateCommand())
{
string command = System.String.Format("SELECT col1, col2, col3, col4 FROM AccountDataBase WHERE ID = @ID");
SQLiteParameter param1 = new SQLiteParameter();
param1.ParameterName = "@ID";
cmd.Parameters.Add(param1);
cmd.CommandText = command;
for (int i = 0; i < selectedIDs.Length; i++)
{
param1.Value = selectedIDs[i];
SQLiteDataReader reader = cmd.ExecuteReader();
primaryfeed.Load(reader);
reader.Close();
}
}
dbTrans.Commit();
}
cnn.Close();
所以我有一个 DataTable 和一个 ID 字符串数组。以最快的方式从 DataTable 中获取具有数组 ID 值的所有行的命令是什么?
它比同等的 SQL 查询慢得多吗?
What is the query syntax from getting the rows of a DataTable that is equivalent of this SQL Query?
SQLiteConnection cnn = new SQLiteConnection(System.String.Format("Data Source={0}", fpath));
cnn.Open();
DataTable primaryfeed = new DataTable();
using (SQLiteTransaction dbTrans = cnn.BeginTransaction())
{
using (SQLiteCommand cmd = cnn.CreateCommand())
{
string command = System.String.Format("SELECT col1, col2, col3, col4 FROM AccountDataBase WHERE ID = @ID");
SQLiteParameter param1 = new SQLiteParameter();
param1.ParameterName = "@ID";
cmd.Parameters.Add(param1);
cmd.CommandText = command;
for (int i = 0; i < selectedIDs.Length; i++)
{
param1.Value = selectedIDs[i];
SQLiteDataReader reader = cmd.ExecuteReader();
primaryfeed.Load(reader);
reader.Close();
}
}
dbTrans.Commit();
}
cnn.Close();
So I have a DataTable and a stringarray of IDs. What is the command to get all rows from the DataTable that have the ID values from the array the quickest way possible?
Is it much slower than the equivalent SQL Query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设
myTable
有一个名为ID
的列,那么您可以:Assuming
myTable
has a column namedID
, then you do: