0

OLEDBを使用して日時ピッカーを使用してExcelファイルをクエリしていますが、Cireria式エラーでデータ型の不一致が発生し続けます。

日付のExcelの形式は「2012年6月8日10:00」です。

        DateTime time = dateTimePicker1.Value;            

        MyCommand = new OleDbDataAdapter("select * from [CR$] where ([Req Start Date] >='" + time + "')", MyConnection);



        DtSet = new System.Data.DataSet();
        MyCommand.Fill(DtSet);


        bindingSource1 = new BindingSource();
        bindingSource1.DataSource = DtSet;
        bindingSource1.DataMember = DtSet.Tables[0].TableName;
        dataGridView1.DataSource = bindingSource1;

        MyConnection.Close();
4

2 に答える 2

1

クエリに文字列として時間を渡しているので、ToString()を使用してクエリを機能させることができます。

MyCommand = new OleDbDataAdapter("select * from [CR$] where ([Req Start Date] >='" + time.ToString("%M/dd/yyyy HH:mm") + "')", MyConnection);

しかし、あなたは本当にそれをパラメータにするべきです。さらに、その方が安全です。

    using (OleDbConnection connection = new OleDbConnection(yourConnectionString))
    {
        OleDbDataAdapter adapter = new OleDbDataAdapter("select * from [CR$] where [Req Start Date] >= ?", connection);
        adapter.SelectCommand.Parameters.Add("@p1", OleDbType.Date);
        adapter.SelectCommand.Parameters["@p1"].Value = time;

        try
        {
            connection.Open();
            adapter.Fill(DtSet);
        }
        catch (Exception ex)
        {
            //handle error
        }
    }

詳細:OleDbParameterクラス

于 2012-11-21T22:12:56.440 に答える
0

OleDbCommandを作成し、値をパラメーターとして渡します。次に、コマンドをOleDbAdapterコンストラクターのパラメーターとして使用します。

string queryString = "select * from [CR$] where ([Req Start Date] >= ?)";
OleDbCommand command = new OleDbCommand(queryString, connection);
command.Parameters.Add("@p1", OleDbType.DateTime).Value = time;
MyCommand = new OleDbDataAdapter(queryString, MyConnection);
于 2012-11-21T22:14:06.920 に答える