惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Last Watchdog
The Last Watchdog
博客园_首页
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
美团技术团队
小众软件
小众软件
V
V2EX
博客园 - Franky
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
Attack and Defense Labs
Attack and Defense Labs
S
Security Affairs
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
T
The Exploit Database - CXSecurity.com
有赞技术团队
有赞技术团队
S
Schneier on Security
人人都是产品经理
人人都是产品经理
Security Archives - TechRepublic
Security Archives - TechRepublic
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
K
Kaspersky official blog
PCI Perspectives
PCI Perspectives
AI
AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
罗磊的独立博客
O
OpenAI News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Register - Security
The Register - Security
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
博客园 - 【当耐特】
C
Cisco Blogs
大猫的无限游戏
大猫的无限游戏
Help Net Security
Help Net Security
Google DeepMind News
Google DeepMind News
S
Securelist
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Proofpoint News Feed
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
L
LangChain Blog
SecWiki News
SecWiki News
博客园 - 叶小钗
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V2EX - 技术
V2EX - 技术
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
L
LINUX DO - 热门话题
Cisco Talos Blog
Cisco Talos Blog

博客园 - 寒天飞雪

转载: 房贷的两种还款方式介绍 RFC访问SAP(C#) VB.net连接SAP实例(vb.net写法) VB.net连接SAP实例 - 寒天飞雪 - 博客园 Winform 树型菜单例子 - 寒天飞雪 - 博客园 介绍: MRP和MPS 收藏: SQLServer的存储结构 转载: 正则表达式介绍 .net 连接ORACLE 数据库的例子 - 寒天飞雪 转载: ReportViewer : RDLC自定义工具栏 C#调用iTextSharp组件生成PDF文件, 在VS2005下已经调试通过! SQLSERVER 2005 BI的帮助文档说明: 9.2 定义和浏览翻译 9.1 定义和浏览透视 8.1 定义操作 7.1 定义关键指标KPI 6.3使用脚本命令定义作用域分配 6.2 定义命名集 6.1 定义计算成员
项目中用到Excel上传到Sql数据库
寒天飞雪 · 2010-10-12 · via 博客园 - 寒天飞雪

      项目中经常会用到Excel上传到数据库中的功能或者读取一个Excel在GridView中显示出来的情况。

基本原理就是获取Excel的连接,读取Excel的数据到DataSet。 根据DataSet的结果来显示内容或者把DataSet的结果更新到数据库中。

      以下就贴出代码并加上简单的注释。

Imports System.Data
Imports System.Data.SqlClient '连接SQLSERVER数据库
Imports System.Data.OleDb     '连接Excel工作表

Partial Class Default_Excel
    Inherits System.Web.UI.Page

    Protected Sub btnImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImport.Click
        Dim fup As FileUpload
        Dim strPath As String = ""
        Dim i As Integer = 0
        Dim dataTable As New DataTable

        '获取上传控件

        fup = Me.form1.FindControl("FileUpload1")
        strPath = fup.PostedFile.FileName

        If strPath = "" Then
            Response.Write(" <script> alert( '请先选择正确的Excel文件导入! ') </script> ")
            Response.End()
        End If

        '定义Excel连接字符串
        Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & "; Extended Properties=Excel 8.0;"
        Try
            Dim oleDbConnection As OleDbConnection = New OleDbConnection(sConnectionString)
            oleDbConnection.Open()
            '获取excel表
            dataTable = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)

            '获取sheet名,其中(0)(1)...(N): 按名称排列的表单元素
            '固定是读取一个工作表

            Dim tableName As String = dataTable.Rows(0)(2).ToString().Trim()
            tableName = "[" & tableName.Replace(" ' ", " ") & "]"

    '以下SELECT 中的字段名要和Excel工作表中的一致

            Dim query As String = "SELECT 字段名1,字段名2, 字段名3,字段名4,字段名5 FROM " & tableName
            Dim dataset As DataSet = New DataSet()
            Dim oleAdapter As OleDbDataAdapter = New OleDbDataAdapter(query, sConnectionString)
            oleAdapter.Fill(dataset, "Rwb")

    '连接SQLSERVER数据库

            Dim sqlcon As New SqlConnection(SqlOpe.ConnStr)
            sqlcon.Open()
            '从excel文件获得数据后,插入记录到SQL Server的数据表
            Dim dataTable1 As DataTable = New DataTable()

            '以下字段名是数据库中 表中的字段名称
            Dim sqlDA1 As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter("SELECT YEARS, PERIOD, WERKS, MATNR, BISMT,VERSION , REQQTY  FROM ProdS_ForeCast WHERE 1<>1 ", sqlcon)
            Dim sqlCB1 As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(sqlDA1)
            sqlDA1.Fill(dataTable1)

            Dim dataRow11 As DataRow
            For Each dataRow11 In dataset.Tables("Rwb").Rows
                'sql里数据dataRow1
                Dim dataRow1 As DataRow = dataTable1.NewRow()
                dataRow1("YEARS") = dataRow11("字段名1")
                dataRow1("PERIOD") = dataRow11("字段名2")
                dataRow1("WERKS") = dataRow11("字段名3")
                dataRow1("MATNR") = dataRow11("字段名4")
                dataRow1("BISMT") = dataRow11("字段名5")
                dataTable1.Rows.Add(dataRow1)
            Next
            Response.Write(" <script> alert( '一共导入 " & dataset.Tables("Rwb").Rows.Count.ToString() & " 条记录  ') </script> ")
            sqlDA1.Update(dataTable1)
            oleDbConnection.Close()
        Catch ex As Exception
            Console.WriteLine(ex.ToString())
        End Try
    End Sub

End Class

如果需要在GridView中显示将DataSet的结果绑定到GridView的DataSource就可以了