with con
if .state = ConnectionState.Open then .Close()
.Open()
end with
Date :
2009-07-15 08:09:36
By :
anucham
No. 4
Guest
ได้แล้วครับ ขอบคุณครับ ที่แนะนำ
Option Explicit On
Option Strict On
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim conn As New SqlConnection()
Dim ds As DataSet = New DataSet
Dim da As New SqlDataAdapter
Dim Find 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=poomin;Initial Catalog=dbtest;Persist Security Info=True;User ID=sa;Password=pwd"
If conn.State = ConnectionState.Connecting Then
conn.Close()
Else
conn.ConnectionString = strconn
conn.Open()
End If
Try
Dim strsql As String = "select * from tbuser"
da.SelectCommand = New SqlCommand(strsql, conn)
ds.Clear()
da.Fill(ds, "qrysql")
DataGridView1.DataSource = ds.Tables("qrysql")
Catch ex As Exception
MessageBox.Show(ex.Message, "ผิดพลาด")
End Try
End Sub
End Class
Date :
2009-07-15 17:29:46
By :
เด็กใหม่
No. 5
Guest
Connect ตั้งแต่ 2 DB ขึ้นไปครับ
สร้าง Module 1 ตัว
import system.data
import system.data.sqlclient
module modulesqlconnecttion
public com as new sqlcommand
public con as new sqlconnection
public connect as string
public function sqlconnection (byval i as integer)
select case i
case 1
connect = "Data Source=tee;Initial Catalog=Sale;Integrated Security=True" ' datasource connect
case 2
connect = "..."
case 3
connect = ".."
end function
public sub sqlconnect()
if con .state= connectionstate.open then con.close()
con .connectionstring = connect
con .open()
end sub
เวลาเรียกใช้
private sub insert_click....
sqlconnection (1) ' ต่อ connection ใน db1 เป็นต้น
...
...
..
end sub