DataTable选择参数化,如SQL选择x = @param

时间:2012-02-17 16:34:17

标签: sql select parameters datatable where

获取与此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 Query慢得多吗?

1 个答案:

答案 0 :(得分:0)

假设myTable有一个名为ID的列,那么您可以:

var foundRows = myTable.Select("ID IN (1, 12, 14, 10)");