【发布时间】:2012-11-22 20:27:16
【问题描述】:
我正在尝试将一行数据插入到我的 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;
}
【问题讨论】:
-
你确定你可以说@tablename吗?我不认为你可以。
-
如果我做了 "INSERT INTO " + tblName + "(... 怎么办?那行不通
-
表名不能参数化
-
是的。 + 表名 +
-
如果 EmployeeID 列是自动递增的 - 不要将其包含在插入中。再说一遍 - 你需要弄清楚你的步骤:1. 使用 insertString 创建命令,2. 指定参数,3. 执行插入,4. 将 queryLast 分配给 CommandText,5. 执行以获取最后一个 id。
标签: c# database ms-access parameters oledb