如何用日期时间数据填充数据表

问题描述:

在这里帮助!..我尝试用日期时间数据填充数据表,但是它给了我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