1

我目前正在构建一个项目,允许用户通过 Web 界面(内置)导入 Excel 文件,将文件保存到服务器(内置),然后根据一些用户选项将数据导入服务器上的 SQL 数据库(未建成)。

我根本不熟悉 VS 中的 SQL 数据库工具,所以我在两天的大部分时间里一直在摸索,只是想把所有东西都设置好。我很确定我需要使用 BulkCopy,但我不太确定如何使用它,而且我似乎找不到具体的例子来解释它与我的特定应用程序有关。

所以在我的 App_Data 文件夹中,我有一个 .mdf 标题“设备数据库”。在那个数据库中,我有三个表:“Galaxy Nexus”、“Hercules”和“Ruby”

我正在尝试将每个导入的 excel 表中的四个单元格导入到各自的表中。

我想将单元格(2,2)导入表格中的column1,将cell(2,3)导入column2,将cell(3,2)导入column3,将cell(1,1)导入column4。

我试图完成此操作的代码是:

    Dim ExcelContentType As String = "application/vnd.ms-excel"
    Dim Excel2010ContentType As String = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    Dim excelConnectionString As String = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", SavedFile)
    Using connection As New OleDbConnection(excelConnectionString)
        Dim Command As OleDbCommand = New OleDbCommand("Select * FROM [Sheet1$]", connection)
        connection.Open()
        Using reader As DbDataReader = Command.ExecuteReader()
            Dim sqlConnectionString As String = "Data Source=.\sqlexpress;Initial Catalog=ExcelDB;Integrated Security=True"
            Using bulkCopy As New SqlBulkCopy(sqlConnectionString)
                bulkCopy.DestinationTableName = DropDown1.SelectedItem.ToString
                bulkCopy.WriteToServer(reader)
            End Using
        End Using
    End Using

我遇到的问题是,我不知道如何从 Excel 表中选择某些单元格进行导入,也不知道如何将这些单元格复制到指定表中的特定列。

任何和所有的帮助总是值得赞赏的。谢谢,扎克

4

4 回答 4

3

我发布了一个答案,以便如果其他人偶然发现这一点,他们也可能会得到帮助。

这就是让一切为我工作的原因。(向凯文大喊)

Protected Sub Button1_Click(sender As Object, e As System.EventArgs)
    Dim appPath As String = Request.PhysicalApplicationPath
        Dim con As New System.Data.SqlClient.SqlConnection
        con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & appPath & "App_Data\Devicedatabase.MDF;Integrated Security=True;User Instance=True;"
        con.Open()
        MsgBox("open")
        con.Close()
        MsgBox("close")
    End Sub

经过多次尝试和挫折,这使连接打开。

这得到了导入数据库的 excel 值:

 Using con As New SqlClient.SqlConnection With
{
    .ConnectionString =
    "Data Source=.\SQLEXPRESS;AttachDbFilename=" & appPath & "App_Data\Devicedatabase.MDF;Integrated Security=True;User Instance=True;"
}
Using cmd As New SqlClient.SqlCommand With
    {
        .Connection = con,
        .CommandText = "INSERT INTO " & """" & DropDownList1.SelectedItem.ToString & """" & "ColumnName1, ColumnName2)VALUES (@Col1,@Col2)"
    }
    cmd.Parameters.Add(New SqlClient.SqlParameter With {.DbType = DbType.String, .ParameterName = "@Col1"})
    cmd.Parameters.Add(New SqlClient.SqlParameter With {.DbType = DbType.String, .ParameterName = "@Col2"})
    cmd.Parameters(0).Value = "Value obtained from Excel"
    cmd.Parameters(1).Value = "Value obtained from Excel"
    con.Open()
    Dim Result As Integer = cmd.ExecuteNonQuery
    If Result <> 1 Then
        MessageBox.Show("Insert failed.")
    Else
        MessageBox.Show("Row inserted.")
    End If

End Using
End Using

尽情享受吧!

于 2012-09-18T23:01:02.060 回答
1

为此,请使用Excel 数据阅读器dll。它将读取 excel 文件并给出数据集作为结果。

于 2012-09-12T09:15:35.323 回答
0

'abcConnectionString' 作为连接字符串和 'pqr_table' 作为 sql 表的简单代码。'Sheet1' 用于 Excel 文件的 sheet1。重要的是表格格式,即excel n数据库文件的行n列应该相同

这是 vb.net 代码敌人 veb 应用程序一个 FileUpload,名称为“FileUpload1”和一个按钮。此代码在按钮方法中

Dim excelConnectionString As String = String.Empty     
Dim uploadPath As String = "~/Uploads/" 
Dim filePath As String = Server.MapPath(uploadPath + FileUpload1.PostedFile.FileName)

Dim fileExt As String = Path.GetExtension(FileUpload1.PostedFile.FileName)

Dim strConnection As [String] = ConfigurationManager.ConnectionStrings("abcConnectionString").ConnectionString

If fileExt = ".xls" OrElse fileExt = "XLS" Then
    excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source='" & filePath & "'" & "; Extended Properties ='Excel 8.0;HDR=Yes'"
ElseIf fileExt = ".xlsx" OrElse fileExt = "XLSX" Then
    excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filePath & ";Extended Properties=Excel 12.0;Persist Security Info=False"
End If
Dim excelConnection As New OleDbConnection(excelConnectionString)
Dim cmd As New OleDbCommand("Select * from [Sheet1$]", excelConnection)
excelConnection.Open()
Dim dReader As OleDbDataReader
dReader = cmd.ExecuteReader()
Dim sqlBulk As New SqlBulkCopy(strConnection)
sqlBulk.DestinationTableName = "pqr_table"
sqlBulk.WriteToServer(dReader)
MsgBox("Congratulations! Successfully Imported.")
excelConnection.Close()
于 2013-08-09T11:22:15.277 回答
0

'abcConnectionString' 作为连接字符串和 'pqr_table' 作为 sql 表的简单代码。'Sheet1' 用于 Excel 文件的 sheet1。重要的是表格格式,即excel n数据库文件的行n列应该相同

她是 vb.net 代码敌人 veb 应用程序,一个名为“FileUpload1”的 FileUpload 和一个按钮。此代码在按钮方法中

Dim excelConnectionString As String = String.Empty
Dim uploadPath As String = "~/Uploads/"
Dim filePath As String = Server.MapPath(uploadPath + FileUpload1.PostedFile.FileName)
Dim fileExt As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
Dim strConnection As [String] = ConfigurationManager.ConnectionStrings("abcConnectionString").ConnectionString
If fileExt = ".xls" OrElse fileExt = "XLS" Then
            excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source='" & filePath & "'" & "; Extended Properties ='Excel 8.0;HDR=Yes'"
ElseIf fileExt = ".xlsx" OrElse fileExt = "XLSX" Then
            excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filePath & ";Extended Properties=Excel 12.0;Persist Security Info=False"
End If
Dim excelConnection As New OleDbConnection(excelConnectionString)
Dim cmd As New OleDbCommand("Select * from [Sheet1$]", excelConnection)
excelConnection.Open()
Dim dReader As OleDbDataReader
dReader = cmd.ExecuteReader()
Dim sqlBulk As New SqlBulkCopy(strConnection)
sqlBulk.DestinationTableName = "pqr_table"
sqlBulk.WriteToServer(dReader)
MsgBox("Congratulations! Successfully Imported.")
        excelConnection.Close()
于 2013-08-09T11:18:11.663 回答