我有一张桌子student
(id, name
)。然后我有一个文本框,用于输入名称,当单击提交按钮时,它将数据插入数据库。那么我怎样才能只插入名称,而不是 id,因为 id 是自动递增的?
我试过这个
insert into student(id, name) values(,name)
但它没有插入我的桌子。
这是我的代码:
protected void Button1_Click(object sender, EventArgs e)
{
string test = txtName.Text;
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Person.mdf;Integrated Security=True;User Instance=True");
string sql = "insert into student(name) values ('test')";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
}
finally
{
conn.Close();
}
}