最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的方法

 更新時間:2015年01月04日 09:23:47   投稿:shichen2014  
這篇文章主要介紹了asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的方法,實現(xiàn)讀取excel數(shù)據(jù)并導(dǎo)入到SQL Server數(shù)據(jù)庫的功能,是非常實用的技巧,需要的朋友可以參考下

本文實例講述了asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的方法。分享給大家供大家參考。具體分析如下:

excel是辦公中非常常用的一個辦公表格了,但我們在開發(fā)中通常會需要直接把excel數(shù)據(jù)快速導(dǎo)入到數(shù)據(jù)庫中了,這里整理了一個asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的例子供各位參考學(xué)習(xí)。

注意:EXCEL中的第一行不能導(dǎo)入。
下面是源碼:IntoExcel.aspx:

復(fù)制代碼 代碼如下:
<%@ Page  AutoEventWireup="true" CodeFile="IntoExcel.aspx.cs" Inherits="study_IntoExcel" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head id="Head1" runat="server"> 
<title>無標(biāo)題頁</title> 
<script language="javascript" type="text/javascript"><!-- 
// <!CDATA[ 
function check() { 
var k=//S+/.[xls]/; 
if(!k.test(document.getElementById("fileId").value)) 

    alert("只能上次xls格式的文件"); 
    return false; 

return true; 

// --></script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <p> 
        <asp:FileUpload ID="fileId" runat="server" /> 
        <asp:Button ID="Button1" runat="server" Text="上傳" OnClientClick="return check()" onclick="Button1_Click" /></p> 
    </div> 
    </form> 
</body> 
</html>

IntoExcel.aspx.cs
復(fù)制代碼 代碼如下:
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Web.Security; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls.WebParts; 
using System.IO; 
using System.Data.OleDb; 
using System.Data.SqlClient; 
using System.Web.UI.WebControls; 
 
public partial class study_IntoExcel : System.Web.UI.Page 

    protected void Page_Load(object sender, EventArgs e) 
    { 
 
    } 
        /// <summary> 
        /// 上傳文件 
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        protected void Button1_Click(object sender, EventArgs e) 
        { 
            string fileName = fileId.FileName; 
            string savePath = Server.MapPath("~/file/"); 
            FileOperatpr(fileName, savePath); 
            fileId.SaveAs(savePath + fileName); 
            DataOperator(fileName, savePath); 
        } 
        /// <summary> 
        /// 數(shù)據(jù)操作 
        /// </summary> 
        /// <param name="fileName"></param> 
        /// <param name="savePath"></param> 
        private void DataOperator(string fileName, string savePath) 
        { 
            string myString = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =  " + savePath + fileName + ";Extended Properties=Excel 8.0"; 
            OleDbConnection oconn = new OleDbConnection(myString); 
            oconn.Open(); 
            DataSet ds = new DataSet(); 
            OleDbDataAdapter oda = new OleDbDataAdapter("select * from [Sheet1$]", oconn); 
            oda.Fill(ds); 
            oconn.Close(); 
            DataSetOperator(ds,savePath+fileName); 
        } 
        /// <summary> 
        /// 數(shù)據(jù)集操作 
        /// </summary> 
        /// <param name="ds"></param> 
        private void DataSetOperator(DataSet ds,string filePath) 
        { 
            SqlConnection conn = new SqlConnection("Data Source=SONYSVR;Initial Catalog=IAR_Factory_811;User ID=sa;Password=P@ssword"); 
            conn.Open(); 
            SqlTransaction str = conn.BeginTransaction();//利用事務(wù)處理 防止中斷 
            int k = 0; 
            if (ds.Tables[0].Rows.Count < 1) 
            { 
                Response.Write("<script>alert('沒有數(shù)據(jù)!')</script>"); 
                return; 
            } 
            try 
            { 
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
                { 
                    string <strong><a href="http://m.fzitv.net" title="sql" target="_blank">sql</a></strong>Str = "insert into IntoExcel(Tname,Tage,Taddress)values"; 
                    sqlStr +="('"+ ds.Tables[0].Rows[i][0].ToString()+"',"; 
                    sqlStr += ds.Tables[0].Rows[i][1].ToString()+","; 
                    sqlStr +="'" +ds.Tables[0].Rows[i][2].ToString()+"')"; 
                    SqlCommand cmd = new SqlCommand(sqlStr, conn, str); 
                    cmd.Transaction = str; 
                    k += cmd.ExecuteNonQuery(); 
                } 
                str.Commit(); 
            } 
            catch (Exception ex) 
            { 
                Response.Write("發(fā)生異常,數(shù)據(jù)已回滾/n信息/n" + ex.Message); 
                str.Rollback(); 
            } 
            finally 
            { 
                Response.Write("上傳成功" + k + "條"); 
                File.Delete(filePath); 
            } 
        } 
        /// <summary> 
        /// 文件操作 
        /// </summary> 
        /// <param name="fileName"></param> 
        /// <param name="savePath"></param> 
        private void FileOperatpr(string fileName, string savePath) 
        { 
            if (!Directory.Exists(savePath)) 
            { 
                Directory.CreateDirectory(savePath); 
            } 
            if (File.Exists(savePath + fileName)) 
            { 
                File.Delete(savePath + fileName); 
            } 
        } 

 
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + savePath + ";Extended Properties='Excel 8.0;HDR=YES
 Provider=Microsoft.Jet.OLEDB.4.0;;//連接驅(qū)動
Data Source=" + savePath + "; // 數(shù)據(jù)庫地址
Extended Properties='Excel 8.0; // 連接的是Excel8.0
HDR=YES;// 有兩個值:YES/ NO, 這2個值,說了你是否能直接讀列名,NO,只可以讀下標(biāo)
IMEX=1;//解決數(shù)字與字符混合時,識別不正常的情況.

這個讀入數(shù)據(jù)庫的方式不是最佳的,應(yīng)該用office組件
select * from [Sheet1$] //引用EXCLE文件中sheet1工作表的內(nèi)容
OleDB控件用的是OleDb的驅(qū)動程序,可以訪問各種數(shù)據(jù)庫  
 
數(shù)據(jù)庫中的字段:
復(fù)制代碼 代碼如下:
create table IntoExcel 

    Tid int identity(1,1) primary key, 
    Tname varchar(50), 
    Tage int, 
    Taddress varchar(200), 
     
)

SQL控件用的是專用的驅(qū)動程序,能高效的訪問SQL Server數(shù)據(jù)庫
SQLConnection只能訪問SQL Server,而OleDbConnection則可以訪問所有數(shù)據(jù)庫。  
如果只是訪問SQL Server的話,SQL比OleDb更快。

希望本文所述對大家的asp.net程序設(shè)計有所幫助。

相關(guān)文章

最新評論

若羌县| 麻城市| 青冈县| 株洲市| 柯坪县| 蓬莱市| 环江| 屏山县| 肥西县| 南和县| 抚州市| 德兴市| 嘉峪关市| 宜君县| 象山县| 郯城县| 临泽县| 专栏| 聊城市| 汤阴县| 惠州市| 平邑县| 兴化市| 梓潼县| 潢川县| 新丰县| 海门市| 陈巴尔虎旗| 泊头市| 五家渠市| 陈巴尔虎旗| 仁怀市| 韶关市| 乌拉特后旗| 咸宁市| 东乌| 汉寿县| 芮城县| 漯河市| 屏山县| 哈巴河县|