为什么它不起作用? [英] why it doesn't work ?

查看:86
本文介绍了为什么它不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cmd.CommandType = CommandType.Text;
cmd.Connection = sqlcon;
cmd.CommandText = "INSERT INTO TBL_Customers (customer_name,FK_fk_id,commissary,FK_area_id,customer_address,tel1,tel2,id_number,datebirth,tel4)
 VALUES
 ('" + txtname.Text + "','" + comboCategoryCustomers.ValueMember + "','" +
 txtcommissary.Text + "','" 
+ comboAreaCustomer.ValueMember + "','" + txtcustomer_address.Text +
 "','" + txttel1.Text + "','"
 + txttel2.Text + "','" 
+ txtid_number.Text + "','" + 
this.dateTimePicker1datebirth.Text 
+ "','" + txttel4.Text + "')";


sqlcon.Open();
cmd.ExecuteNonQuery();

MessageBox.Show("Saved");
sqlcon.Close();
dt1.Clear();







推荐答案

使用参数化语句。每次。没有理由。他们避免了可能出现的问题。



Use a parameterized statement. Every time. No excuses. They avoid a fracking ton of possible problems.

                  cmd.CommandText = "INSERT INTO TBL_Customers customer_name,FK_fk_id,commissary,FK_area_id,customer_address,tel1,tel2,id_number,datebirth,tel4) VALUES (@customer_name,@FK_fk_id,@commissary,@FK_area_id,@customer_address,@tel1,@tel2,@id_number,@datebirth,@tel4)" ;

cmd.Parameters.AddWithValue ( "@customer_name" , txtname.Text );

// etc.

// Pass in dates and numbers as dates and numbers, not as strings
cmd.Parameters.AddWithValue ( "@datebirth" , this.dateTimePicker1datebirth ) ;


你好朋友



你也可以这样做



Hi friend

you can also do like

cmd.CommandType = CommandType.Text;
cmd.Connection = sqlcon;
cmd.CommandText = @"INSERT INTO TBL_Customers (customer_name,FK_fk_id,commissary,FK_area_id,customer_address,tel1,tel2,id_number,datebirth,tel4)
 VALUES
 ('" + txtname.Text + "','" + comboCategoryCustomers.ValueMember + "','" +
 txtcommissary.Text + "','"
+ comboAreaCustomer.ValueMember + "','" + txtcustomer_address.Text +
 "','" + txttel1.Text + "','"
 + txttel2.Text + "','"
+ txtid_number.Text + "','" +
this.dateTimePicker1datebirth.Text
+ "','" + txttel4.Text + "')";


sqlcon.Open();
cmd.ExecuteNonQuery();

MessageBox.Show("Saved");
sqlcon.Close();
dt1.Clear();









Put插入语句前的@并检查错误





Put "@" before the insert statement and check for the error


这篇关于为什么它不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆