您好我的代码有问题,当我按下按钮时出现以下错误:语法附近的错误语法:'。 标签'文字'已经声明了。标签名称在查询批处理或存储过程中必须是唯一的。 标签'文字'已经声明了。在查询批处理或存储过程中,标签名称必须是唯一的。
private void DodajDelavcaa_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(global::Easy_Podjetnik.Properties.Settings.Default.Database1ConnectionString);
try
{
string Spoll = "";
if (RB_M.Checked)
{
Spoll = "Moški";
}
else if (RB_Z.Checked)
{
Spoll = "Ženski";
}
string sql = "INSERT INTO Delavci(Ime, Priimek, Spol, Leto_Rojstva, Placa) values(" + AddIme + "," + AddPriimek + "," + Spoll + "," + Add_Datum + "," + PlacaD + ")";
SqlCommand exeSql = new SqlCommand(sql, con);
con.Open();
exeSql.ExecuteNonQuery();
MessageBox.Show("Congrtz", "Good" ,MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
con.Close();
}
this.Close();
}
答案 0 :(得分:1)
很可能这是由Delavci
表上的插入触发器中的错误引起的。这不是唯一的可能性,但它最有可能。
如果不是这样,那么抓住它的唯一方法就是在SQL Server上使用trace / profiler。
嗯,鉴于您正在合成SQL语句(而不是按照建议使用参数),这也可能是由Values(..)
变量中的错误数据引起的。要检查这一点,请在执行之前使用Debug.Print
记录SQL命令的实际文本。