2

我想从 SQLite 数据库中读取日期时间值并将其分配给日期选择器控件。

这是我的代码:

try
{
    sqlitecon.Open();
    string Query = "Select * from Customer_New where Cust_Id='" + val + "'  ";
    SQLiteCommand createCommand = new SQLiteCommand(Query, sqlitecon);
    //  createCommand.ExecuteNonQuery();
    SQLiteDataReader dr = createCommand.ExecuteReader();
    while(dr.Read()){
        date_open.DisplayDate = Convert.ToDateTime(dr.GetString(0));
        Date_Joining.DisplayDate = Convert.ToDateTime(dr.GetString(1));     
        txt_Title.Text = dr.GetString(3);
        txt_cardnum.Text =dr.GetString(4)          
    }                
    sqlitecon.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

但它给出了这个错误:“指定的演员表无效”。怎么做到呢?。

4

2 回答 2

2

If you already get a DateTime from the database you should not convert it to a string. Then just use:

date_open.DisplayDate = dr.GetDateTime(0);

instead of

date_open.DisplayDate = Convert.ToDateTime(dr.GetString(0));

If it's not a DateTime but a string(varchar) you should consider to change that.

Edit: Also note that you are using two different columns of the query to assign the datatime to your DateTimePicker, so right after above line you do mthis:

date_open.DisplayDate = Convert.ToDateTime(dr.GetString(1)); 

One of both must be incorrect. Which column is the datetime column?

Also, you should parametrize your queries to avoid sql-injection:

string Query = "Select * from Customer_New where Cust_Id=?1";
command.Parameters.Add(new SQLiteParameter { ParameterName = "1", Value = val }); 

(note that i have never used SqlLite, so the code is untested)

于 2013-10-09T09:34:45.357 回答
0
Select strftime([date],0) as [date] from Customer_New where Cust_Id=?1
于 2014-06-30T14:49:39.133 回答