Imports System.Data.SqlClient

Public Class Form1

    Private ConnString As String

 

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

        'CnStr ="Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks_Data.mdf";Integrated Security=True;User Instance=True"

    End Sub

    Public Sub InsertMyData(ByVal myConnString As StringByVal NameStr As String)

        Dim mySelectQuery As String = "SELECT ProductCategoryId, Name FROM Production.ProductCategory"

        Dim myInsertQuery As String = "INSERT into Production.ProductCategory(Name) VALUES('" & NameStr & "')"

        Dim myConnection As New SqlConnection(myConnString)

        Dim myCommand As New SqlCommand(mySelectQuery, myConnection)

        Dim myCommand2 As New SqlCommand(myInsertQuery, myConnection)

        Dim retvalue As Integer

        myConnection.Open()

        retvalue = myCommand2.ExecuteNonQuery()

        Console.WriteLine(retvalue)

        Dim myReader As SqlDataReader

        myReader = myCommand.ExecuteReader()

        ' Always call Read before accessing data.

        While myReader.Read()

            Console.WriteLine((myReader.GetInt32(0) & ", " & myReader.GetString(1)))

        End While

        ' always call Close when done reading.

        myReader.Close()

        ' Close the connection when done with it.

        myConnection.Close()

 

    End Sub 'ReadMyData

 

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

        Dim NameStr As String

        Dim id As Integer

 

        NameStr = Me.TextBox1.Text

        ConnString = "Data Source= .\SQLEXPRESS;Initial Catalog = C:\ADVENTUREWORKS_DATA.mdf;Integrated Security=True;User Instance=True;"

        InsertMyData(ConnString, NameStr)

    End Sub

 

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Me.Close()

    End Sub

End Class