基于文本文件列表更快地从SQL Server中选择元素

Gal*_*you 2 c# sql-server

我正在研究应该执行简单搜索功能的.NET Windows应用程序.应用程序在SQLServer数据库中按序列号搜索某些卡片,这些序列号在文本文件中导入,我只需在文件上打开StreamReader并开始读取行 - 因为每行只包含一个序列.检索数据后,我将它们全部显示在DataGridView上.

文件中的那些连续出版物不按一定的顺序排列(即我做不到Select * from table where serial between( min and max)); 他们完全没有关系.所以不用多说了,这就是我所做的:

DataTable table = new DataTable()

 StreamReader stream= new StreamReader(fileName);                

            while (!stream.EndOfStream) {
                string serial = stream.ReadLine();
                SqlDataReader reader= GetCardBySerial(serial);
                table.Load(reader);
                reader.Close();
            }

  public SqlDataReader GetCardBySerial(string serialNo) {

        SqlConnection cnn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("Cards_GetCardBySerial", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@serialNo", SqlDbType.NVarChar).Value = serialNo;
        cnn.Open();
        return cmd.ExecuteReader(CommandBehavior.CloseConnection);   

    }
Run Code Online (Sandbox Code Playgroud)

虽然这有效,但对我来说这很慢.应该做些什么来使搜索更快?

Jas*_*yon 6

我认为最快的方法是将序列号批量插入SQL Server上的表中.然后通过将搜索表连接到序列号上的Cards表中,使用单个SQL语句搜索所有序列号.