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