System.Data.SqlClient.Exception(0x80131904)字符串或二进制文件将被终止

时间:2010-11-30 08:06:16

标签: c# sql-server

我有以下代码,当我执行它时,我收到以下错误: System.Data.SqlClient.Exception(0x80131904)字符串或二进制文件将被终止

form1.con.Open();
command = new SqlCommand("UPDATE sucursale SET nume_sucursala='" + textBox1.Text.Trim() + "',sucursala_coord='" + textBox3.Text.Trim() + "',mida='" + textBox4.Text.Trim() + "',flex='" + textBox5.Text.Trim() + "',nr_sucursala='" + textBox6.Text.Trim() + "',director='" + textBox7.Text.Trim() + "',tel_director='" + textBox8 + "',director_adj='" + textBox9.Text.Trim() + "',tel_director_adj='" + textBox10.Text.Trim() + "',adresa='" + textBox14.Text.Trim() + "',telefon='" + textBox15.Text.Trim() + "',fax='" + textBox16.Text.Trim() + "',u1_days='" + textBox26.Text.Trim() + "',u1_company='" + textBox17.Text.Trim() + "',u2_days='" + textBox27.Text.Trim() + "',u2_company='" + textBox18.Text.Trim() + "',u3_days='" + textBox28.Text.Trim() + "',u3_company='" + textBox19.Text.Trim() + "',u4_days='" + textBox29.Text.Trim() + "',u4_company='" + textBox20.Text.Trim() + "',u5_days='" + textBox30.Text.Trim() + "',u5_company='" + textBox21.Text.Trim() + "',u6_days='" + textBox31.Text.Trim() + "',u6_company='" + textBox22.Text.Trim() + "',retea_interna='" + textBox23.Text.Trim() + "',timer='" + textBox24.Text.Trim() + "',program_clienti='" + textBox25.Text.Trim() + "',contact_extra='" + textBox11.Text.Trim() + "',functie_contact_extra='" + textBox12.Text.Trim() + "',telefon_contact_extra='" + textBox13.Text.Trim() + "' WHERE nr_sucursala = '" + comboBox3.SelectedItem + "'", form1.con);
form1.da2.UpdateCommand = command;

form1.da2.Update(form1.ds2, "sucursale");
form1.con.Close();

1 个答案:

答案 0 :(得分:16)

首先; 不连接用户输入;应参数化:

using(var cmd = conn.CreateCommand()) {
    cmd.CommandText = @"
UPDATE [SomeTable]
SET [SomeColumn] = @someColumn
WHERE [Id] = @id";
    cmd.Parameters.AddWithValue("someColumn", usersNastyText);
    cmd.Parameters.AddWithValue("id", recordId);
    cmd.ExecuteNonQuery();
}

错误表示其中一个框中的文本长于声明的列。在发送到服务器之前,展开数据库中的列,或验证框中文本的长度。

Obligatory XKCD reference

Exploits of a mom