如何在Visual Studio中执行此操作?我在数据集设计中设置了Autoincrement = true,并在数据库图表中的标识规范中设置了“YES”。还有什么?
但是现在如何添加数据?当我尝试这样做但我有一个例外。 代码。
cmd = new SqlCommand("Insert into Subject" +
" Values (namesubject,numberoflect,numberofpract)", conn);
SqlParameter param = new SqlParameter();
//param.ParameterName = "@idsubject";
//param.Value = Convert.ToInt32(textBox1.Text);
//param.SqlDbType = SqlDbType.Int;
//cmd.Parameters.Add(param);
//param = new SqlParameter();
param.ParameterName = "namesubject";
param.Value = textBox2.Text;
param.SqlDbType = SqlDbType.Text;
cmd.Parameters.Add(param);
param = new SqlParameter();
param.ParameterName = "numberoflect";
if (textBox3.Text == "")
textBox3.Text = "0";
param.Value = Convert.ToInt32(textBox3.Text);
param.SqlDbType = SqlDbType.Int;
cmd.Parameters.Add(param);
param = new SqlParameter();
param.ParameterName = "numberofpract";
if (textBox4.Text == "")
textBox4.Text = "0";
param.Value = Convert.ToInt32(textBox3.Text);
param.SqlDbType = SqlDbType.Int;
cmd.Parameters.Add(param);
cmd.ExecuteNonQuery();
自动增量字段 - 此表中的ids主题。
答案 0 :(得分:2)
首先将idsubject的自动标识设置为true
OR
Create table ....
(
idsubject int Auto identity (1,1) not null
....
....
)
并修改您的插入代码,如
cmd = new SqlCommand("Insert into Subject(namesubject, numberoflect, numberofpract)" +
" Values ('"+textBox2.Text+"',"+textBox3.Text+","+textBox3.Text+")", conn);
cmd.ExecuteNonQuery();