Imports System
Imports System.Data
Imports System.Data.SqlClient
Class PropagateAddsBuilder
Shared Sub Main()
Dim connString As String = "server=(local)\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI"
Dim qry As String = "select * from employee"
Dim conn As New SqlConnection(connString)
Try
Dim da As New SqlDataAdapter()
da.SelectCommand = New SqlCommand(qry, conn)
Dim cb As New SqlCommandBuilder(da)
Dim ds As New DataSet()
da.Fill(ds, "employee")
Dim dt As DataTable = ds.Tables("employee")
' Add a row
Dim newRow As DataRow = dt.NewRow()
newRow("firstname") = "y"
newRow("lastname") = "y"
dt.Rows.Add(newRow)
For Each row As DataRow In dt.Rows
Console.WriteLine("{0} {1}", row("firstname").ToString().PadRight(15), row("lastname").ToString().PadLeft(25))
Next
da.Update(ds, "employee")
Catch e As Exception
Console.WriteLine("Error: " + e)
Finally
conn.Close()
End Try
End Sub
End Class
Private Sub UpdateBinding()
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
ctrl.DataBindings.Clear()
End If
Next
sql = "SELECT * FROM Shop"
command = New SqlCommand(sql, connection)
adapter = New SqlDataAdapter(command)
dataSt = New DataSet()
adapter.Fill(dataSt, "Shop")
bindingSrc = New BindingSource()
bindingSrc.DataSource = dataSt.Tables("Shop")
ID.DataBindings.Add("Text", bindingSrc, "shopID")
name1.DataBindings.Add("Text", bindingSrc, "shopname")
address.DataBindings.Add("Text", bindingSrc, "shopaddress")
tel.DataBindings.Add("Text", bindingSrc, "shoptel")
email.DataBindings.Add("Text", bindingSrc, "shopemail")
ListBox1.DataSource = dataSt.Tables("Shop")
ListBox1.DisplayMember = ("shopname")
End Sub
Code (VB.NET)
Private Sub update1_Click() Handles update1.Click
If ID.Text = "" Or IsDatacomplete() = False Then
Exit Sub
End If
sql = "UPDATE Shop SET " & _
"shopname = @aname, shopaddress = @aaddress, " & _
"shoptel = @atel, shopemail = @aemail " & _
"WHERE shopID = @id"
command.Parameters.Clear()
command.CommandText = sql
command.Parameters.AddWithValue("aname", name1.Text)
command.Parameters.AddWithValue("aaddress", address.Text)
command.Parameters.AddWithValue("atel", tel.Text)
command.Parameters.AddWithValue("aemail", email.Text)
command.Parameters.AddWithValue("id", ID.Text)
If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("บันทึกการเปลี่ยนแปลงแล้ว")
UpdateBinding()
Else
MessageBox.Show("เกิดข้อผิดพลาดในการแก้ไขข้อมูล")
End If
End Sub
Module Module1
Public conStr As String = "Data Source=.\SQLEXPRESS;" & _
"AttachDbFilename=|DataDirectory|\SIRAKLANG.mdf;" & _
"Integrated Security=True;User Instance=True"
Public connection As SqlConnection
Public command As SqlCommand
Public dataSt As DataSet
Public adapter As SqlDataAdapter
Public bindingSrc As BindingSource
Public reader As SqlDataReader
Public sql As String
Public Function GetshopID(ByVal name As String) As Integer
sql = "SELECT shopID FROM Shop " & _
"WHERE shopname =@name"
command = New SqlCommand(sql, connection)
command.Parameters.AddWithValue("name", name)
Return command.ExecuteScalar()
End Function
End Module