使用SQL添加大量行

时间:2012-05-21 08:48:23

标签: c# sql insert oledb

当我添加少于30行时,我的代码运行良好。但它不能处理更多。我怎么能克服这个?

我收到此错误:Unspecified error当我尝试添加少于30时,会添加所有行。当我试图添加超过30时,它不会添加任何内容,我得到错误的行数次。

以下是代码:

                    for (int i = 0; i < st1.Length; i++)
                {
                    UpdateDataBase(st1[i]);
                }

private void UpdateDataBase(char letter)
    {
        letter = char.ToUpper(letter);
        int serialPro = 0;
        string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                                  "Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
        OleDbConnection connection = new OleDbConnection(connectionString);

        string sql = "SELECT tblProInfo.proInfoSerialNum FROM tblProInfo ";
        OleDbCommand command = new OleDbCommand(sql, connection);
        try
        {
        connection.Open();
        OleDbDataReader reader = command.ExecuteReader();
        //get the last!
        while (reader.Read())
            serialPro = reader.GetInt32(reader.Depth);


        sql = "INSERT INTO tblOrderAA (orderAASerialPro, orderAACodon1) "
           + " values (?, ?)";
        OleDbCommand command2 = new OleDbCommand(sql, connection);

        command2.CommandType = CommandType.Text;
        command2.Parameters.AddWithValue("orderAASerialPro", serialPro);
        command2.Parameters.AddWithValue("orderAACodon1", letter);
        command2.ExecuteNonQuery();
            }
        catch (Exception e)
        {
            MessageBox.Show("אירעה שגיאה ב: \n" + e.Message);
            this.Close();
        }
    }`

编辑:         private void UpdateDataBase(char letter)         {             letter = char.ToUpper(letter);             int serialPro = 0;            string connectionString =“Provider = Microsoft.ACE.OLEDB.12.0;” +                                   “数据源= C:\ Projects_2012 \ Project_Noam \ Access \ myProject.accdb”;

        try
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                connection.Open();

                string sql = "SELECT tblProInfo.proInfoSerialNum FROM tblProInfo ";
                using (OleDbCommand command = new OleDbCommand(sql, connection))
                using (OleDbDataReader reader = command.ExecuteReader())
                {
                    //get the last!
                    while (reader.Read())
                        serialPro = reader.GetInt32(0);
                }

                sql = "INSERT INTO tblOrderAA (orderAASerialPro, orderAACodon1) "
       + " values (?, ?)";
                using (OleDbCommand command2 = new OleDbCommand(sql, connection))
                {
                    command2.CommandType = CommandType.Text;
                    command2.Parameters.AddWithValue("orderAASerialPro", serialPro);
                    command2.Parameters.AddWithValue("orderAACodon1", letter);
                    command2.ExecuteNonQuery();
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show("אירעה שגיאה ב: \n" + e.Message);
        }
    }
enter code here

3 个答案:

答案 0 :(得分:1)

我不确定,但您的OleDbConnection永远不会被关闭 尝试保证关闭和处理连接的using语句

using(OleDbConnection connection = new OleDbConnection(connectionString))
{
    string sql = "SELECT tblProInfo.proInfoSerialNum FROM tblProInfo ";         
    OleDbCommand command = new OleDbCommand(sql, connection);         
    try 
    {
     ......
    }
    catch(....)
}

还不清楚你在这一行中的意图

serialPro = reader.GetInt32(reader.Depth); 

GetInt32 获取一个int,它是所需列的序号,而 reader.Depth 获取一个值,该值指示当前行的嵌套深度。<登记/> 我不明白这两个值是如何相关的。

编辑:如果您要查找序列号的最大值,可以用这种方式更改代码

string sql = "SELECT MAX(proInfoSerialNum) AS maxSN FROM tblProInfo ";                 
using (OleDbCommand command = new OleDbCommand(sql, connection))                 
{                     
    serialPro = (int)command.ExecuteScalar();                 
} 

答案 1 :(得分:0)

您可以在完成后关闭数据库连接,然后可能会获得更好的结果。使用using语句执行此操作:

using (OleDbConnection connection = new OleDbConnection(connectionString))
{
    ...
}

using语句确保 - 对于实现IDisposable接口的对象 - 调用Dispose方法。

此外,您正在创建的读者始终保持开放状态。所以最后,如果重写,该方法应如下所示:

private void UpdateDataBase(char letter)
{
    letter = char.ToUpper(letter);
    int serialPro = 0;
    string connectionString = "...";

    try
    {
        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            connection.Open();

            string sql = "SELECT tblProInfo.proInfoSerialNum FROM tblProInfo ";
            using (OleDbCommand command = new OleDbCommand(sql, connection))
            using (OleDbDataReader reader = command.ExecuteReader())
            {
                //get the last!
                while (reader.Read())
                    serialPro = reader.GetInt32(reader.Depth);
            }

            sql = "INSERT INTO ...";
            using (OleDbCommand command2 = new OleDbCommand(sql, connection))
            {
                command2.CommandType = CommandType.Text;
                command2.Parameters.AddWithValue("orderAASerialPro", serialPro);
                command2.Parameters.AddWithValue("orderAACodon1", letter);
                command2.ExecuteNonQuery();
            }
        }
    }
    catch (Exception e)
    {
        MessageBox.Show("אירעה שגיאה ב: \n" + e.Message);
    }
}

加快速度:对于每次通话,您都要选择所有序列号,然后遍历阅读器以获取最新信息。是否有可用于将此工作放入数据库的索引?比如:按某个日期排序或选择MAX(proInfoSerialNum)?否则,当数据库中有更多序列号时,您的软件将显着减慢

答案 2 :(得分:0)

    void dataread()
    {
        query = "select * from nodes";
        cmd = new SqlCommand(query, con);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {

            comboBox1.Items.Add(dr[0].ToString());
        }
        dr.Close();
        cmd.Dispose();

    }