【发布时间】:2014-01-31 14:37:53
【问题描述】:
[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;
}
我正在尝试将数据插入到具有四列的指纹表中 - ID(主键) - 链接到学生表的 StudentID(外键) - 描述 - 模板
但以下错误不断出现。我无法关闭 ID 的 IDENTITY,因为我希望它自动递增。我还有一个学生表来存储信息。我想要实现的是,在输入学生详细信息后,我想将之前生成的学生ID复制到指纹表 - 学生ID列。我的代码如下所示。
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))
这就是我的网络服务上的内容。但是它给了我错误
【问题讨论】:
-
鲍比桌 strikes again.
-
upload.InsertTemplate是做什么的?为什么要混合字符串连接和参数? -
让这个可怜的家伙休息一下:xkcd.com/327
-
ASMX 是一项遗留技术,不应用于新开发。 WCF 或 ASP.NET Web API 应该用于 Web 服务客户端和服务器的所有新开发。一个提示:Microsoft 已停用 MSDN 上的 ASMX Forum。
标签: c# sql sql-server sql-server-2008 asmx