C# sqlbulkcopy-不允许DBNull.Value
我只想制作一个xls映射工具。这是我的sql表:C# sqlbulkcopy-不允许DBNull.Value,c#,asp.net-4.0,sqlbulkcopy,C#,Asp.net 4.0,Sqlbulkcopy,我只想制作一个xls映射工具。这是我的sql表: Id --> int(Not NULL) auto incremented Name --> varchar(50) (Not NULL) type_id --> int(Not NULL) Ownername --> varchar (Allow NULL) Ownermob --> varchar (Allow NULL) Room --> varchar (Allow NULL) Build -->
Id --> int(Not NULL) auto incremented
Name --> varchar(50) (Not NULL)
type_id --> int(Not NULL)
Ownername --> varchar (Allow NULL)
Ownermob --> varchar (Allow NULL)
Room --> varchar (Allow NULL)
Build --> varchar (Allow NULL)
Road --> varchar (Allow NULL)
Area --> varchar (Allow NULL)
City --> varchar (Allow NULL)
Phone --> varchar (Allow NULL)
Mobile --> varchar (Allow NULL)
Email --> varchar (Allow NULL)
ContactPerson --> varchar (Allow NULL)
ContactPersonmob --> varchar (Allow NULL)
UserOFC --> Bool (Allow NULL)
UserVAT --> Bool (Allow NULL)
UserINV --> Bool (Allow NULL)
UserNone --> Bool (Allow NULL)
state_id --> int (Allow NULL)
country_id --> int (Allow NULL)
Remark --> text (Allow NULL)
Register_Date --> Datetime (Not NUll)
User_id --> varchar (Not NUll)
这是我的密码:
protected void lbut_import_Click(object sender, EventArgs e)
{
Page.Validate("ImportXLS");
if (FileUpload1.HasFile)
{
if (FileUpload1.FileContent.Length > 0)
{
string Foldername;
string Extension = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
string filename = DateTime.Now.ToString("ddMMyyyy_HHmmss");
if (Extension == ".XLS" || Extension == ".XLSX" || Extension == ".xls" || Extension == ".xlsx")
{
Foldername = Server.MapPath("~/Files/");
FileUpload1.PostedFile.SaveAs(Foldername + filename + Extension);
string conString = string.Empty;
switch (Extension)
{
case ".xls": //Excel 97-03
conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //Excel 07
conString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
conString = string.Format(conString, Foldername + filename + Extension);
using (OleDbConnection excel_con = new OleDbConnection(conString))
{
excel_con.Open();
string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
DataTable dtExcelData = new DataTable();
//[OPTIONAL]: It is recommended as otherwise the data will be considered as String by default.
dtExcelData.Columns.AddRange(new DataColumn[23]
{new DataColumn("Name", typeof(string)),
new DataColumn("type_id", typeof(int)),
new DataColumn("Ownername",typeof(string)),
new DataColumn("Ownermob",typeof(string)),
new DataColumn("Room",typeof(string)),
new DataColumn("Build",typeof(string)),
new DataColumn("Road",typeof(string)),
new DataColumn("Area",typeof(string)),
new DataColumn("City",typeof(string)),
new DataColumn("Phone",typeof(string)),
new DataColumn("Mobile",typeof(string)),
new DataColumn("Email",typeof(string)),
new DataColumn("ContactPerson",typeof(string)),
new DataColumn("ContactPersonmob",typeof(string)),
new DataColumn("UserOFC",typeof(bool)),
new DataColumn("UserVAT",typeof(bool)),
new DataColumn("UserINV",typeof(bool)),
new DataColumn("UserNone",typeof(bool)),
new DataColumn("state_id",typeof(int)),
new DataColumn("country_id",typeof(int)),
new DataColumn("Remark",typeof(string)),
new DataColumn("Register_Date",typeof(DateTime)),
new DataColumn("User_id",typeof(string)),
});
using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))
{
oda.Fill(dtExcelData);
}
excel_con.Close();
string consString = ConfigurationManager.ConnectionStrings["CRMConnectionString"].ToString();
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
//Set the database table name
sqlBulkCopy.DestinationTableName = "tbl_Party_master";
//[OPTIONAL]: Map the Excel columns with that of the database table
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col.Text.Trim())].ToString(), "Name");
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col1.Text.Trim())].ToString(), "type_id");
if (!string.IsNullOrEmpty(txt_col2.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col2.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col2.Text.Trim())].ToString(), "Ownername");
}
if (!string.IsNullOrEmpty(txt_col3.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col3.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col3.Text.Trim())].ToString(), "Ownermob");
}
if (!string.IsNullOrEmpty(txt_col4.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col4.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col4.Text.Trim())].ToString(), "Room");
}
if (!string.IsNullOrEmpty(txt_col5.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col5.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col5.Text.Trim())].ToString(), "Build");
}
if (!string.IsNullOrEmpty(txt_col6.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col6.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col6.Text.Trim())].ToString(), "Road");
}
if (!string.IsNullOrEmpty(txt_col7.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col7.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col7.Text.Trim())].ToString(), "Area");
}
if (!string.IsNullOrEmpty(txt_col8.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col8.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col8.Text.Trim())].ToString(), "City");
}
if (!string.IsNullOrEmpty(txt_col9.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col9.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col9.Text.Trim())].ToString(), "Phone");
}
if (!string.IsNullOrEmpty(txt_col10.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col10.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col10.Text.Trim())].ToString(), "Mobile");
}
if (!string.IsNullOrEmpty(txt_col11.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col11.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col11.Text.Trim())].ToString(), "Email");
}
if (!string.IsNullOrEmpty(txt_col12.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col12.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col12.Text.Trim())].ToString(), "ContactPerson");
}
if (!string.IsNullOrEmpty(txt_col13.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col13.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col13.Text.Trim())].ToString(), "ContactPersonmob");
}
if (!string.IsNullOrEmpty(txt_col14.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col14.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col14.Text.Trim())].ToString(), "UserOFC");
}
if (!string.IsNullOrEmpty(txt_col15.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col15.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col15.Text.Trim())].ToString(), "UserVAT");
}
if (!string.IsNullOrEmpty(txt_col16.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col16.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col16.Text.Trim())].ToString(), "UserINV");
}
if (!string.IsNullOrEmpty(txt_col17.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col17.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col17.Text.Trim())].ToString(), "UserNone");
}
if (!string.IsNullOrEmpty(txt_col18.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col18.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col18.Text.Trim())].ToString(), "state_id");
}
if (!string.IsNullOrEmpty(txt_col19.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col19.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col19.Text.Trim())].ToString(), "country_id");
}
if (!string.IsNullOrEmpty(txt_col20.Text.Trim()) && !string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col20.Text.Trim())].ToString()))
{
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col20.Text.Trim())].ToString(), "Remark");
}
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col21.Text.Trim())].ToString(), "Register_Date");
sqlBulkCopy.ColumnMappings.Add(dtExcelData.Columns[int.Parse(txt_col22.Text.Trim())].ToString(), "User_id");
con.Open();
sqlBulkCopy.WriteToServer(dtExcelData);
con.Close();
}
}
StringBuilder sb = new StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("alert('");
sb.Append(dtExcelData.Rows.Count.ToString());
sb.Append(" Rows(s) are Inserted.');");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "script", sb.ToString());
if (System.IO.File.Exists(Foldername + filename + Extension))
{
System.IO.File.Delete(Foldername + filename + Extension);
}
}
}
}
ibtnimexls_ModalPopupExtender.Show();
}
}
protectedvoid lbut\u import\u单击(对象发送方,事件参数e)
{
第页:验证(“进口”);
if(FileUpload1.HasFile)
{
如果(FileUpload1.FileContent.Length>0)
{
字符串Foldername;
字符串扩展名=System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
字符串文件名=DateTime.Now.ToString(“ddMMyyyy_HHmmss”);
如果(扩展名=“.XLS”| |扩展名=”.XLSX“| |扩展名=”.XLS“| |扩展名=”.XLSX“)
{
Foldername=Server.MapPath(“~/Files/”);
FileUpload1.PostedFile.SaveAs(Foldername+文件名+扩展名);
string conString=string.Empty;
交换机(分机)
{
案例“.xls”://Excel 97-03
conString=ConfigurationManager.ConnectionString[“Excel03ConString”]。ConnectionString;
打破
案例“.xlsx”://Excel 07
conString=ConfigurationManager.ConnectionString[“Excel07ConString”]。ConnectionString;
打破
}
conString=string.Format(conString,Foldername+文件名+扩展名);
使用(OLEDB连接excel\u con=新OLEDB连接(构造))
{
excel_con.Open();
string sheet1=excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null)。行[0][“TABLE_NAME”]。ToString();
DataTable dtExcelData=新DataTable();
//[可选]:建议这样做,否则默认情况下数据将被视为字符串。
dtExcelData.Columns.AddRange(新数据列[23]
{新数据列(“名称”,类型(字符串)),
新数据列(“type_id”,typeof(int)),
新数据列(“所有者名称”,类型(字符串)),
新数据列(“Ownermob”,类型(字符串)),
新数据列(“房间”,类型(字符串)),
新数据列(“构建”,类型(字符串)),
新数据列(“道路”,类型(字符串)),
新数据列(“区域”,类型(字符串)),
新数据列(“城市”,类型(字符串)),
新数据列(“电话”,类型(字符串)),
新数据列(“移动”,类型(字符串)),
新数据列(“电子邮件”,类型(字符串)),
新数据列(“联系人”,类型(字符串)),
新数据列(“ContactPersonmob”,类型(字符串)),
新数据列(“UserOFC”,typeof(bool)),
新数据列(“用户增值税”,类型(bool)),
新数据列(“UserINV”,typeof(bool)),
新数据列(“UserNone”,typeof(bool)),
新数据列(“state_id”,typeof(int)),
新数据列(“国家/地区id”,类型(int)),
新数据列(“备注”,类型(字符串)),
新数据列(“注册日期”,类型(日期时间)),
新数据列(“用户id”,类型(字符串)),
});
使用(OleDbDataAdapter oda=新的OleDbDataAdapter(“从[“+sheet1+”]”中选择*,excel_-con))
{
oda.Fill(数据);
}
excel_con.Close();
string constring=ConfigurationManager.ConnectionString[“CRMConnectionString”].ToString();
使用(SqlConnection con=newsqlconnection(constring))
{
使用(SqlBulkCopy SqlBulkCopy=newsqlbulkcopy(con))
{
//设置数据库表名
sqlBulkCopy.DestinationTableName=“tbl\u Party\u master”;
//[可选]:将Excel列映射到数据库表的列
添加(dtExcelData.Columns[int.Parse(txt_col.Text.Trim())].ToString(),“Name”);
添加(dtExcelData.Columns[int.Parse(txt_col1.Text.Trim())].ToString(),“type_id”);
如果(!string.IsNullOrEmpty(txt_col2.Text.Trim())和&!string.IsNullOrEmpty(dtExcelData.Columns[int.Parse(txt_col2.Text.Trim())].ToString())
{
添加(dtExcelData.Columns[int.Parse(txt_col2.Text.Trim())].ToString(),“Ownername”);
}
如果(!string.IsNullOrEmpty(txt_col3.Text.Trim())和&!string.IsNullOrEmpty(dtExcelData.Columns[int.Pars])