我有文本和按钮,在提交时,我正在检查数据库是否有任何行 - 如果没有则插入行或者更新它们,但是在提交时抛出错误,说“cmd.ExecuteNonQuery”语法不正确在其他条件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class CM : System.Web.UI.Page
{
DataSet ds = new DataSet();
SqlDataAdapter da;
SqlCommand cmd;
DataTable dt;
SqlConnection con = new SqlConnection("server =consulting76\\SQLEXPRESS; database = msdb; Integrated Security=True; MultipleActiveResultSets=True");
protected void Page_Load(object sender, EventArgs e)
{
da = new SqlDataAdapter("Select * from NOTESMAKER", con);
da.Fill(ds);
//dt = ds.Tables["NOTESMAKER"];
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
if (ds.Tables[0].Rows.Count == 0)
{
cmd = new SqlCommand("Insert into NOTESMAKER(NOTESMAKER) Values(@text1)",con);
cmd.Parameters.Add(new SqlParameter("@text1", SqlDbType.NText)).Value = TextBox1.Text;
da.InsertCommand = cmd;
cmd.ExecuteNonQuery();
}
else
{
cmd = new SqlCommand("Update NOTESMAKER set NOTESMAKER = @text1)",con);
cmd.Parameters.Add(new SqlParameter("@text1", SqlDbType.NText)).Value = TextBox1.Text;
da.UpdateCommand = cmd;
cmd.ExecuteNonQuery();
}
con.Close();
}
}
答案 0 :(得分:5)
您正在关闭此行的括号,该行不会打开:
cmd = new SqlCommand("Update NOTESMAKER set NOTESMAKER = @text1)",con);
此外,不需要设置数据适配器的InsertCommand和UpdateCommand属性。