请不要留恋
2017-06-08 01:09
采纳率: 100%
浏览 4.3k

c#中使用cmd.Parameters.AddWithValue存入数据库

 DBHelper D = new DBHelper();
                MySqlConnection M = D.getconn();
                M.Open();
                MySqlCommand cmd = new MySqlCommand("insert into test1(name,department,specification,amount,packing,declarationInspectionDate,assayDate,reportDate,commodityBatchNumber,inspectionbasis,conclusion,assessor,analyst,laboratoryTechnician) values(@name,@department,@specification,@amount,@packing,@declarationInspectionDate,@assayDate,@reportDate,@commodityBatchNumber,@inspectionbasis,@conclusion,@assessor,@analyst,@laboratoryTechnician)",M);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@name", this.textBox1.Text.Trim());
                cmd.Parameters.AddWithValue("@department", this.textBox2.Text.Trim());
                cmd.Parameters.AddWithValue("@specification", this.textBox3.Text.Trim());
                cmd.Parameters.AddWithValue("@amount", float.Parse(this.textBox4.Text.Trim().ToString()));
                cmd.Parameters.AddWithValue("@packing", this.textBox5.Text.Trim());
                cmd.Parameters.AddWithValue("@declarationInspectionDate", this.dateTimePicker1.Text.Trim());
                cmd.Parameters.AddWithValue("@assayDate", this.dateTimePicker2.Text.Trim());
                cmd.Parameters.AddWithValue("@reportDate", this.dateTimePicker3.Text.Trim());
                cmd.Parameters.AddWithValue("@commodityBatchNumber", this.textBox8.Text.Trim());
                cmd.Parameters.AddWithValue("@inspectionbasis", this.textBox10.Text.Trim());
                cmd.Parameters.AddWithValue("@conclusion", this.textBox11.Text.Trim());
                cmd.Parameters.AddWithValue("@assessor", this.textBox14.Text.Trim());
                cmd.Parameters.AddWithValue("@analyst", this.textBox13.Text.Trim());
                cmd.Parameters.AddWithValue("@laboratoryTechnician", this.textBox12.Text.Trim());
                int j = cmd.ExecuteNonQuery();

用以上代码,我为什么不能实现对数据库的操作?显示插入成功,但是数据库中相应字段全都是空的,也就是说,cmd.Parameters.AddWithValue("@laboratoryTechnician", this.textBox12.Text.Trim());中没有给相应的参数赋值,一个也没有,很难受,我不知道怎么改才行。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • sinat_38913556 2017-06-08 01:34
    已采纳
            try
            {
               M.Open();
    
            }
            catch (SqlException ex)
            {
                                Response.Write(ex.Message);
            }
            catch (IOException ex)
            { 
                                Response.Write(ex.Message);
                        }
            finally
            {
               M.Close();
            }
    
    
    
    
                        捕获一下异常
    
    点赞 打赏 评论
  • sinat_38913556 2017-06-08 01:37

    J返回的是1么?
    M.close()写了么?

    点赞 打赏 评论
  • 襄阳dou腐面 2017-06-08 01:39

    M.close()写了么?检查一下!

    点赞 打赏 评论

相关推荐 更多相似问题