亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么將Excel中數據導入到Access數據庫

發布時間:2021-01-13 16:31:54 來源:億速云 閱讀:235 作者:Leah 欄目:開發技術

今天就跟大家聊聊有關怎么將Excel中數據導入到Access數據庫,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

Default.aspx

復制代碼 代碼如下:


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "-transitional.dtd">

<html xmlns="">
<head id="Head1" runat="server">
    <title>無標題頁</title>
    <style type="text/css">

        .style1
        {
            height: 16px;
        }
        .style3
        {
            height: 23px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">

    <div>

    </div>
    <table align="center" border="1" bordercolor="honeydew" cellpadding="0"
        cellspacing="0">
        <tr>
            <td  
                class="style1">
            </td>
            <td colspan="2"
                >
                將Excel數據寫入Access數據庫中</td>
        </tr>
        <tr>
            <td >
            </td>
            <td >
                <iframe id="I1" name="I1" scrolling="yes" src="學生成績.xls"
                    ></iframe>
            </td>
            <td >
                <asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Size="9pt"
                    ForeColor="#333333" GridLines="None" Width="228px">
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView>
            </td>
        </tr>
        <tr>
            <td  class="style3">
            </td>
            <td  
                valign="top">
                <asp:Button ID="Button3" runat="server" Font-Size="9pt" onclick="Button1_Click"
                    Text="Excel數據寫入Access數據庫中" />
    <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"
                    ></asp:Label>
            </td>
            <td >
                <asp:Button ID="Button2" runat="server" Font-Size="9pt" onclick="Button2_Click"
                    Text="數據庫中顯示Excel數據" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    </form>
</body>
</html>

Default.aspx.cs

復制代碼 代碼如下:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public OleDbConnection CreateCon()
    {
        string strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath("UserScore.mdb") + ";User Id=admin;Password=;";
        OleDbConnection odbc = new OleDbConnection(strconn);
        return odbc;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //定義Excel列表
        string StyleSheet = "Sheet1";
        //調用自定義LoadData方法,將Excel文件中數據讀到ASPNET頁面中
        LoadData(StyleSheet);
        //定義查詢的SQL語句
        string sql = "select ID,用戶姓名,試卷,成績,考試時間 from Score";
        //創建Oledb數據庫連接
        OleDbConnection con = CreateCon();
        con.Open();//打開數據庫連接
        OleDbCommand com = new OleDbCommand(sql, con);
        //開始事務
        OleDbTransaction tran = con.BeginTransaction();
        com.Transaction = tran;
        //創建適配器
        OleDbDataAdapter da = new OleDbDataAdapter(com);
        OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
        //創建DataSet數據集
        DataSet ds = new DataSet();
        //填充數據集
        da.Fill(ds);
        int curIndex = 0;
        if (ds.Tables[0].Rows.Count > 0)
        {
            curIndex = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
        }
        //創建一個內存表
        DataTable tb = this.getExcelDate();
        string selsql = "";
        for (int i = 0; i < tb.Rows.Count; i++)
        {
            string UserName = tb.Rows[i][0].ToString();
            selsql = "select count(*) from Score where 用戶姓名='" + UserName + "'";
        }
        //判斷Excel文件中是否已經導入到Access數據庫中
        if (ExScalar(selsql) > 0)
        {
            Label1.Visible = true;
            Label1.Text = "<script language=javascript>alert('該Excle中的數據已經導入數據庫中!');location='Default.aspx';</script>";
        }
        else
        {
            //循環讀取Excel文件中數據,并添加到Access事先創建好的數據庫表中
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = ds.Tables[0].NewRow();
                dr[0] = ++curIndex;
                dr[1] = tb.Rows[i][0];
                dr[2] = tb.Rows[i][1];
                dr[3] = tb.Rows[i][2];
                dr[4] = tb.Rows[i][3];
                ds.Tables[0].Rows.Add(dr);
            }
            try
            {
                da.Update(ds);//執行插入操作
                tran.Commit();//事務提交
                Label1.Visible = true;
                Label1.Text = "<script language=javascript>alert('數據導入成功!');location='Default.aspx';</script>";
            }
            catch
            {
                tran.Rollback();//事務回滾
                Label1.Visible = true;
                Label1.Text = "<script language=javascript>alert('數據導入失敗!');location='Default.aspx';</script>";
            }
            finally
            {
                con.Close();//關閉數據庫連接
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string sqlstr = "select * from Score";
        OleDbConnection conn = CreateCon();
        conn.Open();
        OleDbCommand mycom = new OleDbCommand(sqlstr, conn);
        OleDbDataReader dr = mycom.ExecuteReader();
        dr.Read();
        if (dr.HasRows)
        {
            GetDataSet(sqlstr);
        }
        else
        {
            Label1.Visible = true;
            Label1.Text = "<script language=javascript>alert('數據庫中沒有數據信息,請先導入再查詢!');location='Default.aspx';</script>";
        }
        dr.Close();
        conn.Close();
    }
    public DataSet GetDataSet(string sqlstr)
    {
        OleDbConnection conn = CreateCon();
        OleDbDataAdapter myda = new OleDbDataAdapter(sqlstr, conn);
        DataSet ds = new DataSet();
        myda.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        return ds;
    }
    public DataTable getExcelDate()
    {
        string strExcelFileName = Server.MapPath("學生成績.xls");
        string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";
        string sql = "select * from [Sheet1$]";
        OleDbDataAdapter da = new OleDbDataAdapter(sql, strcon);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds.Tables[0];
    }
    public void LoadData(string StyleSheet)
    {
        //定義數據庫連接字符串 m
        string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + Server.MapPath("學生成績.xls") + ";Extended Properties=Excel 8.0";
        //創建數據庫連接
        OleDbConnection myConn = new OleDbConnection(strCon);
        //打開數據鏈接,得到一個數據集
        myConn.Open();
        //創建DataSet對象  
        DataSet myDataSet = new DataSet();
        //定義查詢的SQL語句
        string StrSql = "select   *   from   [" + StyleSheet + "$]";
        //創建數據庫適配器
        OleDbDataAdapter myCommand = new OleDbDataAdapter(StrSql, myConn);
        //填充數據集中的數據
        myCommand.Fill(myDataSet, "[" + StyleSheet + "$]");
        //釋放占有的資源
        myCommand.Dispose();
        //關閉數據庫連接
        myConn.Close();
    }
    public int ExScalar(string sql)
    {
        OleDbConnection conn = CreateCon();
        conn.Open();
        OleDbCommand com = new OleDbCommand(sql, conn);
        return Convert.ToInt32(com.ExecuteScalar());
        conn.Close();
    }
}

看完上述內容,你們對怎么將Excel中數據導入到Access數據庫有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

庐江县| 九龙县| 聊城市| 始兴县| 耒阳市| 深州市| 江川县| 仙桃市| 锦屏县| 安西县| 法库县| 诸城市| 锡林浩特市| 南靖县| 临泉县| 宁远县| 灵宝市| 信宜市| 西峡县| 兴宁市| 平邑县| 临江市| 昌平区| 乌兰浩特市| 即墨市| 无为县| 乐亭县| 搜索| 六盘水市| 平塘县| 敦化市| 平利县| 平武县| 凌源市| 玛曲县| 永新县| 清涧县| 绥芬河市| 河北区| 田东县| 准格尔旗|