【发布时间】:2017-01-19 20:20:00
【问题描述】:
Sql 中有一个名为 Student 的表,其中定义了 4 列。第一列是 ID,它按身份自动递增。当我尝试在 sql 中插入数据时,出现以下错误: SqlCommandBuilder 对象上的“关键字 'where' 附近的语法不正确”
protected void Button1_Click(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
string sqlQuery = "insert into Student values where = " + TextStudentID.Text;
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, con);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
DataSet ds = (DataSet)ViewState["dataset"];
SqlCommand cmd = builder.GetInsertCommand();
cmd.Parameters["@StudentName"].Value = TextStudentName.Text;
cmd.Parameters["@Gender"].Value = DropDownList1.SelectedValue;
cmd.Parameters["@Studentmarks"].Value = TextStudentName.Text;
cmd.ExecuteNonQuery();
da.Fill(ds);
}
提前致谢
【问题讨论】:
-
请阅读documentation on INSERT statement,您会发现
WHERE没有像您尝试的那样使用。