如何用日期时间数据填充数据表 [英] how to fill data table with date time data

查看:118
本文介绍了如何用日期时间数据填充数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里帮助!..我尝试用日期时间数据填充数据表,但是它给了我SQL异常.

help here!.. i try to fill data table with datetime data but its giving me sql exception.

DateTime date_time;
date_time = DateTime.Today.AddDays(-1);
db_con_open();
DataTable dt = new DataTable();
sql_quary = "select * from customer_details where             
customer_date_in=''Convert.ToString(date_time)''";
da = new SqlDataAdapter(sql_quary, db_con);
da.Fill(dt);//Conversion failed when converting datetime from character string.
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = dt;

推荐答案

这是您的问题
This is your problem
sql_quary = "select * from customer_details where
customer_date_in='Convert.ToString(date_time)'";


您需要将日期时间传递到SQL标准中


You need to either pass the date time into a SQL standard

sql = "select * from customer_details where customer_date_in='2010-03-02 00:00:00.000'"


或使用参数,以便您的sql看起来像这样


or use a parameter so your sql would look SOMETHING like this

//odbc
sql = "select * from customer_details where customer_date_in=?";
//MS SQL Server
sql = "select * from customer_details where customer_date_in=@customerdata";
//MySQL 
sql = "select * from customer_details where customer_date_in=?customerdata";
//PostgreSQL/Oracle
sql = "select * from customer_details where customer_date_in=:customerdata";


有关更多信息,请参阅我的文章

简单解释安全密码身份验证


不要硬编码
您的数据提供商



使用
来自.NET DataProvider的信息


For more information see my articles

Secure Password Authentication Explained Simply


Don''t hard code
your DataProviders



and Using
Information from the .NET DataProvider


这篇关于如何用日期时间数据填充数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆