Private Sub txtempid_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtempid.KeyDown
If txtempid.Text.Trim() = "" Then Exit Sub
If e.KeyCode = Keys.Enter Then
sb.Remove(0, sb.Length())
sb.Append("SELECT * FROM Employee")
sb.Append(" WHERE(EmpID=@EmpID)")
' sb.Append(" OR Empname LIKE @Empname)")
Dim sqlEmployee As String
sqlEmployee = sb.ToString
dtEmp = New DataTable
Com = New SqlCommand()
With Com
.Parameters.Clear()
.Parameters.Add("@EmpID", SqlDbType.VarChar).Value = txtempid.Text.Trim()
' .Parameters.Add("@Empname", SqlDbType.VarChar).Value = "%" & txtempid.Text.Trim() & "%"
.CommandText = sqlEmployee
.CommandType = CommandType.Text
.Connection = Conn
dr = .ExecuteReader
If dr.HasRows Then
txtempid.DataBindings.Clear()
txtempfname.DataBindings.Clear()
txtempname.DataBindings.Clear()
txtempsname.DataBindings.Clear()
txtposition.DataBindings.Clear()
dtEmp.Load(dr)
bs.DataSource = dtEmp
txtempid.DataBindings.Add("Text", bs, "EmpID")
txtempfname.DataBindings.Add("Text", bs, "Empfname")
txtempname.DataBindings.Add("Text", bs, "Empname")
txtempsname.DataBindings.Add("Text", bs, "Empsname")
txtposition.DataBindings.Add("Text", bs, "Position")
Else
MessageBox.Show("ไม่มี ข้อมูลนี้ !!!", "ผลการค้นหา", MessageBoxButtons.OK, MessageBoxIcon.Information)
CheckWork()
End If
End With
dr.Close()
End If
End Sub
Private Sub CheckWork()
tr = Conn.BeginTransaction()
Dim sqlRe As String
'Dim dtRe As DataTable
Dim drW As SqlDataReader
Try
sqlRe = sb.ToString
sb.Remove(0, sb.Length)
sb.Append("SELECT Work.EmpID,Work.startwork,Work.date")
sb.Append(" FROM Work,Employee")
sb.Append(" Where (Work.EmpID=Employee.EmpID)")
sb.Append(" AND (Work.EmpID=" & txtempid.Text.Trim() & ")")
sb.Append(" AND (Work.date= " & dtpdate.Value & ")")
With Com
'.Connection = Conn
'.CommandType = CommandType.Text
.Parameters.Clear()
.Parameters.Add("@EmpID", SqlDbType.VarChar).Value = txtempid.Text.Trim()
.CommandText = sqlRe
.Transaction = tr
drW = .ExecuteReader
If drW.HasRows Then
drW.Read()
Else
MessageBox.Show("ไม่มีรายการ !!!", "ผลการค้นหา", MessageBoxButtons.OK, MessageBoxIcon.Information)
sb = New StringBuilder
sb.Append("INSERT INTO Work(EmpID,startwork,endwork,date)")
sb.Append("VALUES(@EmpID,@startwork,@endwork,@date)")
sqlRe = sb.ToString
Com = New SqlCommand
.CommandText = sqlRe
.CommandType = CommandType.Text
.Connection = Conn
.Parameters.Add("@EmpID", SqlDbType.VarChar).Value = txtempid.Text
.Parameters.Add("@startwork", SqlDbType.VarChar).Value = mskin.Text
.Parameters.Add("@endwork", SqlDbType.VarChar).Value = mskout.Text
.Parameters.Add("@date", SqlDbType.DateTime).Value = dtpdate.Value
.ExecuteNonQuery()
End If
dr.Close()
End With
tr.Commit()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Tag : .NET, Ms SQL Server 2008, VBScript, Win (Windows App), VS 2008 (.NET 3.x)