[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))
這就是我在我的網絡服務。然而,它給我的錯誤
Bobby Tables [再次觸擊](http://technet.microsoft.com/en-us/library/ms161953 \(v = sql.105 \).aspx)。 – Romoku
[IDENTITY \ _INSERT設置爲OFF時無法在表'表中爲標識列插入顯式值]的可能重複(http://stackoverflow.com/questions/1334012/cannot-insert-explicit-value-for-identity -column-in-table-table-when-identity) – 48klocs
'upload.InsertTemplate'是做什麼用的?你爲什麼要混合字符串連接和參數? –