0

I am transferring data from the Excel file to the DataGridView. Then I transfer the data from the DataGridView to the table in SQL. Is there any other way I can do instead of manually entering column indices? Are there any nonsense things you see in this code? How to follow a path? My teacher said search for using type dataset. I did not find anything.

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

End Class

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75

0 Answers0