2014-07-10 142 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();