Option Strict On
Option Explicit On
Imports System
Imports System.Data.SqlClient
Public Class Form3
Public i As Integer
Dim mybind As New BindingSource
Private Sub b5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b5.Click
Form1.Show()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Close()
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mycmd As New SqlCommand
mycmd = fnccmddspcust()
mydataset = f11(mydataset, mycmd, "customers")
mybind.DataSource = mydataset
mybind.DataMember = "customers"
i = 0
Call showdatadetail()
End Sub
Private Sub showdatadetail()
Me.xCustomerid.Text = mydataset.Tables("customers").Rows(i).Item("customerid").ToString
Me.xcompanyname.Text = mydataset.Tables("customers").Rows(i).Item("companyname").ToString
Me.xcontactname.Text = mydataset.Tables("customers").Rows(i).Item("contactname").ToString
Me.xcontacttitle.Text = mydataset.Tables("customers").Rows(i).Item("contacttitle").ToString
Me.xaddress.Text = mydataset.Tables("customers").Rows(i).Item("address").ToString
Me.xcity.Text = mydataset.Tables("customers").Rows(i).Item("city").ToString
Me.xregion.Text = mydataset.Tables("customers").Rows(i).Item("region").ToString
Me.xpostalcode.Text = mydataset.Tables("customers").Rows(i).Item("postalcode").ToString
Me.xcountry.Text = mydataset.Tables("customers").Rows(i).Item("country").ToString
Me.xphone.Text = mydataset.Tables("customers").Rows(i).Item("phone").ToString
Me.xfax.Text = mydataset.Tables("customers").Rows(i).Item("fax").ToString
End Sub
Private Sub b3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b3.Click
If i = mydataset.Tables("customers").Rows.Count - 1 Then i = -1
i = i + 1
Call showdatadetail()
End Sub
Private Sub b2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b2.Click
If i = 0 Then i = mydataset.Tables("customers").Rows.Count
i = i - 1
Call showdatadetail()
End Sub
Private Sub b1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b1.Click
mybind.MoveFirst()
i = mybind.Position
Call showdatadetail()
End Sub
Private Sub b4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b4.Click
i = mydataset.Tables("customers").Rows.Count - 1
Call showdatadetail()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim mycustid As String = InputBox("please", "")
i = mybind.Find("Customerid", mycustid)
MessageBox.Show(i.ToString)
Call showdatadetail()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call cleartext()
Dim myrows As DataRow = mydataset.Tables("customers").NewRow
End Sub
Private Sub cleartext()
Me.xCustomerid.Text = ""
Me.xcompanyname.Text = ""
Me.xcontactname.Text = ""
Me.xcontacttitle.Text = ""
Me.xaddress.Text = ""
Me.xcity.Text = ""
Me.xregion.Text = ""
Me.xpostalcode.Text = ""
Me.xcountry.Text = ""
Me.xphone.Text = ""
Me.xfax.Text = ""
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim mydatarow As DataRow = mydataset.Tables("customers").NewRow
mydatarow(0) = xCustomerid.Text
mydatarow(1) = xcompanyname.Text
mydatarow(2) = xcontactname.Text
mydatarow(3) = xcontacttitle.Text
mydatarow(4) = xaddress.Text
mydatarow(5) = xcity.Text
mydatarow(6) = xregion.Text
mydatarow(7) = xpostalcode.Text
mydatarow(8) = xcountry.Text
mydatarow(9) = xphone.Text
mydatarow(10) = xfax.Text
Dim re As Integer
re = fnccmdinsertcust(mydatarow)
'Dim result As Integer = mycmd.ExecuteNonQuery()
'Call fncupdatedb(mycmd.CommandType.Text)
End Sub
End Class
==========================
Option Strict On
Option Explicit On
Imports System
Imports System.Data.SqlClient
Imports Microsoft.VisualBasic
Module Module1
Public mydataset As New DataSet
Public Function fncconnectiondb() As SqlConnection
Dim mycon As String
Dim mycondb As New SqlConnection
Try
mycon = "Data Source=lab322\SQLExpress;Initial Catalog=Northwind;Integrated Security=True"
mycondb.ConnectionString = mycon
If mycondb.State = ConnectionState.Closed Then
mycondb.Open()
End If
Catch ex As Exception
'MessageBox.Show(ex.ToString)
MsgBox(ex.ToString)
End Try
Return mycondb
End Function
Public Sub xmain()
'Call fncconnectiondb()
Dim mycmd As New SqlCommand
mycmd = fnccmddspcust()
mydataset = f11(mydataset, mycmd, "Customers")
mycmd = New SqlCommand
mycmd = fnccmddsppro()
mydataset = f11(mydataset, mycmd, "products")
'MessageBox.Show(mydataset.Tables.Count.ToString)
' test
End Sub
End Module
Option Strict On
Option Explicit On
Imports System
Imports System.Data.SqlClient
Imports Microsoft.VisualBasic
Public Module qcmd
Public Function fnccmddspcust() As SqlCommand
Dim mycmd As New SqlCommand
mycmd.CommandType = CommandType.Text
mycmd.CommandText = "select * from customers"
mycmd.Connection = fncconnectiondb()
mycmd.CommandTimeout = 0 ' จนกว่าจะได้ข้อมูล
mycmd.ExecuteNonQuery()
Return mycmd
End Function
Public Function fnccmdinsertcust(ByVal p As DataRow) As Integer
Dim mycmd As New SqlCommand
Dim result As Integer
mycmd.CommandType = CommandType.Text
mycmd.CommandText = "insert into customers (customerid,companyname,contactname,contacttitle,address,city,region,postalcode,country,phone,fax) values ('" & _
p("customerid").ToString & "','" & _
p("companyname").ToString & "','" & _
p("contactname").ToString & "','" & _
p("contacttitle").ToString & "','" & _
p("address").ToString & "','" & _
p("city").ToString & "','" & _
p("region").ToString & "','" & _
p("postalcode").ToString & "','" & _
p("country").ToString & "','" & _
p("phone").ToString & "','" & _
p("fax").ToString & "')"
MessageBox.Show(mycmd.CommandText.ToString)
mycmd.Connection = fncconnectiondb()
result = mycmd.ExecuteNonQuery()
Return result
End Function
Public Function fnccmddsppro() As SqlCommand
Dim mycmd As New SqlCommand
mycmd.CommandType = CommandType.Text
mycmd.CommandText = "select * from products"
mycmd.Connection = fncconnectiondb()
mycmd.CommandTimeout = 0 ' จนกว่าจะได้ข้อมูล
mycmd.ExecuteNonQuery()
Return mycmd
End Function
Public Function fncupdatedb(ByVal x As CommandType, ByVal q As SqlCommand) As SqlCommand
Dim mycmd As New SqlCommand
mycmd.CommandType = CommandType.Text
mycmd.CommandText = x.Text.ToString
mycmd.Connection = fncconnectiondb()
mycmd.CommandTimeout = 0 ' จนกว่าจะได้ข้อมูล
mycmd.ExecuteNonQuery()
Return mycmd
End Function
End Module
Option Strict On
Option Explicit On
Imports System
Imports System.Data.SqlClient
Imports Microsoft.VisualBasic
Public Module rdb
Public Function f11(ByVal p0 As DataSet, ByVal p1 As SqlCommand, ByVal p2 As String) As DataSet
'Dim mydataset As New DataSet
Dim m1 As New SqlDataAdapter(p1)
Try
If Not (p0.Tables(p2) Is Nothing) = True Then)
p0.Tables(p2).Rows.Clear())
End If)
m1.Fill(p0, p2)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Return p0
End Function
End Module
|