1

我是 C# 新手,我有一个 C# 程序和一个从另一个程序导出的 excel 文件。如何将其转换为日期时间?这是代码,其中dtFromdtToDateTimePicker变量:

System.Data.DataTable dtExcel = new System.Data.DataTable();
dtExcel.TableName = "MyExcelData";
string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" +        this.textBox1.Text + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
OleDbConnection con = new OleDbConnection(SourceConstr);
string query = "Select Customer, SUM(Wt) AS Weight from [Sheet0$] WHERE (CONVERT(datetime, [Date]) >= " + this.dtFrom.Value.Month + ") GROUP BY Customer order by Customer"; 
OleDbDataAdapter data = new OleDbDataAdapter(query, con);
data.Fill(dtExcel);
4

1 回答 1

0

我认为这可能不是您唯一的问题,但要回答您的问题,如果您更换

string query = "Select Customer, SUM(Wt) AS Weight from [Sheet0$] WHERE (CONVERT(datetime, [Date]) >= " + this.dtFrom.Value.Month + ") GROUP BY Customer order by Customer"; 
OleDbDataAdapter data = new OleDbDataAdapter(query, con);

string query = "Select Customer, SUM(Wt) AS Weight from [Sheet0$] WHERE (CONVERT(datetime, [Date]) >= @Date) GROUP BY Customer order by Customer"; 
OleDbCommand command = new OleDbCommand(query);
command.Parameters.Add("@Date", OleDbType.DBTimeStamp).Value = this.dtFrom.Value;
OleDbDataAdapter data = new OleDbDataAdapter(command, con);

你可能会走在正确的轨道上。不能 100% 确定OleDbType您需要哪个,但您可以很容易地看到可用的选项。

于 2012-11-22T10:11:01.243 回答