比如我在前台上有一个控件dateTimePicker1,现在我想把它的值写入数据库,老是报类型不对,请问怎么解决

解决方案 »

  1.   

    string sql="insert into emp(hiredate) values ("+Convert.ToDateTime(this.dateTimePicker1.Text)+")";
      

  2.   

    你的数据库是什么类型DateTime a=dateTimePicker1.value;如果数据库是DateTime类型
                System.Data.SqlClient.SqlParameter para = new System.Data.SqlClient.SqlParameter("@CreateDate", SqlDbType.DateTime);
               para.Value = d;最好把你的sql语句那一块检查一下,应该是类型不一致造成的
      

  3.   

    如果不是参数方式
    改成下面的sql语句试试string sql="insert into emp(hiredate) values ('"+this.dateTimePicker1.Text+"')";
      

  4.   

    不行的,我试过了啊,报ORA-01861错误
      

  5.   

    能把string类型的隐式转换成date型吗?
      

  6.   

    oralce的改成
    DateTime a=dateTimePicker1.value; string sql="insert into emp(hiredate) values (to_date('"+a.ToString("yyyy-MM-dd")+"','yyyy-MM-dd'))";
      

  7.   

    oracle中日期,要不用参数方式,要不必须得sql语句中进行转换如果是包括小时,分,秒
    则用
    to_date('2009-05-04 22:13:00','yyyy-MM-dd HH24:mm:ss')具体的查一下帮助
      

  8.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.OracleClient;namespace test6
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void btninsert_Click(object sender, EventArgs e)
            {
                try
                {
                    if (this.txtempno.Text=="")
                    {
                        MessageBox.Show("输入的员工工号为空,请输入!");
                        return;
                    }
                    if (this.txtename.Text=="")
                    {
                        MessageBox.Show("输入的员工姓名为空,请输入!");
                        return;
                    }                DateTime a = dateTimePickerhiredate.Value; 
                    string sconnection = "Data Source=vigorss; User ID=scott; Password=tiger";
                     string sql="insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values ("+int.Parse(this.txtempno.Text)+",'"+this.txtename.Text+"','"+this.txtjob.Text+"','"+int.Parse(this.txtmgr.Text)+"','"+this.dateTimePickerhiredate.Text+"','"+double.Parse(this.txtsal.Text)+"','"+double.Parse(this.txtcomm.Text)+"',"+int.Parse(this.txtdeptno.Text)+")";                 OracleConnection sconn=new OracleConnection(sconnection);
                     sconn.Open();
                     OracleCommand comm = new OracleCommand(sql, sconn);
                     comm.ExecuteNonQuery();                 try
                     {
                         MessageBox.Show("添加成功!");
                         sconn.Close();
                         Form2 frm2 = new Form2();
                         Form1 frm1 = new Form1();
                         frm2.Hide();
                         frm1.Show();
                     }
                     catch (Exception x)
                     {
                         MessageBox.Show("添加失败!", x.Message);
                         sconn.Close();
                     }
                     finally
                     {
                         if (sconn.State == ConnectionState.Open)
                         {
                             sconn.Close();
                         }
                     }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {            }  
                
            }        private void btnexit_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
      

  9.   


    string sql="insert into emp(hiredate) values (to_date('" + DateTime.Parse(dateTimePicker1.Text).ToString("yyyy-MM-dd") +"','yyyy-mon-dd'))";
      

  10.   

    不行啊,你把我的代码的整个字符串sql改好了发上来看看
      

  11.   


                string sql = string.Format("insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values ({0},'{1}','{2}',{3},to_date('{4}','yyyy-MM-dd'),{5},{6},{7})", this.txtempno.Text, this.txtename.Text, this.txtjob.Text, this.txtmgr.Text, this.dateTimePickerhiredate.Value.ToString("yyyy-MM-dd"), this.txtsal.Text, this.txtcomm.Text, this.txtdeptno.Text);
      

  12.   

    dateTimePicker1.Value.ToShortDateString()