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 String, ByVal NewNameStr As String, ByVal
OldNameStr As String)
Dim mySelectQuery As
String = "SELECT
ProductCategoryId, Name FROM Production.ProductCategory"
Dim myUpdateQuery As
String = "Update
Production.ProductCategory set Name = ('" & NewNameStr & "') Where Name = '" & OldNameStr
& "'"
Dim myConnection As
New SqlConnection(myConnString)
Dim myCommand As
New SqlCommand(mySelectQuery, myConnection)
Dim myCommand2 As
New SqlCommand(myUpdateQuery, 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 NewNameStr As
String
Dim OldNameStr As
String
Dim id As Integer
NewNameStr = Me.TextBox1.Text
OldNameStr = Me.TextBox2.Text
ConnString = "Data Source= .\SQLEXPRESS;Initial
Catalog = C:\ADVENTUREWORKS_DATA.mdf;Integrated Security=True;User
Instance=True;"
InsertMyData(ConnString, NewNameStr, OldNameStr)
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