 |
|
|
 |
 |
|
Code (VB.NET)
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
นี้คือโค๊ดอะคะ
|
 |
 |
 |
 |
Date :
2010-02-16 11:21:08 |
By :
P |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Symptom:
DataGrid not display correct data event if create/remove/edit
Solution:
Replace all ShowData() With ConnectDatabase()
At Line137, 186
Possible Causes:
ShowData only connect to database ,not assigned any data to datagrid
hope it help ka
|
 |
 |
 |
 |
Date :
2010-02-16 11:21:44 |
By :
blurEye |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Module Module1
Public strConn As String = "Data Source=CODENAME;Initial Catalog=BookForSales;Integrated Security=True"
End Module
ส่วนโมดูลคะ
|
 |
 |
 |
 |
Date :
2010-02-16 11:22:10 |
By :
p |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

รันออกมาเป็นแบบนี้ค่ะ
ไม่สามารถเพิ่มได้เลย
|
 |
 |
 |
 |
Date :
2010-02-16 11:24:05 |
By :
p |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
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
อื่นๆ ลองดูใหม่
|
 |
 |
 |
 |
Date :
2010-02-16 11:35:11 |
By :
numenoy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เงียบไปหมดเลย
งงเหรอคะ
โพสแบบนี้
|
 |
 |
 |
 |
Date :
2010-02-16 11:44:14 |
By :
p |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบแล้วค่ะ
ที่บล้อกที่ 2 จากข้างบนค่ะ ^^
Symptom:
DataGrid not display correct data event if create/remove/edit
Solution:
Replace all ShowData() With ConnectDatabase()
At Line137, 186
Possible Causes:
ShowData only connect to database ,not assigned any data to datagrid
hope it help ka
|
 |
 |
 |
 |
Date :
2010-02-16 11:52:19 |
By :
blurEye |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาใส่ส่วนไหนอะ
|
 |
 |
 |
 |
Date :
2010-02-16 12:17:29 |
By :
p |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แทน ShowData() ด้วย ConnectDatabase()
ที่บรรทัด 137 และ 186 ตาม Code ที่โพสไว้ค่ะ
ยังไม่ได้เขียน ADD ใช่ไหมคะ เพราะยังไม่เห็นเลยอย่าลืมเขียนเพิ่มนะคะ
แล้วแนะนำว่า ชื่อปุ่มควรตั้งให้สื่อค่ะ Button1 Button2 ดูแล้วเข้าใจยาก
ตั้งไปเลยค่ะให้เข้าใจจะเป็นภาษาคาราโอเกะก็ได้ อย่าง ButtonKumnumPolLub
หรือ ButtonPermKorMool ค่ะ
|
 |
 |
 |
 |
Date :
2010-02-16 12:25:56 |
By :
blurEye |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังเหมือนเดิมค่ะ
และก็ไม่เออเลอด้วย
แต่ทำไมไม่เพิ่มให้นะคะ
งงจริงๆ
|
 |
 |
 |
 |
Date :
2010-02-16 12:37:53 |
By :
p |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มัน error นะคะ
แต่ไม่แสดงออก
sql = sql + " AuID ='" + TextBox4.Text + "', "
ตัวสุดท้ายตัดออกไปค่ะ
sql = sql + " AuID ='" + TextBox4.Text + "'"
ที่บรรทัด 71 ,123 ค่ะ
แล้ว เพิ่ม
Catch ex As Exception
End Try
เป็น
Catch ex As Exception
Throw New Exception ( ex.Message )
End Try
มันจะแสดง error ออกมาให้เห็นค่ะ
|
 |
 |
 |
 |
Date :
2010-02-16 12:45:38 |
By :
blurEye |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดูเหมือนจะมี error เยอะอยู่นะ insert command กับ update command อะนะ อิอิ หุหุ 555+ 
|
 |
 |
 |
 |
Date :
2010-02-16 13:09:07 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ พี่ tungman ที่แว็บมาเยี่ยมเยียนค่ะ
ช่วยแนะนำหน่อยนะคะ
น้องเห็นคุณพี่ไปแนะนำหลายกรูทู้เลย
|
 |
 |
 |
 |
Date :
2010-02-16 13:57:55 |
By :
p |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมเป็นคนบ้าครับ ผ่านไปผ่านมา
ลองแก้ command ดู
Code (VB.NET)
sql = "Insert Into [book] ([ISBN], [BName], [PubID], [AuID]) Values ('" & TextBox1.Text & "', "
sql = sql & "'" & TextBox2.Text & "', "
sql = sql & "'" & TextBox3.Text & "', "
sql = sql & "'" & TextBox4.Text & "'"
Code (VB.NET)
sql = "Update [book] Set [ISBN]= '" & TextBox1.Text & "', "
sql = sql & " [BName] = '" & TextBox2.Text & "', "
sql = sql & " [PubID] = '" & TextBox3.Text & "', "
sql = sql & " [AuID] = '" & TextBox4.Text & "'"
|
 |
 |
 |
 |
Date :
2010-02-16 14:10:13 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เห็นด้วยกับคุณ tungman ค่ะ
โค้ดนะคะ ไม่ใช่ประโยคแรก อิอิ
|
 |
 |
 |
 |
Date :
2010-02-18 15:12:09 |
By :
njnight |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หน้าจอฟอร์มvb2008ที่เขียนโค้ตเรียบร้อยแล้วทำไมพอย้ายปุ่มแล้ว กดปุ่มรันแล้วปุ่มยังเหมือนเดิมไม่ได้รับการเคลื่อนย้าย
ช่วยแนะนำหน่อนค่ะ
|
 |
 |
 |
 |
Date :
2010-11-05 23:16:56 |
By :
wachira |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หน้าจอฟอร์มvb2008ที่เขียนโค้ตเรียบร้อยแล้วทำไมพอย้ายปุ่มแล้ว กดปุ่มรันแล้วปุ่มยังเหมือนเดิมไม่ได้รับการเคลื่อนย้าย
ช่วยแนะนำหน่อนค่ะ
ลอง rebuild ดูน่าจะหายนะ เพราะมันยังจำค่าเดิมอยู่
|
 |
 |
 |
 |
Date :
2010-11-06 10:37:59 |
By :
babyprogrammer |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
การเชื่อมต่อฐานข้อมูล sql 2008 เข้ากับ vb 2005 ละค่ะ ขอตัวอย่างโค้ดหน่อยค่ะ
|
 |
 |
 |
 |
Date :
2010-11-08 10:59:45 |
By :
demo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
visual studio 2008 เชื่อมกับ sql 2005 เชื่อมกันได้ไหมค่ะ ขอโค้ดด้วยค่ะ ขอบคุณค่ะ
|
 |
 |
 |
 |
Date :
2011-11-20 12:01:41 |
By :
pupe |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|