当前位置: 代码迷 >> 综合 >> Mss sql excel 表格导入到数据库(sql拼接、DbHelperSQL.Query(strSql.ToString()) sql执行)
  详细解决方案

Mss sql excel 表格导入到数据库(sql拼接、DbHelperSQL.Query(strSql.ToString()) sql执行)

热度:11   发布时间:2023-12-12 19:47:38.0

第一步先要把你导入的excel表格上传至服务器然后获取该路径

然后.... 请看下面

protected void btnEdit_Click(object sender, EventArgs e){if (int.Parse(ddlProductType.SelectedValue) == 0){MessageBox.ShowRedirect(this, "请选择您要导入的分类!");return;}string fileUrl2 = "";fileUrl2 = txtShopName.Text.Trim();//获取上传的Excel的路径string fileUrl = Server.MapPath("/") + fileUrl2;//转成绝对路径this.InsetData(this.GetExcelDatatable(fileUrl));//前去执行插入数据}//2:Excel数据导入Datable//@param fileUrl 服务器文件路径//@return System.Data.DataTable dt protected System.Data.DataTable GetExcelDatatable(string fileUrl){//office2007之前 仅支持.xlsconst string cmdText = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;IMEX=1';";//支持.xls和.xlsx,即包括office2010等版本的   HDR=Yes代表第一行是标题,不是数据;//const string cmdText = "Provider=Microsoft.Ace.OleDb.12.0;Data Source={0};Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";System.Data.DataTable dt = null;//建立连接OleDbConnection conn = new OleDbConnection(string.Format(cmdText, fileUrl));//打开连接if (conn.State == ConnectionState.Broken || conn.State == ConnectionState.Closed){conn.Open();}System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);//获取Excel的第一个Sheet名称string sheetName = schemaTable.Rows[0]["TABLE_NAME"].ToString().Trim();//查询sheet中的数据string strSql = "select * from [" + sheetName + "]";OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn);DataSet ds = new DataSet();da.Fill(ds);dt = ds.Tables[0];return dt;}//3:从System.Data.DataTable导入数据到数据库//@param System.Data.DataTable dtprotected void InsetData(System.Data.DataTable dt){int i = 0;Mod_Product dto = new Mod_Product();string s = OperateHelper.Getpages(1);string d = OperateHelper.Getpages(2);string ProductName = "";string Other1 = ""; string Other2 = ""; string Other3 = ""; string Other4 = ""; string Other5 = "";string Other6 = ""; string Other7 = ""; string Other8 = ""; string Other9 = ""; string Other10 = "";int TypeID = int.Parse(ddlProductType.SelectedValue);if (TypeID == 0){MessageBox.ShowRedirect(this, "请选择您要导入的分类!");return;}StringBuilder strSql = new StringBuilder("insert into dbo.SW_Product  (ProductName,Model,TypeID,WebSiteID,AddDate,OrderBy,ShopState,Other1,Other2,Other3,Other4,Other5,Other6,Other7,Other8,Other9,Other10) VALUES");foreach (DataRow dr in dt.Rows){//此处遍历获取excel的数据ProductName = dr[0].ToString().Trim();Other1 = dr[1].ToString().Trim();Other2 = dr[2].ToString().Trim();Other3 = dr[3].ToString().Trim();Other4 = dr[4].ToString().Trim();Other5 = dr[5].ToString().Trim();Other6 = dr[6].ToString().Trim();Other7 = dr[7].ToString().Trim();Other8 = dr[8].ToString().Trim();Other9 = dr[9].ToString().Trim();Other10 = dr[10].ToString().Trim();i++;//此处执行你的数据库插入语句即可//  string strInsert = "insert into 'zhongbang9'.'dbo.SW_Product' (ProductName) values(" + ProductName + "',to_date('" + AddDate + "','yyyy-mm-dd hh24:mi:ss'))";//string strInsert = "insert into dbo.SW_Product  (ProductName,Model,TypeID,WebSiteID,AddDate,OrderBy,ShopState,Other1,Other2,Other3,Other4,Other5,Other6,Other7,Other8,Other9,Other10) VALUES" +//     " ('" + ProductName + "','" + Model + "','" + TypeID + "','" + AdminManage.WebSiteID + "','" + DateTime.Now + "','99','1','" + Other1 + "','" + Other2 + "','" + Other3 + "','" + Other4 + "','" + Other5 + "','" + Other6 + "','" + Other7 + "','" + Other8 + "','" + Other9 + "','" + Other10 + "')";//DbHelperSQL.Query(strInsert);strSql.Append("('" + ProductName + "','" + Model + "','" + TypeID + "','" + AdminManage.WebSiteID + "','" + DateTime.Now + "','99','1','" + Other1 + "','" + Other2 + "','" + Other3 + "','" + Other4 + "','" + Other5 + "','" + Other6 + "','" + Other7 + "','" + Other8 + "','" + Other9 + "','" + Other10 + "')");if (i < dt.Rows.Count){strSql.Append(",");}//dto.ProductName = ProductName;//dto.Model = Model;//dto.WebSiteID = AdminManage.WebSiteID;//dto.AddDate = DateTime.Now;//dto.OrderBy = 99;//dto.ShopState = 1;//dto.Other1 = Other1; dto.Other2 = Other2; dto.Other3 = Other3; dto.Other4 = Other4; dto.Other5 = Other5;//dto.Other6 = Other6; dto.Other7 = Other7; dto.Other8 = Other8; dto.Other9 = Other9; dto.Other10 = Other10;//dto.ID = BProduct.Add(dto, s, d);}if (i == dt.Rows.Count){DbHelperSQL.Query(strSql.ToString());MessageBox.ShowRedirect(this, "导入成功:共导入" + i + "组数据!若要查看导入的数据请移步产品列表查看!");}else{MessageBox.ShowRedirect(this, "未完全导入:共导入" + i + "组数据!若要查看导入的数据请移步产品列表查看!");}}

 

  相关解决方案