我正在尝试将一行数据插入到我的 MS Access 数据库中。当我通过 VS 调试它时,它没有发现问题,但没有插入行。这个查询结果是没有插入任何行。我的日期是字符串,并且格式正确。我在参数列表中传递了 18 个值,必须有一种更简单的方法来做到这一点。是否有另一种方法可以为 OleDb 使用参数?我已经在我的方法下面发布了。你能看看我的语法是否正确吗?它仍然行不通。
这是我传递信息的地方:
rowsAdded = ((DataAccessLayer)Application["dbAccess"]).insert("Employees", txtLname.Text, txtFname.Text, txtTitle.Text, txtCourt.Text,
txtAddress.Text, txtCity.Text, txtCountry.Text, txtHomePhone.Text, txtExtension.Text, int.Parse(txtReports.Text), txtPassword.Text,
txtPostalCode.Text, txtNotes.Text, txtRegion.Text, txtHireDate.Text, txtBday.Text, upPhoto.FileName.ToString());
这是我用这种方法查询数据库的地方
public int insert(string tablename, string lname, string fname, string title, string toc, string address, string city,
string country, string phone, string ext, int report, string pass, string postal, string notes, string region,
string hire, string birth, string photo)
{
string tblName = tablename;
string last = lname;
string first = fname;
string tlt = title;
string tOfc = toc;
string addy = address;
string town = city;
string reg = country;
string phum = phone;
string exten = ext;
int rep = report;
string pas = pass;
string pc = postal;
string note = notes;
string regions = region;
string hD = hire;
string bD = birth;
string pho = photo;
int rows = 0;
int ID = 0;
string insertString = "INSERT INTO @tablename ([EmployeeID],[LastName],[FirstName],[Title],[TitleOfCourtesy],[Address],[City]," +
"[Country],[HomePhone],[Extension],[ReportsTo],[Password],[PostalCode],[Notes],[Region], [HireDate],[BirthDate],[Photo]) VALUES (" +
ID + ", @lname, @fname, @title, @toc, @addy, @city, @country," +
"@phone, @ext, @report, @pass, @postal, @notes,@region, @hire, @birth, @photo)";
string queryLast = "Select @@Identity";
try
{
conn.Open();
oleCommand = new OleDbCommand(insertString, conn);
oleCommand.CommandText = queryLast;
ID = (int)oleCommand.ExecuteScalar();
oleCommand.Parameters.AddWithValue("@tablename", tblName);
oleCommand.Parameters.AddWithValue("@lname", last);
oleCommand.Parameters.AddWithValue("@fname", first);
oleCommand.Parameters.AddWithValue("@title", tlt);
oleCommand.Parameters.AddWithValue("@toc", tOfc);
oleCommand.Parameters.AddWithValue("@addy", addy);
oleCommand.Parameters.AddWithValue("@city", town);
oleCommand.Parameters.AddWithValue("@country", reg);
oleCommand.Parameters.AddWithValue("@phone", phum);
oleCommand.Parameters.AddWithValue("@ext", exten);
oleCommand.Parameters.AddWithValue("@report", rep);
oleCommand.Parameters.AddWithValue("@pass", pas);
oleCommand.Parameters.AddWithValue("@region", regions);
oleCommand.Parameters.AddWithValue("@postal", pc);
oleCommand.Parameters.AddWithValue("@notes", note);
oleCommand.Parameters.AddWithValue("@hire", hD);
oleCommand.Parameters.AddWithValue("@birth", bD);
oleCommand.Parameters.AddWithValue("@photo", pho);
oleCommand.ExecuteNonQuery();
rows = (int)oleCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
dbError = "Add Employee command Error: " + ex.Message;
}
finally
{
conn.Close();
}
return rows;
}