使用 SQL 添加大量行

本文关键字:添加 SQL 使用 | 更新日期: 2023-09-27 18:34:42

当我

添加少于 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();
        }
    }`

编辑: 私有无效更新数据库(字符字母( { 字母 = 字符。到上(字母(; int serialPro = 0; 字符串连接字符串 = "提供程序=Microsoft.ACE.OLEDB.12.0;" + "数据源=C:''Projects_2012''Project_Noam''访问''我的项目.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

使用 SQL 添加大量行

我不确定,但您的 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,它是所需列的序号,而读取器。深度 获取一个值,该值指示当前行的嵌套深度。
我不明白这两个值是如何相关的。

编辑:如果您正在寻找序列号的最大值,您可以通过这种方式更改代码

string sql = "SELECT MAX(proInfoSerialNum) AS maxSN FROM tblProInfo ";                 
using (OleDbCommand command = new OleDbCommand(sql, connection))                 
{                     
    serialPro = (int)command.ExecuteScalar();                 
} 
完成后,您可以从

关闭数据库连接开始,这可能会获得更好的结果。使用 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)?否则,当数据库中有更多序列号时,您的软件将显著变慢。

    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();
    }