Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql VB-连接到本地SQL server并从excel将数据加载到表中_Mysql_Sql Server_Excel_Vb.net_Database Connection - Fatal编程技术网

Mysql VB-连接到本地SQL server并从excel将数据加载到表中

Mysql VB-连接到本地SQL server并从excel将数据加载到表中,mysql,sql-server,excel,vb.net,database-connection,Mysql,Sql Server,Excel,Vb.net,Database Connection,我想用VB代码连接到本地SQL Server,并将数据从Excel文件加载到表中。到目前为止,我的代码是不完整的。我在SQL数据库中创建的表(me_表)的字段是z、ad、ag、retd、to、wg,在Excel sheet1中是列,其中包含反映表中字段的数据。谢谢 请告知 Imports System.Data Imports System.Data.SqlClient Module Module1 Dim myconnection As SqlConnection Dim m

我想用VB代码连接到本地SQL Server,并将数据从Excel文件加载到表中。到目前为止,我的代码是不完整的。我在SQL数据库中创建的表(me_表)的字段是z、ad、ag、retd、to、wg,在Excel sheet1中是列,其中包含反映表中字段的数据。谢谢

请告知

Imports System.Data
Imports System.Data.SqlClient

Module Module1
    Dim myconnection As SqlConnection
    Dim mycommand As SqlCommand
    Dim dr As SqlDataReader
    Dim dr1 As SqlDataReader
    Dim ra As Integer

    Sub Main()

        Dim connectionString As String = "Server=DER7D;Database=testDB;User Id=DER7D\Der;Password="
        myconnection = New SqlConnection("server=DER7D;uid=root;pwd=;database=simple")
        'you need to provide password for sql server
        myconnection.Open()

    End Sub

End Module

没有考虑好的设计,这就是你需要的。只需更新SqlConnection字符串。如果决定使用多张图纸,只需将它们添加到worksheets变量中即可

Private Sub SaveDataFromSpreadsheet()
Dim filePath = "directory\me_spreadsheet.xlsx"
Dim connectionString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;", filePath)
Dim worksheets As New List(Of String)() From {
  "Sheet1"
}

For i As Integer = 0 To worksheets.Count - 1
  Dim worksheetName As String = worksheets(i)
  Dim adapter = New OleDbDataAdapter(String.Format("SELECT * FROM [{0}$]", worksheetName), connectionString)
  Dim ds = New DataSet()

  adapter.Fill(ds, "me_table")

  Dim data As DataTable = ds.Tables("me_table")

  For x As Integer = 0 To data.Rows.Count - 1
    Dim z As String = If(String.IsNullOrEmpty(data.Rows(x).ItemArray(0).ToString()), "", data.Rows(x).ItemArray(0).ToString())
    Dim ad As String = If(String.IsNullOrEmpty(data.Rows(x).ItemArray(1).ToString()), "", data.Rows(x).ItemArray(1).ToString())
    Dim ag As String = If(String.IsNullOrEmpty(data.Rows(x).ItemArray(2).ToString()), "", data.Rows(x).ItemArray(2).ToString())
    Dim retd As String = If(String.IsNullOrEmpty(data.Rows(x).ItemArray(3).ToString()), "", data.Rows(x).ItemArray(3).ToString())
    Dim wg As String = If(String.IsNullOrEmpty(data.Rows(x).ItemArray(4).ToString()), "", data.Rows(x).ItemArray(4).ToString())

    Using myconnection As New SqlConnection("Data Source=Your-Server;Initial Catalog=me_database;Integrated Security=True")
      myconnection.Open()
      Dim mycommand As New SqlCommand("INSERT INTO me_Table(z, ad, ag, retd, wg) VALUES(@z, @ad, @ag, @retd, @wg)", myconnection)

      mycommand.Parameters.Add(New SqlParameter("@z", z))
      mycommand.Parameters.Add(New SqlParameter("@ad", ad))
      mycommand.Parameters.Add(New SqlParameter("@ag", ag))
      mycommand.Parameters.Add(New SqlParameter("@retd", retd))
      mycommand.Parameters.Add(New SqlParameter("@wg", wg))

      mycommand.ExecuteNonQuery()
      myconnection.Close()
    End Using

  Next
Next
End Sub

听起来第一步是加载excel数据这里有一个从excel工作表获取数据的链接:好的,谢谢您提供的信息和链接。我正在研究它。Dim adapter=New-OleDbDataAdapter(String.Format(“SELECT*FROM[{0}$]”,worksheetName),connectionString)----。。。。。。。。谢谢你的代码,但是OleDbDataAdapter有一个错误没有定义。我发现这个问题需要使用Import System.Data.Oledb,我正在查看代码的其余部分。感谢非常好的代码,它可以工作。非常感谢,我真的很感激。:-)很抱歉再次打扰您Krob,我注意到它在我的window 7和sql server 2014中起作用,但是该代码在window 10、office 360和sql server 2016中不起作用。代码适配器.fill(da,“zipco”)出错。错误消息Microsoft.ace.oledb.12.0提供程序未在本地计算机上注册。请告知。谢谢,没问题。您需要安装这些组件。任何一个都应该有效