All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
网页技巧:通过web.config设置数据库连接串_.net资料_编程技术
.NET2.0在Repeater中实现删除_[Asp.Net教程]
Sql,Server2005实现远程备份数据库_mssql学习_编程技术
不用GD库生成当前时间的PNG格式图象的程序_[PHP教程]
[delphi语法7]delphi中While语句的使用实例
远程连接sql,server,2000服务器的解决方案_[SQL,Server教程]
Sql,server内存不断增加的问题分析_[SQL,Server教程]
修改SQL Server2005的sa用户密码_[SQL Server教程]
使用Forms,Authentication实现用户注册、登录,(二)用户注册与登录_[Asp.Net教程]
Asp.net连接数据实例(ACCESS版)_[Asp.Net教程]
Reading number is top 10 pictures
NeedWallpaper6
刘亦菲写真集1
影评-疯子,我爱你
职场回春术
看到这名字我也是醉了。。。。。。
The most popular girls welcome eggplant
男人,就要活出棱角
Discharge accidentally Actresses by the breast1
夕阳下的北京街道
徐若瑄展示美丽胸围2
Download software ranking
Unix video tutorial6
Boxer's Top ten classic battle4
C++教程第四版
linux安装大全
传奇私服架设教程-chm
The Bermuda triangle1
Eclipse 4.2.2 For Win64
电脑知识及技巧大合集
Sora aoi 120 minutes
Proficient in JavaScript
归海一刀 published in(发表于) 2014/1/30 1:57:13 Edit(编辑)
用asp.net把Excel转换为SQL,Server_[Asp.Net教程]

用asp.net把Excel转换为SQL,Server_[Asp.Net教程]

用asp.net把Excel转换为SQL Server_[Asp.Net教程]
办公软件Excel是一种常用的电子表格软件,在编程项目中有需要将Excel转换为SQL Server数据库的需求,本文对此进行一些介绍并给出设计代码。
1.功能分析
通过Microsoft.Jet.OLEDB.4.0方式可实现使用ADO.NET访问Excel的目的,如以下示例代码为连接Excel数据的字符串:

string strOdbcCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=D:\2010年图书销售情况.xls;Extended Properties=Excel 8.0";

2.实施方法
程序开发步骤:
(1)新建一个网站,命名为25,其主页默认为Default.aspx。
(2)Default.aspx页面中添加一个Table表格,用来布局页面,然后在该Table表格中添加一个iframe框架、两个Button控件和一个GridView控件,其中,iframe框架用来显示原始Excel数据表中的数据;Button控件分别用来将指定Excel中的数据表导入到SQL Server数据库中和将导入SQL Server数据库中的Excel数据绑定到GridView控件上;GridView控件用来显示导入SQL Server数据库中的Excel数据。
(3)程序主要代码如下。
Default.aspx页面中,首先自定义一个LoadData方法,该方法为无返回值类型方法,主要用来将Excel数据表中的数据导入到SQL Server数据库中。LoadData方法实现代码如下:

public void LoadData(string StyleSheet)
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + Server.MapPath
("usersdb.xls") + ";Extended Properties=Excel 8.0";
OleDbConnection myConn = new OleDbConnection(strCon);
myConn.Open(); //打开数据链接,得到一个数据集
DataSet myDataSet = new DataSet(); //创建DataSet对象
string StrSql = "select * from [" + StyleSheet + "$]";
OleDbDataAdapter myCommand = new OleDbDataAdapter(StrSql, myConn);
myCommand.Fill(myDataSet, "[" + StyleSheet + "$]");
myCommand.Dispose();
DataTable DT = myDataSet.Tables["[" + StyleSheet + "$]"];
myConn.Close();
myCommand.Dispose();
string strConn = "Data Source=(local);DataBase=Usersdb;Uid=sa;Pwd=";
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
for (int j = 0; j < DT.Rows.Count; j++)
{
string UserID = DT.Rows[j][0].ToString();
string EmailAddress = DT.Rows[j][1].ToString();
string FirstName = DT.Rows[j][2].ToString();
string LastName = DT.Rows[j][3].ToString();
string Address1 = DT.Rows[j][4].ToString();
string Address2 = DT.Rows[j][5].ToString();
string City = DT.Rows[j][6].ToString();
string strSql = "insert into Usersdb(EmailAddress,FirstName,
LastName,Address1,Address2,City) ";
strSql = strSql + "values(’" + EmailAddress + "’,’" + FirstName + "’,
’" + LastName + "’,’" + Address1 + "’,’" + Address2 + "’,’" + City + "’)";
SqlCommand comm = new SqlCommand(strSql, conn);
comm.ExecuteNonQuery();
if (j == DT.Rows.Count - 1)
{
Label1.Visible = true;
}
else
{
Label1.Visible = false;
}
}
conn.Close();
}

单击【Excel数据写入数据库中】按钮,定义一个string类型的变量,用来为LoadData传入参数,然后调用LoadData自定义方法将指定的Excel中的数据表导入到SQL Server数据库中。【Excel数据写入数据库中】按钮的Click事件代码如下:


protected void Button1_Click(object sender, EventArgs e)
{
string StyleSheet = "Sheet1";
LoadData(StyleSheet);
}

单击【显示导入SQL的Excel数据】按钮,将导入SQL Server数据库中的Excel数据绑定到GridView控件上,显示在网页中。【显示导入SQL的Excel数据】按钮的Click事件代码如下:


protected void Button2_Click(object sender, EventArgs e)
{
string strConn = "Data Source=(local);DataBase=Usersdb;Uid=sa;Pwd=";
string sqlstr="select * from Usersdb";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr,conn);
DataSet ds = new DataSet();
conn.Open();
myda.Fill(ds, "Usersdb");
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}

说明:程序中进行与Excel和SQL Server数据库相关的操作时,首先需要分别添加System.Data.OleDb和System.Data.SqlClient命名空间。
3.补充说明
除了可以将Excel中数据导入到SQL Server数据库外,还可以将其转换为.txt文本文件格式,或者导入到AccessOracle等数据库中。


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.