Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim conn As New SqlConnection
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim sql As String = "select * from View_book1"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
ConnectDatabase()
End Sub
Private Sub ConnectDatabase()
Me.CenterToScreen()
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
Try
sql = "select * from View_book1"
da = New SqlDataAdapter(sql, conn)
ds = New DataSet
da.Fill(ds, "PubName")
DataGridView1.DataSource = ds.Tables("PubName")
Catch ex As Exception
MsgBox(ex.Message)
End Try
sql = "select * from bookType "
Try
da = New SqlDataAdapter(sql, conn)
ds = New DataSet
da.Fill(ds, "bookType")
ComboBox1.DataSource = ds.Tables("bookType")
ComboBox1.ValueMember = ds.Tables("bookType").Columns(0).ToString
ComboBox1.ValueMember = ds.Tables("bookType").Columns(1).ToString
Catch ex As Exception
MsgBox(ex.Message)
End Try
sql = "select * from Publisher"
Try
da = New SqlDataAdapter(sql, conn)
ds = New DataSet
da.Fill(ds, "Publisher")
ComboBox2.DataSource = ds.Tables("Publisher")
ComboBox2.ValueMember = ds.Tables("Publisher").Columns(0).ToString
ComboBox2.ValueMember = ds.Tables("Publisher").Columns(1).ToString
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "Add" Then
Button1.Text = "Save"
GroupBox1.Enabled = True
TextBox1.Focus()
Else
sql = " insert book set ISBN= '" + TextBox1.Text + "', "
sql = sql + " BName ='" + TextBox2.Text + "', "
sql = sql + " PubID ='" + TextBox3.Text + "', "
sql = sql + " AuID ='" + TextBox4.Text + "', "
MsgBox(sql)
Try
Dim ComAdd As SqlCommand = conn.CreateCommand
ComAdd.Connection = conn
ComAdd.CommandText = sql
ComAdd.ExecuteNonQuery()
Catch ex As Exception
End Try
Button1.Text = "Add"
GroupBox1.Enabled = False
conn.Close()
End If
Showdata()
End Sub
Private Sub Showdata()
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim r As New Form1
r.Show()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Button2.Text = "แก้ไข" Then
GroupBox1.Enabled = True
TextBox2.Focus()
Button2.Text = "ปรับปรุง"
Else
sql = " Update book set ISBN= '" + TextBox1.Text + "', "
sql = sql + " BName ='" + TextBox2.Text + "', "
sql = sql + " PubID ='" + TextBox3.Text + "', "
sql = sql + " AuID ='" + TextBox4.Text + "', "
MsgBox(sql)
Try
Dim ComAdd As SqlCommand = conn.CreateCommand
ComAdd.Connection = conn
ComAdd.CommandText = sql
ComAdd.ExecuteNonQuery()
Catch ex As Exception
End Try
GroupBox2.Enabled = True
Showdata() ' แสดงข้อมูล
GroupBox2.Enabled = True
Button2.Text = "แก้ไข"
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim dDel, ndel As String
dDel = " ลบรายการ " & TextBox1.Text & " ชื่อ" & TextBox2.Text
ndel = MsgBox(dDel, 16 + vbYesNo + vbDefaultButton1, " ลบข้อมูล ")
If ndel = 6 Then
With conn ' ตัวแปรใช้ติดต่อฐานข้อมูล
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
Try
Dim Convendor As SqlCommand = conn.CreateCommand
Convendor.Connection = conn
sql = "delete from book where ISBN ='" + TextBox1.Text + "'"
MsgBox(sql)
Convendor.CommandText = sql
Convendor.ExecuteNonQuery()
Showdata()
Catch
MsgBox(" ไม่สามารถลบข้อมูลได้ ", MsgBoxStyle.Information, "แจ้ง Error")
End Try
End If
End Sub
Private Sub autoISBN()
With conn ' ตัวแปรใช้ติดต่อฐานข้อมูล
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
sql = "SELECT top 1 ISBN From book Order By ISBN desc"
da = New SqlDataAdapter(sql, conn)
ds = New DataSet
da.Fill(ds, "book")
Dim ncode, prefix As String
Dim postfix As Integer
ncode = ds.Tables("book").Rows(0).Item("ISBN")
prefix = Mid(ncode, 1, 1)
postfix = Mid(ncode, 2, 4) + 1
TextBox1.Text = prefix + postfix.ToString("0000")
End Sub
End Class
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim conn As New SqlConnection
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim sql As String = "select * from View_book1"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
ConnectDatabase()
End Sub
Private Sub ConnectDatabase()
Me.CenterToScreen()
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
Try
sql = "select * from View_book1"
da = New SqlDataAdapter(sql, conn)
ds = New DataSet
da.Fill(ds, "PubName")
DataGridView1.DataSource = ds.Tables("PubName")
Catch ex As Exception
MsgBox(ex.Message)
End Try
sql = "select * from bookType "
Try
da = New SqlDataAdapter(sql, conn)
ds = New DataSet
da.Fill(ds, "bookType")
ComboBox1.DataSource = ds.Tables("bookType")
ComboBox1.ValueMember = ds.Tables("bookType").Columns(0).ToString
ComboBox1.ValueMember = ds.Tables("bookType").Columns(1).ToString
Catch ex As Exception
MsgBox(ex.Message)
End Try
sql = "select * from Publisher"
Try
da = New SqlDataAdapter(sql, conn)
ds = New DataSet
da.Fill(ds, "Publisher")
ComboBox2.DataSource = ds.Tables("Publisher")
ComboBox2.ValueMember = ds.Tables("Publisher").Columns(0).ToString
ComboBox2.ValueMember = ds.Tables("Publisher").Columns(1).ToString
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "Add" Then
Button1.Text = "Save"
GroupBox1.Enabled = True
TextBox1.Focus()
Else
'INSERT INTO table_name VALUES(v1,v1...) หรือจะ UPDATE tablename SETS column1 = v1, column2 = v2...
sql = " insert book set ISBN= '" + TextBox1.Text + "', "
sql = sql + " BName ='" + TextBox2.Text + "', "
sql = sql + " PubID ='" + TextBox3.Text + "', "
sql = sql + " AuID ='" + TextBox4.Text + "', "
MsgBox(sql)
Try
Dim ComAdd As SqlCommand = conn.CreateCommand
ComAdd.Connection = conn
ComAdd.CommandText = sql
ComAdd.ExecuteNonQuery()
Catch ex As Exception
End Try
Button1.Text = "Add"
GroupBox1.Enabled = False
conn.Close()
End If
Showdata()
End Sub
Private Sub Showdata()
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim r As New Form1
r.Show()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Button2.Text = "แก้ไข" Then
GroupBox1.Enabled = True
TextBox2.Focus()
Button2.Text = "ปรับปรุง"
Else
sql = " Update book set ISBN= '" + TextBox1.Text + "', "
sql = sql + " BName ='" + TextBox2.Text + "', "
sql = sql + " PubID ='" + TextBox3.Text + "', "
sql = sql + " AuID ='" + TextBox4.Text + "', "
MsgBox(sql)
Try
Dim ComAdd As SqlCommand = conn.CreateCommand
ComAdd.Connection = conn
ComAdd.CommandText = sql
ComAdd.ExecuteNonQuery()
Catch ex As Exception
End Try
GroupBox2.Enabled = True
Showdata() ' แสดงข้อมูล
GroupBox2.Enabled = True
Button2.Text = "แก้ไข"
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim dDel, ndel As String
dDel = " ลบรายการ " & TextBox1.Text & " ชื่อ" & TextBox2.Text
ndel = MsgBox(dDel, 16 + vbYesNo + vbDefaultButton1, " ลบข้อมูล ")
If ndel = 6 Then
With conn ' ตัวแปรใช้ติดต่อฐานข้อมูล
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
Try
Dim Convendor As SqlCommand = conn.CreateCommand
Convendor.Connection = conn
sql = "delete from book where ISBN ='" + TextBox1.Text + "'"
MsgBox(sql)
Convendor.CommandText = sql
Convendor.ExecuteNonQuery()
Showdata()
Catch
MsgBox(" ไม่สามารถลบข้อมูลได้ ", MsgBoxStyle.Information, "แจ้ง Error")
End Try
End If
End Sub
Private Sub autoISBN()
With conn ' ตัวแปรใช้ติดต่อฐานข้อมูล
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
sql = "SELECT top 1 ISBN From book Order By ISBN desc"
da = New SqlDataAdapter(sql, conn)
ds = New DataSet
da.Fill(ds, "book")
Dim ncode, prefix As String
Dim postfix As Integer
ncode = ds.Tables("book").Rows(0).Item("ISBN")
prefix = Mid(ncode, 1, 1)
postfix = Mid(ncode, 2, 4) + 1
TextBox1.Text = prefix + postfix.ToString("0000")
End Sub
End Class