//没有测试过,不晓得能否通过编译
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data.OleDb;
public partial class test_test1 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
String MyConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d://temp.xls;Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
OleDbConnection ExcelConn = new OleDbConnection(MyConnection);
OleDbDataAdapter adapter = new OleDbDataAdapter("select * from [Sheet1$]", ExcelConn);
DataSet ds = new DataSet();
ExcelConn.Open();
adapter.Fill(ds,"exceldata");
string ConnetctionString = "Data Source=ZC;Initial Catalog=导师制信息平台;Integrated Security=True";
SqlConnection conn = new SqlConnection(ConnetctionString);
conn.Open();
for (int i = 0; i < ds.Tables["exceldata"].Rows.Count; i++)
{
string sqlconn="insert into course(cou_id,cou_name) values(@id,@name)";
SqlCommand cmd = new SqlCommand(sqlconn,conn);
cmd.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4));
cmd.Parameters["@id"].Value = ds.Tables["exceldata"].Rows[i][0].ToString();
cmd.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar, 100));
cmd.Parameters["@name"].Value = ds.Tables["exceldata"].Rows[i][1].ToString();
cmd.ExecuteNonQuery();
cmd.Dispose();
}
Response.Write("<script>alert('导入成功!')</script>");
adapter.Dispose();
ExcelConn.Close();
conn.Close();
}
}