Public connetionString As SqlConnection = New SqlConnection("Data Source=PONGWICS2014-PC; Initial Catalog=data1;User ID=sa;Password=Pong")
Public sqlCommand As SqlCommand = New SqlCommand
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnn As SqlConnection
Dim connetionString = "Data Source=PONGWICS2014-PC; Initial Catalog=data1;User ID=sa;Password=Pong"
cnn = New SqlConnection(connetionString)
Dim strCon As String = cnn.ToString
Try
cnn.Open()
MsgBox("เชื่อมต่อได้แกล้ว ")
cnn.Close()
Catch ex As Exception
MsgBox("โอ้ๆ มันเกิดอะไรขึ้น ")
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Dim aaa As String = ""
' Dim bbb As String = ""
Try
Dim str As String = "select user,password From inputdata where user = '" & user.Text & "' and password = '" & password.Text & "'"
Dim da As New SqlDataAdapter(str, connetionString)
Dim ds As New DataSet
' Dim dt As New DataTable
' aaa = user.Text
' bbb = password.Text
da.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
Me.Hide()
Form2.Show()
Else
MessageBox.Show("error")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
[/vb]
Tag : MySQL, Ms SQL Server 2005, Ms SQL Server 2008, Ms SQL Server 2012, VB.NET
Imports System.Data.SqlClient
Imports System.IO
Public Class Form1
Dim Conn As SqlConnection
Dim Invalid As Integer
Dim rsToAffect As Long
Dim connetionString As String = "Data Source=PONGWICS2014-PC; Initial Catalog=data1;User ID=sa;Password=Pong"
Public sqlCommand As SqlCommand = New SqlCommand
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnn As SqlConnection
cnn = New SqlConnection(connetionString)
Dim strCon As String = cnn.ToString
Try
cnn.Open()
MsgBox("เชื่อมต่อได้แกล้ว ")
cnn.Close()
Catch ex As Exception
MsgBox("โอ้ๆ มันเกิดอะไรขึ้น ")
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim aaa As String = ""
Dim bbb As String = ""
Try
Dim strSql As String = "select [user],[password] From inputdata where [user] = '" & user.Text & "' and [password] = '" & password.Text & "'"
Dim da As New SqlDataAdapter(strSql, connetionString)
Dim ds As New DataSet
Dim dt As New DataTable
da.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
Form2.Show()
Me.Hide()
Else
MessageBox.Show("ไม่พบผู้ใช้งาน หรือ รหัสผ่านไม่ถูกต้อง")
End If
Catch ex As Exception
MessageBox.Show("error TTT")
End Try
End Sub
End Class
หลีกเลี่ยง การตั้งชื่อ attribute ที่เป็น reserved word ใน database นะครับ