MySQL查询在C#中的数据库中插入空值

时间:2014-02-27 08:55:35

标签: c# mysql insert null

我有一个存储客户信息的C#应用​​程序。我正在使用MySQL Workbench作为后端。现在,当我将数据插入数据库(通过我的表单上的各种控件)时,主键(client_file_no)会自动递增,但其余字段显示为“NULL”。我一遍又一遍地检查了代码,但我仍然找不到错误。

    try
            {
                ConnectionDBLabData cdld = new ConnectionDBLabData();
                string ConnStr = cdld.GetConnectionString();
                MySqlConnection conn = new MySqlConnection(ConnStr);
                conn.Open();
                string QueryString = "insert into client_tbl (client_date_of_creation, client_referral_code, client_first_name, client_middle_name, client_last_name, client_sex, client_nationality, client_status, client_civil_id , client_birthday, client_born_year , client_preferred_language, client_occupation , client_email, client_tel_mobile, client_tel_home1, client_tel_home2, client_tel_work1,client_tel_work2, client_address1, client_address2, client_notes, client_cc , client_credit, client_debit, client_debitmax, client_stat, client_pricetype, client_qr_path ) VALUES(@value1, @value2, @value3 ,@value4, @value5 ,@value6, @value7, @value8, @value9 ,@value10 ,@value11, @value12, @value13, @value14, @value15, @value16, @value17, @value18, @value19, @value20, @value21, @value22, @value23, @value24, @value25, @value26, @value27, @value28, @value29)";
                MySqlCommand cmd = new MySqlCommand(QueryString, conn);
                cmd.Parameters.Add("@value1", textBox21.Text);
                cmd.Parameters.Add("@value2", textBox23.Text);
                cmd.Parameters.Add("@value3", textBox1.Text);
                cmd.Parameters.Add("@value4", textBox2.Text);
                cmd.Parameters.Add("@value5", textBox3.Text);
                cmd.Parameters.Add("@value6", Convert.ToString(comboBox4.SelectedItem));
                cmd.Parameters.Add("@value7", Convert.ToString(comboBox2.SelectedItem));
                cmd.Parameters.Add("@value8", Convert.ToString(comboBox1.SelectedItem));
                cmd.Parameters.Add("@value9", textBox5.Text);
                cmd.Parameters.Add("@value10", textBox13.Text);
                cmd.Parameters.Add("@value11", textBox19.Text);
                cmd.Parameters.Add("@value12", textBox22.Text);
                cmd.Parameters.Add("@value13", textBox6.Text);
                cmd.Parameters.Add("@value14", textBox7.Text);
                cmd.Parameters.Add("@value15", textBox8.Text);
                cmd.Parameters.Add("@value16", textBox9.Text);
                cmd.Parameters.Add("@value17", textBox10.Text);
                cmd.Parameters.Add("@value18", textBox11.Text);
                cmd.Parameters.Add("@value19", textBox12.Text);
                cmd.Parameters.Add("@value20", richTextBox1.Text);
                cmd.Parameters.Add("@value21", richTextBox2.Text);
                cmd.Parameters.Add("@value22", richTextBox3.Text);
                cmd.Parameters.Add("@value23", textBox4.Text);
                cmd.Parameters.Add("@value24", Convert.ToDouble(textBox14.Text));
                cmd.Parameters.Add("@value25", Convert.ToDouble(textBox15.Text));
                cmd.Parameters.Add("@value26", Convert.ToDouble(textBox16.Text));
                cmd.Parameters.Add("@value27", Convert.ToInt32(textBox17.Text));
                cmd.Parameters.Add("@value28", Convert.ToInt32(textBox18.Text));
                cmd.Parameters.Add("@value29", textBox5.Text + "_qr.png");
                cmd.ExecuteScalar();

数据库:

    create table client_tbl(client_file_no int not null auto_increment primary key,
                            client_date_of_creation varchar(20),
                            client_referral_code varchar(60), 
                            client_first_name varchar(20),
                            client_second_name varchar(20),
                            client_last_name varchar(20),
                            client_nationality varchar(60),
                            client_sex varchar(10),
                            client_status varchar(20),
                            client_civil_id varchar(30),
                            client_birthday varchar(20), 
                            client_born_year varchar(20),
                            client_occupation varchar(30),
                            client_preferred_language varchar(30),
                            client_email varchar(40), 
                            client_tel_mobile varchar(20),
                            client_tel_work1 varchar(20),
                            client_tel_work2 varchar(20),
                            client_tel_home2 varchar(20),
                            client_address1 varchar(512), 
                            client_address2 varchar(512), 
                            client_notes varchar(512),
                            client_cc  varchar(50), 
                            client_credit float,
                            client_debit float, 
                            client_debitmax float, 
                            client_stat int,
                            client_pricetype int, 
                            client_rq_code varchar(80));

2 个答案:

答案 0 :(得分:1)

尝试AddWithValue

command.Parameters.AddWithValue("@value1", textBox21.Text);

答案 1 :(得分:0)

通过更改连接器解决了问题(原来我的连接器有问题) 谢谢大家的帮助 问候, Kshitij Thube