C# EXCEL导入到DataSet上的数据写入数据源
C#WINFORM:我在窗体上建了一个dataGridView1,里面的数据是我从EXCEL里导入的数据,现在我要将某些列全部导入到SQL数据库的某个表对应的字段,高手们...
C# WINFORM :
我在窗体上建了一个dataGridView1,里面的数据是我从EXCEL里导入的数据,现在我要将某些列全部导入到SQL数据库的某个表对应的字段,高手们,真么写;希望收到两种方法,一是一条一条的写入数据源,二是批量的写入数据源! 展开
我在窗体上建了一个dataGridView1,里面的数据是我从EXCEL里导入的数据,现在我要将某些列全部导入到SQL数据库的某个表对应的字段,高手们,真么写;希望收到两种方法,一是一条一条的写入数据源,二是批量的写入数据源! 展开
3个回答
展开全部
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + MsgBox.getFileLastName(File1.Value);
File1.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("~/Excel/" + fileName));
string conn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + Server.MapPath("~/Excel/" + fileName) + ";Extended Properties=Excel 8.0";
OleDbConnection thisconnection = new OleDbConnection(conn);
thisconnection.Open();
string Sql = "select DISTINCT(厂牌) from [Sheet1$]";
OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, thisconnection);
DataSet ds = new DataSet();
mycommand.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataTable dt2 = ConString.QuerySql("select * from protype where name='" + ds.Tables[0].Rows[i]["厂牌"].ToString().Replace("'", "") + "' and languageid='" + Session["language"] + "'").Tables[0];
if (dt2.Rows.Count < 1)
{
ConString.ExecuteSql("insert protype (name,languageid)values('" + ds.Tables[0].Rows[i]["厂牌"].ToString().Replace("'", "") + "','" + Session["language"] + "')");
}
}
try
{
DataTable dt = ConString.QuerySql("select * from protype").Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
string name = dt.Rows[i]["name"].ToString();
string id = dt.Rows[i]["id"].ToString();
string Sql2 = "select * from [Sheet1$] where 厂牌='" + name + "'";
OleDbDataAdapter mycommand2 = new OleDbDataAdapter(Sql2, thisconnection);
DataSet ds2 = new DataSet();
mycommand2.Fill(ds2);
for (int ii = 0; ii < ds2.Tables[0].Rows.Count; ii++)
{
DataTable dt3 = ConString.QuerySql("select * from product where name='" + ds2.Tables[0].Rows[ii]["型号"].ToString() + "'and languageid='" + Session["language"] + "'").Tables[0];
if (dt3.Rows.Count < 1)
{
//ConString.ExecuteSql("insert product (type_id,name,num,pice,file1,languageid)values('" + id + "','" + ds2.Tables[0].Rows[ii]["型号"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["数量"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["价格"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["设备描述"].ToString().Replace("'", "") + "','" + Session["language"] + "')");
ConString.ExecuteSql("insert product (type_id,name,num,pice,file1,languageid,shows,top_num,fact,img)values('" + id + "','" + ds2.Tables[0].Rows[ii]["型号"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["数量"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["价格"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["设备描述"].ToString().Replace("'", "") + "','" + Session["language"] + "','" + ds2.Tables[0].Rows[ii]["热门"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["排序"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["其他厂牌"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["图片"].ToString().Replace("'", "") + "')");
}
}
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
Response.End();
}
thisconnection.Close();
File.Delete(Server.MapPath("~/Excel/" + fileName));
MsgBox.alert("导入产品成功~!");
}
catch(Exception ex)
{
MsgBox.alert(ex.Message);
}
}
完全自己编写,如有问题可以HI留言。。。
{
try
{
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + MsgBox.getFileLastName(File1.Value);
File1.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("~/Excel/" + fileName));
string conn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + Server.MapPath("~/Excel/" + fileName) + ";Extended Properties=Excel 8.0";
OleDbConnection thisconnection = new OleDbConnection(conn);
thisconnection.Open();
string Sql = "select DISTINCT(厂牌) from [Sheet1$]";
OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, thisconnection);
DataSet ds = new DataSet();
mycommand.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataTable dt2 = ConString.QuerySql("select * from protype where name='" + ds.Tables[0].Rows[i]["厂牌"].ToString().Replace("'", "") + "' and languageid='" + Session["language"] + "'").Tables[0];
if (dt2.Rows.Count < 1)
{
ConString.ExecuteSql("insert protype (name,languageid)values('" + ds.Tables[0].Rows[i]["厂牌"].ToString().Replace("'", "") + "','" + Session["language"] + "')");
}
}
try
{
DataTable dt = ConString.QuerySql("select * from protype").Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
string name = dt.Rows[i]["name"].ToString();
string id = dt.Rows[i]["id"].ToString();
string Sql2 = "select * from [Sheet1$] where 厂牌='" + name + "'";
OleDbDataAdapter mycommand2 = new OleDbDataAdapter(Sql2, thisconnection);
DataSet ds2 = new DataSet();
mycommand2.Fill(ds2);
for (int ii = 0; ii < ds2.Tables[0].Rows.Count; ii++)
{
DataTable dt3 = ConString.QuerySql("select * from product where name='" + ds2.Tables[0].Rows[ii]["型号"].ToString() + "'and languageid='" + Session["language"] + "'").Tables[0];
if (dt3.Rows.Count < 1)
{
//ConString.ExecuteSql("insert product (type_id,name,num,pice,file1,languageid)values('" + id + "','" + ds2.Tables[0].Rows[ii]["型号"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["数量"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["价格"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["设备描述"].ToString().Replace("'", "") + "','" + Session["language"] + "')");
ConString.ExecuteSql("insert product (type_id,name,num,pice,file1,languageid,shows,top_num,fact,img)values('" + id + "','" + ds2.Tables[0].Rows[ii]["型号"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["数量"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["价格"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["设备描述"].ToString().Replace("'", "") + "','" + Session["language"] + "','" + ds2.Tables[0].Rows[ii]["热门"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["排序"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["其他厂牌"].ToString().Replace("'", "") + "','" + ds2.Tables[0].Rows[ii]["图片"].ToString().Replace("'", "") + "')");
}
}
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
Response.End();
}
thisconnection.Close();
File.Delete(Server.MapPath("~/Excel/" + fileName));
MsgBox.alert("导入产品成功~!");
}
catch(Exception ex)
{
MsgBox.alert(ex.Message);
}
}
完全自己编写,如有问题可以HI留言。。。
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以的遍历dg 循环导入db
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询