2014-07-10 145 views
-3

好的,我在这个方面很难,我花了几天的时间与谷歌等试图找出我搞砸了。必须声明标量变量?

我对C#非常陌生,我正在做的是试图抓住Customer_ID (Primary Key)从我的SQL Server中,将值1加1,然后输入它作为下一个客户ID。如果我拿出将值添加到Customer_ID字段的部分,我收到一条错误消息,说我无法插入空值。任何帮助将不胜感激。

的错误是:

必须声明标量变量 “@custID”。

这里是我的代码:

string firstName = fName.Text; 
string lastName = lName.Text; 
string address1 = address.Text; 
string city = addressCity.Text; 
string state = addressState.Text; 
string zip1 = addressZip.Text; 
string phoneNum = phone.Text; 
string emailadd = custEmail.Text; 
int finalCustID = 0; 

SqlCommand getMaxCustID = new SqlCommand("SELECT MAX(Customer_ID) FROM dbo.Customers", Form1.sqlConnection); 
getMaxCustID.Parameters.AddWithValue("@custID", finalCustID); 
Int32 count = Convert.ToInt32(getMaxCustID.ExecuteScalar()); 
finalCustID = (int)count + 1; 
//Int32 newCustID = count + 1; 

SqlCommand saveRecord = new SqlCommand("INSERT INTO dbo.Customers (Customer_ID,First_Name, Last_Name, Customer_Address,City,St,Zip,Phone,Email)" + "Values (@custID'firstName','lastName','address1','city','state','zip1','phoneNum','emailadd')", Form1.sqlConnection); 

Console.Write(" This is the final ID " + finalCustID + " \n"); 
try 
{ 
    saveRecord.ExecuteNonQuery(); 
} 
catch (Exception ex) 
{ 
    Console.WriteLine(ex.Message); 
    Console.Write(saveRecord); 
} 

这里是我结束了。我不认为这是最好的,但它工作正常。感谢所有帮助:

 string firstName = fName.Text; 
     string lastName = lName.Text; 
     string address1 = address.Text; 
     string city = addressCity.Text; 
     string state = addressState.Text; 
     string zip1 = addressZip.Text; 
     string phoneNum = phone.Text; 
     string emailadd = custEmail.Text; 
     int finalCustID = 0; 

     SqlCommand getMaxCustID = new SqlCommand("SELECT MAX(Customer_ID) FROM dbo.Customers", Form1.sqlConnection); 
     //getMaxCustID.Parameters.AddWithValue("@custID", finalCustID); 
     Int32 count = Convert.ToInt32(getMaxCustID.ExecuteScalar()); 
     finalCustID = (int)count + 1; 
     //Int32 newCustID = count + 1; 

     SqlCommand saveRecord = new SqlCommand("INSERT INTO dbo.Customers " + "(Customer_ID,First_Name, Last_Name, Customer_Address,City,St,Zip,Phone,Email)" + " Values (@custID,@fName,@lName,@address,@city,@state," + "@zip,@phoneNumber,@custEmail)", Form1.sqlConnection); ; 
     Console.Write(" This is the final ID " + finalCustID + " \n"); 
     try 
     { 
      saveRecord.Parameters.AddWithValue("@custID", finalCustID); 
      saveRecord.Parameters.AddWithValue("@fName", firstName); 
      saveRecord.Parameters.AddWithValue("@lName", lastName); 
      saveRecord.Parameters.AddWithValue("@address", address1); 
      saveRecord.Parameters.AddWithValue("@city", city); 
      saveRecord.Parameters.AddWithValue("@state", city); 
      saveRecord.Parameters.AddWithValue("@zip", zip1); 
      saveRecord.Parameters.AddWithValue("@phoneNumber", phoneNum); 
      saveRecord.Parameters.AddWithValue("@custEmail", emailadd); 

      saveRecord.ExecuteNonQuery(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
      Console.Write(saveRecord); 
     } 
+0

@Steve我假设你的意思是说,这是不是一个好方法。我会同意这100%。 josh - 这种类型的方法的问题是,当有多个人同时执行此代码并且他们都获得相同的值时,您会因某些并发问题而导致主键违规。要么使用一个身份,要么你是sql 2012+,你可以使用一个序列。 –

+0

只有当你没有多用户场景时它才能正常工作 – Steve

回答

2

您要添加参数的命令,但你有没有在您的命令文本中指定它。

SqlCommand getMaxCustID = new SqlCommand("SELECT MAX(Customer_ID) FROM dbo.Customers", 
            Form1.sqlConnection); 
//getMaxCustID.Parameters.AddWithValue("@custID", finalCustID); //Comment that out 

既然你不使用你的参数@custID,你并不需要传递给你的命令。只是评论或删除该行。

后来您在yoru插入语句中使用参数@custID您需要将参数添加到该命令对象。

saveRecord.Parameters.AddWithValue("@custID", finalCustID); 

(感谢@ N55PEC),你缺少你的参数和下一个值之间的INSERT命令文本之间的逗号。它应该是这样的:

注意:其他格式用于保持视口内的答案。

SqlCommand saveRecord = new SqlCommand("INSERT INTO dbo.Customers " + 
     "(Customer_ID,First_Name, Last_Name, Customer_Address,City,St,Zip,Phone,Email)" + 
     " Values (@custID,'firstName','lastName','address1','city','state'," + 
     "'zip1','phoneNum','emailadd')", Form1.sqlConnection); 
+0

他的第二个查询(插入的) – User999999

+0

@ N55PEC也有错误,刚刚看到它,我正在修改我的答案。 – Habib

+2

而在saverecord中,“@custid”后面还有一个逗号:“@ custID'firstName'”应该是“@ custID,'firstName'”:) – User999999

0

需要的@custID参数的命令被称为saveRecord,你把它添加到命令调用getMaxCustID

SqlCommand getMaxCustID = new SqlCommand("SELECT MAX(Customer_ID) FROM dbo.Customers", Form1.sqlConnection); 
Int32 count = Convert.ToInt32(getMaxCustID.ExecuteScalar()); 
finalCustID = (int)count + 1; 

SqlCommand saveRecord = new SqlCommand("INSERT INTO dbo.Customers (Customer_ID,First_Name, Last_Name, Customer_Address,City,St,Zip,Phone,Email)" + "Values (@custID', firstName','lastName','address1','city','state','zip1','phoneNum','emailadd')", Form1.sqlConnection); 
saveRecord.Parameters.AddWithValue("@custID", finalCustID); 

然而,正如我在上面的评论说,使用MAX检索下一个分配给下一位客户的号码在多用户环境中显然是错误的。如果另一位用户在执行阅读客户ID和插入新记录之间添加客户时会怎么办?您将以主键违例异常结束。

与此方案的工作,最好的办法是让Customer_ID柱为IDENTITY列,然后像这样写代码

SqlCommand saveRecord = new SqlCommand(@"INSERT INTO dbo.Customers 
     (First_Name, Last_Name, Customer_Address,City,St,Zip,Phone,Email) 
     Values (@custID,'firstName','lastName','address1','city','state','zip1', 
     'phoneNum','emailadd'); 
     SELECT SCOPE_IDENTITY()", Form1.sqlConnection); 
int newCustomerID = Convert.ToInt32(saveRecord.ExecuteScalar()); 

注意,如果你有CUSTOMER_ID作为一个标识列,你不应该试图通过它的值,但是你可以检索使用SELECT SCOPE_IDENTITY()

0

使用以下行的数据库引擎分配的价值,因为它是:

SqlCommand getMaxCustID = new SqlCommand("SELECT MAX(Customer_ID) FROM dbo.Customers", Form1.sqlConnection); 
Int32 count = Convert.ToInt32(getMaxCustID.ExecuteScalar()); 

务必:

int finalCustID = (int)count + 1; 

然后:

SqlCommand saveRecord = new SqlCommand("INSERT INTO dbo.Customers (Customer_ID,First_Name, Last_Name, Customer_Address,City,St,Zip,Phone,Email)" + "Values ('finalCustID.toString() ','firstName','lastName','address1','city','state','zip1','phoneNum','emailadd')", Form1.sqlConnection); 

最后:

saveRecord.ExecuteNonQuery();