Imports System.Data.SqlClient
Imports System.Data.OleDb
Public Class Form1
Private strKeyWord As String = ""
Dim str As String
Dim Conn As SqlConnection
Dim cmd As New SqlCommand
Dim connetionString As SqlConnection = New SqlConnection("Data Source=PONGWICS2014-PC; Initial Catalog=Topdata1;User ID=sa;Password=Pong")
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Topdata1DataSet.Business' table. You can move, or remove it, as needed.
Me.BusinessTableAdapter.Fill(Me.Topdata1DataSet.Business)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Print.Click
str = "select * From Business where username = '" & username.Text & "' and password = '" & password.Text & "'"
Dim myCommand As SqlCommand = New SqlCommand(str, connetionString)
Dim a As String
Try
If username.Text = Nothing Then
MsgBox("You Must Enter The Username!", MsgBoxStyle.Exclamation, "Error!")
MsgBox("ท่านยังไม่ใส่ข้อมูล ของ username ไม่ครบถ้วน") : Exit Sub
End If
If password.Text = Nothing Then
MsgBox("You Must Enter The Passworld !", MsgBoxStyle.Exclamation, "Error!")
MsgBox("ท่านใส่ข้อมูลของ password ไม่ครบถ้วน") : Exit Sub
End If
connetionString.Open()
Dim result = myCommand.ExecuteScalar()
If result IsNot Nothing Then
MessageBox.Show("Database is created successfully", _
"MyProgram", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Else
MessageBox.Show("Error เนื่องจากไม่สามารถทำได้ ", MessageBoxButtons.OKCancel.ToString)
End If
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
If (connetionString.State = ConnectionState.Open) Then
connetionString.Close()
End If
End Try
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub username_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles username.TextChanged
Dim dt As New DataTable
Dim str As String
Dim index As Integer = 0
str = "SELECT username,password FROM Business "
strKeyWord = Me.username.Text
PopulateData()
End Sub
Private Sub PopulateData()
Dim objConn As SqlConnection
Dim dtAdapter As SqlDataAdapter
Dim dt As New DataTable
Dim ds As New DataSet
' Dim strConnString As String
Dim connetionString As SqlConnection = New SqlConnection("Data Source=PONGWICS2014-PC; Initial Catalog=Topdata1;User ID=sa;Password=Pong")
' Dim myCommand As SqlCommand = New SqlCommand(str, connetionString)
connetionString.Open()
'Dim strSQL As String
str = "SELECT * FROM Business WHERE username = '" & username.Text & ""
'str = "SELECT username,password FROM Business "
' If strKeyWord <> "" Then
'str = str & " AND Name Like '%" & strKeyWord & "%' "
' End If
dtAdapter = New SqlDataAdapter(str, Conn)
'dtAdapter.Fill(dt)
dt = ds.Tables(str)
'dtAdapter = Nothing
connetionString.Close()
objConn = Nothing
Me.DataGridView1.Columns.Clear()
With (DataGridView1.ColumnHeadersDefaultCellStyle)
.BackColor = Color.Navy
.ForeColor = Color.White
.Font = New Font(DataGridView1.Font, FontStyle.Bold)
End With
Dim ColumnTextBox As DataGridViewTextBoxColumn
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "username"
ColumnTextBox.HeaderText = "username"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
' ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "password"
ColumnTextBox.HeaderText = "password"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
' ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Employee"
ColumnTextBox.HeaderText = "Employee"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
' ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Area"
ColumnTextBox.HeaderText = "Area"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
' ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "Enname"
ColumnTextBox.HeaderText = "Enname"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
' ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "THname"
ColumnTextBox.HeaderText = "THname"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
' ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
End Sub
Dim i As Integer = 0
Private Sub ckBox_CheckedChanged()
For j As Integer = 0 To Me.DataGridView1.RowCount - 1
Me.DataGridView1(0, j).Value = True
Next
End Sub
Public Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub
End Class
Tag : Ms SQL Server 2005, Ms SQL Server 2008, VB.NET, VS 2008 (.NET 3.x), VS 2010 (.NET 4.x)
connetionString.Open()
str = "SELECT * FROM Business WHERE username like = '" & username.Text & ""
If str <> "" Then
str = str & " username Like '%" & username.Text & "%' "
End If
DataGridView1.DataSource = ds
dtAdapter = New SqlDataAdapter(str, Conn)
'dtAdapter.Fill(dt)
dt = ds.Tables(str)
connetionString.Close()
strConnString = "Data Source=PONGWICS2014-PC; Initial Catalog=Topdata1;User ID=sa;Password=Pong"
objConn = New SqlConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT * FROM Business WHERE 1=1 "
If strKeyWord <> "" Then
strSQL = strSQL & " AND username Like '%" & strKeyWord & "%' "
End If