比如我在前台上有一个控件dateTimePicker1,现在我想把它的值写入数据库,老是报类型不对,请问怎么解决
解决方案 »
- 请问,假设我并不知道一个object里面有什么属性和方法,能用反射获取里面所有的属性和方法并设置属性调用方法么?
- C#获取网页数据不完整,怎么办
- 请问Winform中的DataGrid能显示多少行啊?在哪儿进行设置?
- C#中将ACCESS的数据写入SQL SERVER,出错?在线等!
- c#中的打印功能如何实现?
- winform中如何取得DataGrid當前單元格的位置?實在沒分了,幫幫忙,很急的!
- c#波形图
- WinForm编程窗体间如何传递值?
- 如何把INI文件转换位XML格式的文件?
- C#编C/S三层结构的问题!
- ListView控件要怎么取到里面项的值
- 手动向DataGridView中添加列名和相应的数据
System.Data.SqlClient.SqlParameter para = new System.Data.SqlClient.SqlParameter("@CreateDate", SqlDbType.DateTime);
para.Value = d;最好把你的sql语句那一块检查一下,应该是类型不一致造成的
改成下面的sql语句试试string sql="insert into emp(hiredate) values ('"+this.dateTimePicker1.Text+"')";
DateTime a=dateTimePicker1.value; string sql="insert into emp(hiredate) values (to_date('"+a.ToString("yyyy-MM-dd")+"','yyyy-MM-dd'))";
则用
to_date('2009-05-04 22:13:00','yyyy-MM-dd HH24:mm:ss')具体的查一下帮助
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();
}
}
}
string sql="insert into emp(hiredate) values (to_date('" + DateTime.Parse(dateTimePicker1.Text).ToString("yyyy-MM-dd") +"','yyyy-mon-dd'))";
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);