如何在VB.Net中使用ADO.net创建新的数据集

时间:2017-12-20 09:42:47

标签: vb.net datagridview

我正在将数据从Excel文件传输到DataGridView。然后我将数据从DataGridView传输到SQL中的表。有没有其他方法可以做而不是手动输入列索引? 你在这段代码中看到了什么废话吗? 如何走一条路? 我的老师说搜索使用类型数据集。 我没找到任何东西。

Public Class Form1
Public Sub New()
    InitializeComponent()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub


Private DBCon As New SqlConnection("Server=LENOVO-PC;Database=Customer;User ID=sa;Password=zeynep")
Private DBCmd As New SqlCommand

Public DBDA As SqlDataAdapter
Public DBDT As DataTable

Public Sub New(ConnectionString As String)
    DBCon = New SqlConnection(ConnectionString)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Try

        Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim dataSet As System.Data.DataSet
        Dim dataSet2 As System.Data.DataSet
        Dim dataSet3 As System.Data.DataSet
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
        Dim MyCommand2 As System.Data.OleDb.OleDbDataAdapter
        Dim MyCommand3 As System.Data.OleDb.OleDbDataAdapter
        Dim path As String = "C:\\Users\\irem\\Desktop\\Customer.xls"

        MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0; HDR=YES;IMEX=1;';")
        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
        dataSet = New System.Data.DataSet
        MyCommand.Fill(dataSet)
        DataGridView1.DataSource = dataSet.Tables(0)

        MyCommand2 = New System.Data.OleDb.OleDbDataAdapter("select * from [41-GLN$]", MyConnection)
        dataSet2 = New System.Data.DataSet
        MyCommand2.Fill(dataSet2)
        DataGridView2.DataSource = dataSet2.Tables(0)

        MyCommand3 = New System.Data.OleDb.OleDbDataAdapter("select * from [Info$]", MyConnection)
        dataSet3 = New System.Data.DataSet
        MyCommand3.Fill(dataSet3)
        DataGridView3.DataSource = dataSet3.Tables(0)


        MyConnection.Close()
    Catch ex As Exception
        MsgBox(ex.Message.ToString)
    End Try
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    DBCon.Open()
    DBCmd.Connection = DBCon

    Dim i As Integer
    For i = 0 To DataGridView1.Rows.Count - 1 Step +1


        DBCmd.CommandText = "INSERT INTO Musteri(partner_id, type, fs_type, fullname, shortname, city, postalcode, address, tel, fax) VALUES('" & DataGridView1.Rows(i).Cells(0).Value & "','" & DataGridView1.Rows(i).Cells(1).Value & "','" & DataGridView1.Rows(i).Cells(2).Value & "','" & DataGridView1.Rows(i).Cells(3).Value & "','" & DataGridView1.Rows(i).Cells(4).Value & "','" & DataGridView1.Rows(i).Cells(5).Value & "','" & DataGridView1.Rows(i).Cells(6).Value & "','" & DataGridView1.Rows(i).Cells(7).Value & "','" & DataGridView1.Rows(i).Cells(8).Value & "','" & DataGridView1.Rows(i).Cells(9).Value & "')"

        DBCmd.ExecuteNonQuery()

    Next

    For i = 0 To DataGridView2.Rows.Count - 1 Step +1


        DBCmd.CommandText = "INSERT INTO GLN(GLN,partner_id) VALUES('" & DataGridView2.Rows(i).Cells(0).Value & "','" & DataGridView2.Rows(i).Cells(1).Value & "')"
        DBCmd.ExecuteNonQuery()

    Next


    For i = 0 To DataGridView3.Rows.Count - 1 Step +1


        DBCmd.CommandText = "INSERT INTO Info(Customer_Mast_Data) VALUES('" & DataGridView3.Rows(i).Cells(0).Value & "')"
        DBCmd.ExecuteNonQuery()

    Next

    DBCon.Close()
    MsgBox("Transfer is succesfully!")
End Sub

结束班

0 个答案:

没有答案