将Excel导入SQL Server时如何插入日期
技术问答
182 人阅读
|
0 人回复
|
2023-09-12
|
我将一个Excel文件加载到SQL( o4 K0 {$ i9 R9 r# c5 i6 O
Server工作正常,但我想在导入文件时添加当前日期。这意味着每行都将加载日期数据。因此,每次我加载文件,新数据都应该使用当前日期,而旧数据仍然有旧日期。' o& b8 B k% _; S0 F( B
我怎样才能做到这一点 ?
9 M- x6 \5 c3 t9 P% j4 w导入Excel文件代码:
) R( m/ L* c% v7 ^string ssqltable = comboBox1.GetItemText(comboBox1.SelectedItem);string myexceldataquery = "select * from [" ssqltable "$]";try{ OleDbConnection oconn = new OleDbConnection(@"rovider=Microsoft.ACE.OLEDB.12.0;Data Source=" imagepath ";Extended Properties='Excel 12.0 Xml; HDR=YES;IMEX=1;';"); string ssqlconnectionstring = "Data Source=.;Initial Catalog=Bioxcell;Integrated Security=true"; OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery,oconn); oconn.Open(); SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring); DataTable dt = new DataTable(); dt.Load(oledbcmd.ExecuteReader(); bulkcopy.DestinationTableName = ssqltable; for (int i = 0; i 这是为了插入日期,但对我不起作用) Y) L; C& v4 Y9 h
if (ssqltable == "Overseas"){ conn.Open(); SqlCommand sqlc = new SqlCommand("delete from Overseas where Bonus = 'Bonus'",conn); sqlc.ExecuteScalar(); SqlCommand Update1 = new SqlCommand("Update Overseas set ID = 1 Where ProductCode = 9630",conn); Update1.ExecuteNonQuery(); SqlCommand Update2 = new SqlCommand("Update Overseas set ID = 2 Where ProductCode = 9628",conn); Update2.ExecuteNonQuery(); SqlCommand Update3 = new SqlCommand("Update Overseas set ID = 3 Where ProductCode = 9629",conn); Update3.ExecuteNonQuery(); SqlCommand Update4 = new SqlCommand("Update Overseas set ID = 4 Where ProductCode = 9632",conn); Update4.ExecuteNonQuery(); SqlCommand Update5 = new SqlCommand("Update Overseas set ID = 5 Where ProductCode = 9631",conn); Update5.ExecuteNonQuery(); SqlCommand Update6 = new SqlCommand("insert into Overseas (Date) Values (GETDATE())",conn); Update6.ExecuteNonQuery();}没有错,只插入一个日期。这个问题怎么解决?7 w# l7 _% g* _3 n* d6 p$ |1 b
9 i' r3 j7 m) W& H; U, z3 z 解决方案: |
|
|
|
|
|