Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim Conn As SqlConnection = New SqlConnection
Dim da As SqlDataAdapter
Dim add_state As Boolean = False
Dim dt As New DataTable
' Dim db As New DataTable
Dim land As SqlCommand
Private bin As New BindingSource
Dim currRecPos As Integer
Dim addNewMode As Boolean
Dim ds As DataSet = New DataSet
Dim IsFind As Boolean = False
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strConn As String
strConn = "Data Source=.\SQLEXPRESS;Initial Catalog=SSPEGIS4;Integrated Security=True"
Dim sqlupdate As String
Dim sqladd As String
sqlupdate = "update land"
sqlupdate &= " set PARCEL_CODE = '" & txtPARCEL_CODE.Text & "',"
sqlupdate &= " TAMBON_TH= '" & txtTAMBON_TH.Text & "',"
sqlupdate &= " DISTRICT_TH= " & Val(txtDISTRICT_TH.Text) & ","
sqladd = "insert into land (PARCEL_CODE,TAMBON_TH,DISTRICT_TH)"
sqladd &= " values ('" & txtPARCEL_CODE.Text & "',"
sqladd &= " '" & txtTAMBON_TH.Text & "',"
sqladd &= "'" & txtDISTRICT_TH.Text & "',"
Try
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
land = New SqlCommand
With land
.CommandType = CommandType.Text
If add_state = True Then
.CommandText = sqladd
Else
.CommandText = sqlupdate
End If
.Connection = Conn
.ExecuteNonQuery()
End With
Catch ErrorToEdit As Exception
MessageBox.Show(ErrorToEdit.Message & sqladd, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
add_state = False
MessageBox.Show("บันทึกข้อมูลเสร็จแล้ว ", "แจ้งให้ทราบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
addNewMode = True
currRecPos = bin.Position
bin.AddNew()
End Sub
End Class
ตัวอย่าง ผมเขียนเเบบถูกมั้ยครับ ช่วยเเนะนำหน่อยครับ คือผมมีหลายตาราง เเต่อยากให้ตารางเดียวที่อยากให้ Save ลงฐาน ส่วนตารางอื่น เอาไว้โชว์ ข้อมูลเฉยๆ
Tag : .NET, Ms SQL Server 2008, Win (Windows App), VS 2008 (.NET 3.x)