2016-09-24 68 views
0

我一直有问题让我的插入工作。我一直在考虑它是基于两个组合框(1.cboCustomer 2.cboProduct)和一个文本框(RegistrationDate)的选定索引来完成的。我得到的最多的错误将与CustomerID为空有关;我认为这应该是自动生成的。当然,最让我困惑的是形式规范。 cboCustomer组合框必须显示客户的名称;这不是注册表的一部分。并且cboProduct组合框必须显示产品名称;这也与注册表无关。注册表是我需要创建插入的位置,但是组合框使用两种方法加载,即从客户和产品表中获取名称,该名称不能用于插入。其中,如果我尝试只插入注册日期,我会收到一条错误,指出ProductCode不能为空。如果AddRegistration方法尽管返回一个布尔值,仍然会运行insert,我也有点困惑。感谢您的帮助和时间。试图插入到数据库文件时出现C#问题

以下大部分代码已被更改几次,因为我尝试了几种解决方案,但都没有成功。

这是我必须提取值以传递给AddRegistration方法的代码。

public void PutRegistrationData(Registration registration) 
    { 
     registration.RegistrationDate = registrationDateTextBox.Text; 
     registration.ProductCode = productComboBox.SelectedValue.ToString(); 
    } 

这是触发的 AddRegistration插入表单上的按钮的代码。

private void btnRegister_Click(object sender, EventArgs e) 
    { 
     if (this.IsPresent(registrationDateTextBox.Text)) 
     { 
      registration = new Registration(); 
      this.PutRegistrationData(registration); 
      try 
      { 
       RegistrationDB.AddRegistration(registration); 
       MessageBox.Show("The product was entered successfully", "Registration Success"); 

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message, ex.GetType().ToString()); 
      } 
     } 
    } 

这是我在insertDB类的AddRegistration中插入的代码。

public static bool AddRegistration(Registration registration) 
    { 
     try 
     { 
      SqlConnection connection = TechSupportDB.GetConnection(); 
      string insertStatement = "INSERT INTO Registrations (ProductCode, RegistrationDate" + 
            "VALUES (@ProductCode,@RegistrationDate)"; 
      SqlCommand insertCommand = new SqlCommand(insertStatement, connection); 
      //insertCommand.Parameters.AddWithValue("@CustomerID", registration.CustomerID); 
      insertCommand.Parameters.AddWithValue("@ProductCode", registration.ProductCode); 
      insertCommand.Parameters.AddWithValue("@RegistrationDate", registration.RegistrationDate); 
      insertCommand.Connection = connection; 

      connection.Open(); 
      insertCommand.ExecuteNonQuery(); 
      connection.Close(); 
      return true; 
     } 
     catch 
     { 
      return false; 
     } 
    } 
} 

}

+0

插入命令缺少字段列表后面的关闭括号。这是一个错字吗? – Steve

+0

也删除AddRegistration内部的空捕获,因为这样你就失去了真正的异常,告诉你什么是你的查询错误 – Steve

+0

感谢您的答复。这可能是一个错字,我已经花了几个小时在这段代码上,并在这个过程中改变了插入几次。 – JaceLandrum

回答

0

根据您的上述评论,你也需要读取来自客户组合的CustomerID值,将其添加到您的Registration实例,并更改查询也插入值

public void PutRegistrationData(Registration registration) 
{ 
    registration.RegistrationDate = registrationDateTextBox.Text; 
    registration.ProductCode = productComboBox.SelectedValue.ToString(); 
    registration.CustomerID = customerComboBox.SelectedValue.ToString(); 
} 


public static bool AddRegistration(Registration registration) 
{ 
    string insertStatement = @"INSERT INTO Registrations 
      (ProductCode, RegistrationDate, CustomerID) 
      VALUES (@ProductCode,@RegistrationDate, @CustomerID)"; 
    using(SqlConnection connection = TechSupportDB.GetConnection()) 
    using(SqlCommand insertCommand = new SqlCommand(insertStatement, connection)) 
    {  
     insertCommand.Parameters.AddWithValue("@CustomerID", registration.CustomerID); 
     insertCommand.Parameters.AddWithValue("@ProductCode", registration.ProductCode); 
     insertCommand.Parameters.AddWithValue("@RegistrationDate", registration.RegistrationDate); 

     connection.Open(); 
     int added = insertCommand.ExecuteNonQuery(); 
     return added != 0; 
    } 
} 

请注意,我已将您的SqlConnection和SqlCommand放入使用块中,以确保正确关闭和处理这两个对象实例。当一个类实现IDisposable接口时,总是尽可能快地处理对象,using statement就是这样。此外,如果记录已添加,则返回布尔值true。只有当插入成功检查ExecuteNonQUery的返回值是否为零时,我才将AddRegistration的return语句更改为true。

在所有其他情况下,让例外达到您的应用程序的上层,您可以为您的用户处理它。

我也希望强调一个事实,即AddWithValue is a dangerous method,应该非常谨慎地使用(或者最好不要使用)。它对你的参数做了很多假设。例如,日期在这里作为字符串传递。您只能希望数据库引擎可以在列期望日期时将字符串转换回日期。

+0

我应用了您所建议的更改,但我收到另一个异常声明; INSERT语句与FOREIGN KEY约束冲突。表客户,列CustomerID。该语句已终止。 – JaceLandrum

+0

这个问题解释你的问题:http://stackoverflow.com/questions/2965837/insert-statement-conflicted-with-the-foreign-key-constraint你如何设法插入客户组合框中的客户不在它的桌子? – Steve

+0

该信息是有用的,谢谢你的链接。然而,我仍然在思考如何解决这个问题。我已经考虑过第二个INSERT,它会将customerID插入到Customers表中,但它似乎并不是一个可行的解决方案atm。我会继续尝试解决这个问题。我还注意到,这篇文章提到了“插入不良数据”,我可以说这是可能的。我在应用程序执行过程中查看了运行值,并且声明的值为0 customerID, – JaceLandrum