【问题标题】:Insert data from uploaded Excel document to database using ASP.net (Vb.net)使用 ASP.net (Vb.net) 将上传的 Excel 文档中的数据插入数据库
【发布时间】:2013-09-11 03:14:00
【问题描述】:

我正在开发一个网络应用程序,我想知道是否有可能在上传 Excel 文件后将其数据字段信息插入到我的 SqlServer 数据库中,使用 Asp.net(VB.net) ??

谢谢

【问题讨论】:

    标签: asp.net sql-server vb.net file-upload


    【解决方案1】:

    在 VB.NET 中试试这个代码:

    Protected Sub btnExport(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim TheFile As FileInfo = New FileInfo(MapPath(".") & "\" & "filename.xls")
    
        ' Connection String to Excel Workbook 2010 (xlsx)
        ' Dim excelConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~\directory\filename.xlsx") + ";Extended Properties=""Excel 12.0 Xml;HDR=YES;"""
    
        ' Connection String to Excel Workbook 2003 (xls)
        Dim excelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~\directory\filename.xls") + ";Extended Properties=""Excel 8.0;HDR=YES;"""
    
        ' Create Connection to Excel Workbook
        Using connection As New OleDbConnection(excelConnectionString)
            Dim command As New OleDbCommand("Select * FROM [sheetname$] ", connection)
    
            connection.Open()
    
            ' Create DbDataReader to Data Worksheet
            Using dr As DbDataReader = command.ExecuteReader()
    
                ' SQL Server Connection String
                Const sqlConnectionString As String = "Data Source=server; Initial Catalog=database; Persist Security Info=True;User ID=userid;Password=password"
    
                ' Bulk Copy to SQL Server
                Using bulkCopy As New SqlBulkCopy(sqlConnectionString)
                    bulkCopy.DestinationTableName = "SqlServerTableName"
                    bulkCopy.WriteToServer(dr)
                End Using
            End Using
        End Using
    End Sub
    

    附加提示:尝试将 IIS 设置为运行 32 位应用程序。

    【讨论】:

      【解决方案2】:

      是的,这是可能的。以下是可能的基本步骤的鸟瞰图:

      1. 上传后 - 以临时唯一名称保存 Excel 文件
      2. 打开与上传文件的 OLEDB 连接
      3. 读取它(通过 OleDbDataReader - 如果它是一个巨大的文件,那么您不必立即将它加载到内存中,或者如果它很小则加载到 DataTable 中)
      4. 运行查询、存储过程以使用获取的数据更新 SqlServer DB
      5. 删除上传的文件

      【讨论】:

      • 这是一个很常见的代码,你可以在网上找到很多例子。例如 Google“Path.GetRandomFileName”或“VB.NET 读取 Excel 文件 OLEDB”或“DataTable TVP”
      【解决方案3】:

      试试这个

      public void export(string excelfilepath)
      {
      
          string ssqltable = "tdatamigrationtable";
      
          string myexceldataquery = "select student,rollno,course from [sheet1$]";
          try
          {
              //create our connection strings
              string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;data source=" + excelfilepath +
              ";extended properties=" + "\"excel 8.0;hdr=yes;\"";
              string ssqlconnectionstring = "server=mydatabaseservername;user
              id=dbuserid;password=dbuserpassword;database=databasename;connection reset=false";
      
              //series of commands to bulk copy data from the excel file into our sql table
              oledbconnection oledbconn = new oledbconnection(sexcelconnectionstring);
              oledbcommand oledbcmd = new oledbcommand(myexceldataquery, oledbconn);
              oledbconn.open();
                  oledbdatareader dr = oledbcmd.executereader();
                  sqlbulkcopy bulkcopy = new sqlbulkcopy(ssqlconnectionstring);
                  bulkcopy.destinationtablename = ssqltable;
                  while (dr.read())
                  {
                      bulkcopy.writetoserver(dr);
                  }
      
                  oledbconn.close();
              }
              catch (exception ex)
              {
                  //handle exception
              }
          }
      

      【讨论】:

        【解决方案4】:
        Imports System.Data.Common
        Imports System.Data.SqlClient
        
        Public Class Form1
        
            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                Dim fname As String
                Using ofd As New OpenFileDialog
                    If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
                        fname = ofd.FileName
        
                    End If
                End Using
        
                Dim olecon As String = "Provider=Microsoft.ACE.OLEDB.12.0 ;Data Source=" & fname & ";Extended Properties=""Excel 12.0;IMEX=1;HDR=YES;"""
        
                Dim dBaseConnection As New System.Data.OleDb.OleDbConnection(olecon)
        
                dBaseConnection.Open()
        
        
                SSQL = "select  [LOT],[IMAGE],[STYLENO],[VENDOR] from [Sheet1$]"
        
                Dim cmd As New OleDbCommand(SSQL, dBaseConnection)
        
                Dim da As New OleDbDataAdapter(cmd)
        
                Dim ds As New DataSet
        
                da.Fill(ds)
        
        
                Using dr As DbDataReader = cmd.ExecuteReader
        
                    If SHCONNECTION.State = ConnectionState.Closed Then
                        Call SHconn(MCONNECTIONSTRING)
                    End If
        
                    Using bulkCopy As New SqlBulkCopy(MCONNECTIONSTRING)
                        bulkCopy.DestinationTableName = "DBimage"
                        bulkCopy.WriteToServer(ds)
                    End Using
                End Using
            End Sub
        End Class
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多