从excel批量批量导入到sql,在现等!谢谢
private void button1_Click(object sender, EventArgs e){
openFileDialog1.ShowDialog();
textBox1.Text = openFileDialog1.FileName;
}
private void button2_Click_1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=820828;database=kf");
// 连接excel数据源
string excelconnstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" + textBox1.Text + ";Extended Properties=Excel 8.0";
OleDbConnection excelconn = new OleDbConnection(excelconnstring);
OleDbDataAdapter mycomm = new OleDbDataAdapter("select * from [Sheet1$]", excelconn);
DataSet myds = new DataSet();
mycomm.Fill(myds);
SqlCommand cm = new SqlCommand();
cm.Connection = con;
con.Open();
for (int i = 0; i < myds.Tables[0].Rows.Count; i++)
{
string updateSql1 = "insert into newsclass(id,cid,cname)values('" + myds.Tables[0].Rows[i]["id"] + "','" + myds.Tables[0].Rows[i]["cid"] + "','" + myds.Tables[0].Rows[i]["cname"] + "')";
cm.CommandText = updateSql1;
try
{
cm.ExecuteNonQuery();
this.label2.Visible = true;
this.label2.Text = "数据导入成功!";
}
catch
{
}
}
cm.Dispose();
con.Close();
}
大家帮帮我吧!谢谢了 --------------------编程问答-------------------- 大家帮帮我啊 --------------------编程问答-------------------- 应该可以实现呀,LZ什么意思 --------------------编程问答-------------------- 提供一个Excel文件操作引擎类
http://blog.csdn.net/suosuoyyy/archive/2008/06/11/2534967.aspx --------------------编程问答-------------------- 先将Excel读入DataSet然后将DataSet存入数据库。
读Excel
private DataSet BindDsFromExcel(string strFileDir, string strDataName)
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFileDir + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();
String sql = "SELECT * FROM [" + strDataName + "$]";
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
DataSet OleDsExcle = new DataSet();
OleDaExcel.Fill(OleDsExcle, strDataName);
OleConn.Close();
return OleDsExcle;
}
保存到数据库应该没什么问题了吧。 --------------------编程问答-------------------- winform?
心痛啊 忘了都
补充:.NET技术 , C#