0

当我添加少于 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
4

3 回答 3

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();                 
} 
于 2012-05-21T08:53:22.597 回答
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();

    }
于 2012-06-02T11:22:50.513 回答
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)?否则,当数据库中有更多序列号时,您的软件将显着变慢。

于 2012-05-21T08:52:54.110 回答