C# EXCEL导入到DataSet上的数据写入数据源

来源:百度知道 编辑:UC知道 时间:2024/05/30 23:18:02
C# WINFORM :

我在窗体上建了一个dataGridView1,里面的数据是我从EXCEL里导入的数据,现在我要将某些列全部导入到SQL数据库的某个表对应的字段,高手们,真么写;希望收到两种方法,一是一条一条的写入数据源,二是批量的写入数据源!

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][