我正在使用SqlBulkCopy對象將數據表寫入sql服務器表。但是,每次我重新檢查我的數據庫時,它都保持不變,沒有任何更改。 A爲什麼我的SqlBulkCopy不起作用
我試圖做谷歌搜索來確定我的問題,但我無法解決它。
數據表來自.xls文件。
public static DataTable dt = new DataTable();
private void ExportToGrid(String path, String filen)
{
int idx = filen.IndexOf(".");
string tf = filen.Remove(idx, 4);
OleDbConnection MyConnection = null;
DataSet DtSet = null;
OleDbDataAdapter MyCommand = null;
MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" + path + "';Extended Properties=Excel 8.0;");
ArrayList TblName = new ArrayList();
MyConnection.Open();
DataTable schemaTable = MyConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow row in schemaTable.Rows)
{
TblName.Add(row["TABLE_NAME"]);
}
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + TblName[0].ToString() + "]", MyConnection);
DtSet = new System.Data.DataSet();
MyCommand.Fill(DtSet);
MyCommand.FillSchema(DtSet, SchemaType.Source);
DataTable dt = new DataTable();
dt = DtSet.Tables[0];
Session["dt"] = dt;
int x = dt.Rows.Count;
MyConnection.Close();
if (dt.Rows.Count > 0)
{
theGridView.DataSource = dt;
theGridView.DataBind();
}
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
}
這是我寫器功能
private void StartImport()
{
string servername = server;
string database = database;
string tbl = "dbo.LinkDb";
Stopwatch sw = new Stopwatch();
sw.Start();
SqlBulkCopy bulkCopy = new SqlBulkCopy("Data Source=" + servername + ";Initial Catalog=" + database + ";Integrated Security=SSPI", SqlBulkCopyOptions.TableLock);
bulkCopy.DestinationTableName = tbl;
bulkCopy.WriteToServer(dt);
sw.Stop();
lblResult.Visible = true;
lblResult.Text = (sw.ElapsedMilliseconds/1000.00).ToString();
}
下面是存儲在我的SQL Server表的截圖。我向你保證,我一直遵從區分大小寫的規則。
有沒有異常拋出經過平均時間是0.018 - 0.020秒
欣賞任何幫助。
謝謝
當您調用'WriteToServer'時,'dt'有多少行?這些列是否匹配? (後者雖然應該表現爲例外,而不是做任何事情) – 2012-02-08 08:28:22
我認爲這些列匹配。 dt中還有一行。我也感到困惑,爲什麼它不工作,它沒有拋出任何異常...... – rofans91 2012-02-08 08:34:06