Dim connection As New OdbcConnection("Driver={PostgreSQL};Server=IP address;Port=5432;Database=myDataBase;Uid=myUsername;Pwd=myPassword;")
connection.Open()
System.Console.WriteLine("State: " & connection.State.ToString())
Dim query As String = "SELECT * FROM TESTTABLE"
Dim command As New OdbcCommand(query, connection)
Dim reader As OdbcDataReader = command.ExecuteReader(CommandBehavior.CloseConnection)
While reader.Read() = True
Console.WriteLine("New Row:")
For i As Integer = 0 To reader.FieldCount - 1
Console.WriteLine(reader.GetString(i))
Next
End While
reader.Close()
connection.Close()
Imports System.Data
Imports ADODB
Public Conn As New ADODB.Connection
Public rsttd As New ADODB.Recordset
Public StrConn As String
Dim StrSql As String
StrConn = "Driver=PostgreSQL;server=192.168.0.xxx;database=cts2012;uid=ปป;Pwd=กก;"
Conn = New ADODB.Connection
Conn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
Conn.Open(StrConn)
StrSql = "select * from Bank"
If rsttd.State = ObjectStateEnum.adStateOpen Then rsttd.Close()
rsttd.Open(StrSql, Conn, CursorTypeEnum.adOpenStatic, LockTypeEnum.adLockOptimistic)
Dim xx As String = rsttd.Fields.Item("bankname").Value
TextBox1.Text = xx 'บรรทัดนี้ผมสามารถดึงข้อมูลจาก Table Bank มาโชว์ที่ Textbox ได้แล้ว
"แต่ที่ผมจะเขียนต่อคือ ให้ดึงข้อมูลมาโชว์ที่ DataGridView1 ดังโค๊ดข้างล่างนี้ครับ"
Dim dataadapter As New SqlDataAdapter(StrSql, Conn)
Dim ds As New DataSet()
Conn.Open()
dataadapter.Fill(ds, "Bank")
Conn.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Bank"