我有这个代码可以正常工作。但不知何故,在特定的形式它不起作用,我得到这个错误(在行connction.Open();
)Unspecified error
该代码假设将多行添加到表中。这是代码:
for (int i = 0; st1.Length > i; i++)
UpdateDataBase(st1[i]);
private void UpdateDataBase(char letter)
{
int serialPro = 0;
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb";
try
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
string sql = "SELECT tblProInfo.proInfoSerialNum FROM tblProInfo ";
using (OleDbCommand command = new OleDbCommand(sql, connection))
{
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
//get the last!
while (reader.Read())
serialPro = reader.GetInt32(0);
}
sql = "INSERT INTO tblOrderAA (orderAASerialPro, orderAACodon1) "
+ " values (?, ?)";
using (OleDbCommand command = new OleDbCommand(sql, connection))
{
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("orderAASerialPro", serialPro);
command.Parameters.AddWithValue("orderAACodon1", letter);
command.ExecuteNonQuery();
}
}
}
catch (Exception e)
{
MessageBox.Show("error in:" + e.Message);
}
}
我知道我可以使用 MAX 获得最后一个。但它以不同的形式正常工作。所以我坚持要明白为什么!谢谢大家的帮助。