PDA

View Full Version : خطا هنگام ذخیره سازی در دیتابیس



aminhexa
چهارشنبه 10 آبان 1391, 17:35 عصر
سلام
خطای string or binary data would be truncated . The statement has been terminated. برای چیست؟

programerinfonet
چهارشنبه 10 آبان 1391, 17:59 عصر
دوست عزیز اگر اشتباه نکنم میگه که کدت کوتاه و یا ناقصه
اگه می تونی کدتو بذار تا بیشتر راهنماییت کنیم

aminhexa
چهارشنبه 10 آبان 1391, 18:56 عصر
SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Customer (ccode,cname,clname,cfname,cshcode,cmcode,ctel,cph one,cco,caddress,cpostal) values('" + Int32.Parse(txtId.Text) + "','" + txtCname.Text + "','" + txtClname.Text + "','" + txtCfname.Text + "','" + txtCshenasname.Text + "','" + txtCmcode.Text + "','" + txtCtel.Text + "','" + txtCphone.Text + "','" + txtCco.Text + "','" + txtCaddress.Text + "','" + txtCpostal.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();

r_s1389@yahoo.com
چهارشنبه 10 آبان 1391, 19:16 عصر
سلام
توصیه میکنم از اون روش برای ذخیره در دیتابیس استفاده نکنید چون امان اشتباه (تایپی) زیاد میشه

SqlConnection con = new SqlConnection("data source=(local);initial catalog=db;integrated security=true;");
SqlCommand cmd = new SqlCommand("insert into T2 (name,fname) values (@name,@fname)", con);
cmd.CommandType = CommandType.Text;

cmd.Parameters.AddWithValue("@name", textBox1.Text);
cmd.Parameters.AddWithValue("@fname", textBox2.Text);

con.Open();
cmd.ExecuteNonQuery();
con.Close();
از این روش که به صورت پارامتری است استفاده کنید