好的,我很难用这个,我花了几天时间与谷歌等人试图找出我搞砸的地方。
我对C#非常陌生,而我正在做的是尝试抓取Customer_ID (Primary Key)
从我的SQL Server中添加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 :(得分:2)
您正在为命令添加参数,但您根本没有在命令文本中指定它。
SqlCommand getMaxCustID = new SqlCommand("SELECT MAX(Customer_ID) FROM dbo.Customers",
Form1.sqlConnection);
//getMaxCustID.Parameters.AddWithValue("@custID", finalCustID); //Comment that out
由于您未使用参数@custID
,因此无需将其传递给命令。只需注释掉或删除该行。
稍后您在yoru insert语句中使用参数@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);
答案 1 :(得分: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作为IDENTITY列,则不应尝试为其传递值,但可以使用SELECT SCOPE_IDENTITY()
答案 2 :(得分: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();