excel表导入Sqlserver2005中!!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了excel表导入Sqlserver2005中!!相关的知识,希望对你有一定的参考价值。

有5000数据的excel 想导入到sql server 2005我指定的数据库里,不知道有什么办法可以实现?很郁闷!

在页面上放一个FileUpLoad控件和一个按钮,下面是后台代码,给你参考:
using System.Data.SqlClient;
using System.Data.OleDb;
protected void Button3_Click(object sender, EventArgs e)

if (FileUpload1.FileName == "")

Response.Write("<script>alert('尚未选择上传文件!')</script>");

else

string sConnectionString = //"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= '"+ File1.PostedFile.FileName.ToString() + "';Extended Properties= 'Excel 11.0;HDR=YES; ' ";
//string sConnectionString = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = G:\\test.xls;Extended Properties=Excel 8.0";
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + FileUpload1.PostedFile.FileName.ToString() + ";" +
"Extended Properties=\"Excel 8.0;\"";

//建立EXCEL的连接
OleDbConnection objConn = new OleDbConnection(sConnectionString);

// System.Data.OleDb.OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + File1.PostedFile.FileName.ToString() + ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";");

objConn.Open();

OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;

DataSet objDataset1 = new DataSet();

objAdapter1.Fill(objDataset1, "XLData");

// DataGrid1.DataSource = objDataset1.Tables[0].DefaultView; //测试代码,用来测试是否能读出EXCEL上面的数据
// DataGrid1.DataBind();

DataTable dt = objDataset1.Tables[0];
DataView myView = new DataView(dt);

//SQL SERVER的数据库连接
SqlConnection conn;
string dns = ConfigurationManager.ConnectionStrings["contoadmin"].ToString();//连接串
conn = new SqlConnection(dns);

SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO Student values(@stu_id,@stu_name,@stu_sex,@polity,@stu_birth,@stu_addr,@class_id,@remark)";
//cmd是插入数据库
SqlCommand cmd2 = conn.CreateCommand();
cmd2.CommandText = "INSERT INTO Login values(@stu_id,@stu_id,1)";

int count = 0;//用来记录出错的条数

try

foreach (DataRowView myDrv in myView)

count++;
//要关闭上一次的SQL Server的连接
if (conn.State.ToString() != "Closed")
conn.Close();

//每一次都要清空所有的CMD的参数
cmd.Parameters.Clear();
cmd2.Parameters.Clear();

//执行存储过程
//首先获得参数 共 3个
//@CardNo,@ToAddress,@CCAddress
SqlParameter stu_id = cmd.Parameters.Add("@stu_id", SqlDbType.Char);
SqlParameter stu_name = cmd.Parameters.Add("@stu_name", SqlDbType.Char);
SqlParameter stu_sex = cmd.Parameters.Add("@stu_sex", SqlDbType.Char);
SqlParameter polity = cmd.Parameters.Add("@polity", SqlDbType.Char);
SqlParameter stu_birth = cmd.Parameters.Add("@stu_birth", SqlDbType.DateTime);
SqlParameter stu_addr = cmd.Parameters.Add("@stu_addr", SqlDbType.Char);
SqlParameter class_id = cmd.Parameters.Add("@class_id", SqlDbType.Char);
SqlParameter remark = cmd.Parameters.Add("@remark", SqlDbType.Char);
cmd2.Parameters.Add("@stu_id", SqlDbType.Char).Value = myDrv[0].ToString().Trim();

//表示是输入参数
stu_id.Direction = ParameterDirection.Input;
stu_name.Direction = ParameterDirection.Input;
stu_sex.Direction = ParameterDirection.Input;
polity.Direction = ParameterDirection.Input;
stu_birth.Direction = ParameterDirection.Input;
stu_addr.Direction = ParameterDirection.Input;
class_id.Direction = ParameterDirection.Input;
remark.Direction = ParameterDirection.Input;

//参数赋值
stu_id.Value = myDrv[0].ToString().Trim();
stu_name.Value = myDrv[1].ToString().Trim();
stu_sex.Value = myDrv[2].ToString().Trim();
polity.Value = myDrv[3].ToString().Trim();
//paraYYYYMM.Value = ddlYear.Items[ddlYear.SelectedIndex].Value + ddlMonth.Items[ddlMonth.SelectedIndex].Value;
stu_birth.Value = myDrv[4].ToString().Trim();
stu_addr.Value = myDrv[5].ToString().Trim();
class_id.Value = DropDownList5.SelectedValue;
remark.Value = myDrv[6].ToString().Trim();
conn.Open();
cmd.ExecuteNonQuery();//写入SQL数据库
cmd2.ExecuteNonQuery();

Page.Response.Write("<script>alert('导入成功')</script>");

catch (SqlException ex)

Page.Response.Write(ex.ToString());
//Page.Response.Write(ex.ToString());//调试用
Page.Response.Write("<script>alert('添加第" + count.ToString() + "个学生时数据出错,请检查学号!')</script>");
objConn.Close();//关闭EXCEL的连接

finally

objConn.Close();//关闭EXCEL的连接
conn.Close();


参考技术A 右击<表>--<所有任务>--<导入数据>--<下一步>
然后选择数据源为excel格式的,接着按向导做就可以了。
参考技术B SQL Server 2005 有导入导出功能,很简单。只要按向导作就行 参考技术C 没什么难度

以上是关于excel表导入Sqlserver2005中!!的主要内容,如果未能解决你的问题,请参考以下文章

如何将Oracle中的数据导入到SQL Server中来

从 Excel 导入 SQL Server 2005 时出现截断错误

将 Excel 电子表格列导入 SQL Server 数据库

如何把SQLServer表数据导出为Excel文件

如何把SQLServer表数据导出为Excel文件

怎样把CSV格式的文件导入到SQL 2005?