【问题标题】:DataTable Select parameterized like SQL Select where x = @paramDataTable Select 参数化,如 SQL Select where x = @param
【发布时间】:2012-03-09 01:49:41
【问题描述】:

获取与此 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 慢很多?

【问题讨论】:

    标签: sql select parameters datatable where


    【解决方案1】:

    假设myTable 有一个名为ID 的列,那么你这样做:

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

    【讨论】:

    • 感谢它有一个 ID 列的答案,但问题的最重要部分是这个 ID 数组的范围可以从几千到几万。这就是为什么我不能像这样简单地连接一个字符串。或者如果我在那个 Select 括号内附加一个 4800 ID 字符串,你认为它会正常工作吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    相关资源
    最近更新 更多