0
我試圖導入excel文件到我的網站,然後將其內容保存在sql服務器 我的問題與sqlbulkcopy當它填充數據庫的一些列填充錯誤值是NULL!而列中有一個非空的double值!SqlBulkCopy插入null而不是雙重
這裏是代碼:
void ImporttoDatatable()
{
try
{
if (FileUpload3.HasFile)
{
string FileName = FileUpload3.FileName;
string path = Path.Combine(Server.MapPath("~/ImportDocument"), Guid.NewGuid().ToString() + Path.GetExtension(FileUpload3.PostedFile.FileName));
FileUpload3.PostedFile.SaveAs(path);
using (OleDbConnection OleDbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + "; Extended Properties = \"Excel 8.0;HDR=Yes;IMEX=2\" "))
{
conn.Open();
OleDbcon.Open();
OleDbCommand command = new OleDbCommand("Select [IdOftable], [Time],[InstrumentLeftHand],[LeftSwitch],[LeftKnob],[ForceFeedbackLeftHand],[CumTimeLeftForceOverThreshold],[CumTimeLeftForceOver2xThreshold],[TranslationLeft_x],[TranslationLeft_y],[TranslationLeft_z],[quatLeft_x],[quatLeft_y],[quatLeft_z],[quatLeft_w],[InstrumentRightHand],[RightSwitch],[RightKnob],[ForceFeedbackRightHand],[CumTimeRightForceOverThreshold],[CumTimeRightForceOver2xThreshold],[TranslationRight_x],[TranslationRight_y],[TranslationRight_z],[quatRight_x],[quatRight_y],[quatRight_z],[quatRight_w],[BloodEmittedFrame],[BloodCurrentFrame],[TotalBloodEmitted],[TotalWhiteFibreCut],[TotalRedFibreCut],[Volume0_Brain],[Volume1_Tumor],[Volume2_Tumor] from [Sheet1$]", OleDbcon);
//OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(command);
DbDataReader dr = command.ExecuteReader();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))
{
bulkCopy.DestinationTableName = "MyExcel";
try
{
bulkCopy.BulkCopyTimeout = 400;
bulkCopy.WriteToServer(dr);
bulkCopy.BatchSize = 16000;
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
dr.Close();
}
OleDbcon.Close();
bulkCopy.Close();
}
}
} lblmessage.Text = "The File Succssesfully Imported ";
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
,這是SQL表定義:
CREATE TABLE [dbo].[MyExcel] (
[IdOftable] NVARCHAR (50) NOT NULL,
[Time] FLOAT (53) NULL,
[InstrumentLeftHand] NVARCHAR (50) NULL,
[LeftSwitch] FLOAT (53) NULL,
[LeftKnob] FLOAT (53) NULL,
[ForceFeedbackLeftHand] FLOAT (53) NULL,
[CumTimeLeftForceOverThreshold] FLOAT (53) NULL,
[CumTimeLeftForceOver2xThreshold] FLOAT (53) NULL,
[TranslationLeft_x] FLOAT (53) NULL,
[TranslationLeft_y] FLOAT (53) NULL,
[TranslationLeft_z] FLOAT (53) NULL,
[quatLeft_x] FLOAT (53) NULL,
[quatLeft_y] FLOAT (53) NULL,
[quatLeft_z] FLOAT (53) NULL,
[quatLeft_w] FLOAT (53) NULL,
[InstrumentRightHand] NVARCHAR (50) NULL,
[RightSwitch] FLOAT (53) NULL,
[RightKnob] FLOAT (53) NULL,
[ForceFeedbackRightHand] FLOAT (53) NULL,
[CumTimeRightForceOverThreshold] FLOAT (53) NULL,
[CumTimeRightForceOver2xThreshold] FLOAT (53) NULL,
[TranslationRight_x] FLOAT (53) NULL,
[TranslationRight_y] FLOAT (53) NULL,
[TranslationRight_z] FLOAT (53) NULL,
[quatRight_x] FLOAT (53) NULL,
[quatRight_y] FLOAT (53) NULL,
[quatRight_z] FLOAT (53) NULL,
[quatRight_w] FLOAT (53) NULL,
[BloodEmittedFrame] NVARCHAR (50) NULL,
[BloodCurrentFrame] FLOAT (53) NULL,
[TotalBloodEmitted] FLOAT (53) NULL,
[TotalWhiteFibreCut] FLOAT (53) NULL,
[TotalRedFibreCut] FLOAT (53) NULL,
[Volume0_Brain] FLOAT (53) NULL,
[Volume1_Tumor] FLOAT (53) NULL,
[Volume2_Tumor ] FLOAT (53) NULL,
PRIMARY KEY CLUSTERED ([IdOftable] ASC)
);
有,當我跑的代碼,但沒有例外,當我檢查了數據的服務器裏面有一些值不爲空值=(
我使用visual studio express 2012 for web。怎麼辦才能糾正它?
謝謝
但除了一個所有的字段可以有NULL值... – e4rthdog 2014-11-06 08:38:02
我沒有檢查的'SqlBulkCopy.WriteToServer(IDataReader的)內部工作',但我的猜測是,由於從電子表格讀不給一個強類型的讀者,在那裏我是一個相當寬鬆的轉換器,在轉換失敗時使用'DbNull'。我昨天遇到了這個問題,並發現添加額外的代碼來顯式迭代讀者,並用顯式轉換填充一個強類型的'DataTable',然後將這個表寫入服務器的速度比試圖反映.NET更快準確找出發生這種情況的原因。 – GarethD 2014-11-06 08:40:07
@ e4rthdog謝謝你的回覆,但是當我改變它不爲null異常「system.invalidoperationexception列」列名'不允許dbnull.value「會出現這意味着相同的問題出來與null或沒有它=( – seetah 2014-11-06 09:33:20