日期:2014-05-18  浏览次数:20904 次

确保方法的参数的格式正确。将一个字符串转换为datatime时,先分析该字符串以获取的日期,然后再将每个变量放置到datetime对象中。获取此异常的常规帮助。?
private void btntianjia_Click(object sender, EventArgs e)
  {



  try
  {
  DataAccess ds=new DataAccess ();
  string str = ds.connection();
  con = new SqlConnection(str);
  con.Open();
  string Sql = "insert into ygjbtable(yggh,xingming,nianling,jiguan,shenfz,bumeng,zhiwei,jinctime,jbgz,gongb,jishi,lictime,bz) values('" + this.textBox1.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "','" + this.textBox4.Text + "','" + this.textBox5.Text + "','" + this.textBox6.Text + "','" +Convert .ToDateTime( this.textBox7.Text) + "','" + this.textBox8.Text + "','" + this.textBox9.Text + "','" +decimal .Parse ( this.textBox10.Text) + "','" +decimal .Parse ( this.textBox11.Text) + "','" +Convert .ToDateTime ( this.textBox12.Text )+ "','" + this.textBox13.Text + "')";

  SqlCommand cmd = con.CreateCommand();
  cmd.CommandType = CommandType.Text;
  cmd.CommandText = Sql;
  cmd.ExecuteNonQuery();
  }
  catch (Exception ex)
  {

  MessageBox.Show(ex.Message);
  }


  }
错误提示参数的格式正确。将一个字符串转换为datatime时,先分析该字符串以获取的日期,然后再将每个变量放置到datetime对象中。获取此异常的常规帮助。?

(其中jinctime,lictime 是时间字段相对应的是Convert .ToDateTime( this.textBox7.Text),Convert .ToDateTime ( this.textBox12.Text ) )



------解决方案--------------------
string Sql = "insert into ygjbtable(yggh,xingming,nianling,jiguan,shenfz,bumeng,zhiwei,jinctime,jbgz,gongb,jishi,lictime,bz) values('" + this.textBox1.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "','" + this.textBox4.Text + "','" + this.textBox5.Text + "','" + this.textBox6.Text + "','" +Convert.ToDateTime( this.textBox7.Text) + "','" + this.textBox8.Text + "','" + this.textBox9.Text + "','" +decimal .Parse ( this.textBox10.Text) + "','" +decimal .Parse ( this.textBox11.Text) + "','" +Convert .ToDateTime ( this.textBox12.Text ) + "','" + this.textBox13.Text + "')"; 
Sql是string类型,Convert.ToDateTime( this.textBox7.Text)是DateTime类型,不同的数据类型怎么可以赋值?
如果textBox7.Text已经是日期格式的话根本不容转换为DateTime