2014-01-31 90 views
-1
[WebMethod] 
     public bool AddStudent(Student student) 
     {    bool UploadSuccess = false; 
      cn.Open(); 
      int StudentID = 0; 
      using (SqlCommand com = new SqlCommand("INSERT into tblStudent (StudentNumber, Name, Surname, DOB, Gender, EmailAddress, Address1, Address2, City, Postcode, Username, Password, Course) values ('" + student.StudentNumber + "' ,'" + student.Name + "' ,'" + student.Surname + "' ,'" + student.DOB + "', '" + student.Gender + "' ,'" + student.EmailAddress + "' ,'" + student.Address1 + "' ,'" + student.Address2 + "' ,'" + student.City + "' ,'" + student.Postcode + "' ,'" + student.Username + "' ,'" + student.Password + "' ,'" + student.Course + "')", cn)) 
      { 
       int i = com.ExecuteNonQuery(); 
       StudentID = (int)com.ExecuteScalar(); 
       cn.Close(); 
       if (i != 0) 

        UploadSuccess = true; 

       return UploadSuccess; 
      } 

我試圖將數據插入其中有四列指紋表中「無法插入的標識列‘TBL指紋’顯性價值鍵)鏈接到學生表 - 描述 - 模板當IDENTITY_INSERT設置爲OFF

但是,下面的錯誤不斷出現,我不能關閉ID的IDENTITY,因爲我想讓它自動增加,我還有一個學生表來存儲信息。我想要達到的目的是在輸入學生的詳細信息之後,我想將以前生成的studentID複製到指紋表 - StudentID列中。我爲此得到的代碼如下所示:

private void btnSave_Click(object sender, EventArgs e) 
     { 
      fgrTemplate template = new fgrTemplate(); 
      template.StudentID = std.StudentID; 
      template.Description = fngDes.Text; 
      template.Template = m_StoredTemplate; 
      if (upload.InsertTemplate(template)) 
      { 
       MessageBox.Show("Student Successfully Added!"); 
      } 
      else 
      { 
       MessageBox.Show("Student Not Successfully Added!"); 
      } 

using (SqlCommand com = new SqlCommand("INSERT INTO tblFingerprint (StudentID, Description, Template) values ('" + template.StudentID + "' ,'" + template.Description + "' ,@Template)", cn)) 

這就是我在我的網絡服務。然而,它給我的錯誤

+3

Bobby Tables [再次觸擊](http://technet.microsoft.com/en-us/library/ms161953 \(v = sql.105 \).aspx)。 – Romoku

+0

[IDENTITY \ _INSERT設置爲OFF時無法在表'表中爲標識列插入顯式值]的可能重複(http://stackoverflow.com/questions/1334012/cannot-insert-explicit-value-for-identity -column-in-table-table-when-identity) – 48klocs

+0

'upload.InsertTemplate'是做什麼用的?你爲什麼要混合字符串連接和參數? –

回答

0

刪除或註釋該行

//template.StudentID = std.StudentID; 

同時刪除StudentIDINSERT

"INSERT INTO tblFingerprint (Description,.... 
+0

如果我這樣做,我如何複製鏈接指紋模板的學生ID到學生。 – user3144368

+0

這似乎是身份領域,它會自動增加,當你將新的記錄插入表中。如果你想在這個字段中插入你自己的值,他們從數據庫結構中刪除IDENTITY,那麼不需要改變你的代碼就可以工作。 – Iqbal

+0

template.StudentID = std.StudentID將信息從前一個表單傳遞到當前表單。它加載信息並將其保存爲std,以便我可以將studentID保存到tblFingeprint中。我已經有了一個名爲ID的tblFingerprint主鍵。 StudentID是與tblStudent鏈接的外鍵。我只想複製該StudentID以將模板鏈接到他們的信息 – user3144368

1

你的第一個錯誤:

您正在嘗試插入StudentID這看起來像一個IDENTITY類型f (自動增量),你不必在你的INSERT語句中通過,SQL服務器會爲你生成一個。

您的第二個問題是:您的查詢未正確參數化,您正在使用字符串連接和參數的組合。您的查詢,聲明應該是這樣的:

using (SqlCommand com = new SqlCommand(@"INSERT INTO tblFingerprint (Description, Template) 
             values (@Description, @Template)", cn)) 
{ 
    com.Parameters.AddWithValue("@Description", template.Descriptio); 
    com.Parameters.AddWithValue("@Template", template.Value); //what ever value is 
    //....rest of your code 

} 

如果你已經有一個StudentID並要更新現有記錄,然後使用UPDATE聲明。

如果你想手動插入StudentID(覆蓋自動遞增ID)那麼你必須使用SET IDENTITY_INSERT

+0

我在我的指紋表中有四列 - ID(主鍵),學生ID(外鍵)鏈接到學生表,描述和模板。輸入學生信息後,我想將學生ID從tblstudent複製到tblfingeprint。 – user3144368

相關問題