【问题标题】:Inserting a row of data from ASP webpage to Access database with OleDB使用 OleDB 将 ASP 网页中的一行数据插入 Access 数据库
【发布时间】: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


【解决方案1】:

在您的代码中,您不会执行 oleCommand,而 CommandTextinsertString。所有三个执行都使用等于queryLast 的命令文本完成。

您将insertString 传递给OleDbCommand 构造函数这一事实并不意味着它会在您分配后以某种方式被记住

oleCommand.CommandText = queryLast;

在这个分配中oleCommand 忘记了insertString 并且只会执行它的CommandText 中的内容,直到您分配其他内容。

另一件事:如果我没记错的话,执行insertString 可能会抛出异常,因为sql 参数化不支持指定为参数的表名。

如果 EmployeeID 列是自动递增的 - 不要将其包含在插入中。再一次 - 你需要把你的步骤弄直:

  1. 使用 insertString 创建命令,

  2. 指定参数,

  3. 执行插入,

  4. 将 queryLast 分配给 CommandText,

  5. 执行获取最后一个id。

【讨论】:

  • 我刚刚尝试添加oleCommand.CommandText = insertString,但它仍然没有返回任何行。当我调用我的oleCommand = new OleDbCommand(insertString, conn); 时,我的insertString 被实例化,queryStringoleCommand 的附加组件
  • 我明白了,我可以将我的ExecuteNonQuery 放在 CommandText 和 LOL 之前:该字段太小,无法接受您尝试添加的数据量。尝试插入或粘贴更少的数据。可能是我包含 ID 的原因。
  • 如果我排除自动增量编号,那么ID会使用吗?
  • 我不明白你在说什么。对于自动递增的字段,在 INSERT sql 语句中既不包括它们的名称也不包括它们的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多