 |
|
รบกวนช่วยเกี่ยวกับ การ ลบ แต่ไม่ลบออกจากฐานข้อมูลหน่อยคะ ใช้ Ms Sql2005 กับ VB.net 2008 ค่ะ |
|
 |
|
|
 |
 |
|
รบกวนผู้รู้ช่วยเรื่องการลบข้อมูลแต่ไม่ลบจากฐานข้อมูลหน่อยคะ
ได้ยินมาเกี่ยวกับเรื่อง การใช้ สถานะ เช่น 1 , 0
คือไม่เข้าใจคะว่า จะ ทำยังไงให้ ฟิลด์ที่สร้างขึ้นมา มีแค่ค่า 1, 0 ด้วยการ code หรือ ใช้ tool ค่ะ
และการตั้งค่า default ของสถานะให้เป็น 1 ต้องทำยังไงคะ
ใช้ Ms Sql2005 กับ VB.net 2008 ค่ะ
รบกวนแนะแนวทาง หรือ ตัวอย่างโค้ด ด้วยนะคะ หาข้อมูลแล้วไม่เข้าใจเลยคะ
>> ได้แนวทางมาจาก https://www.thaicreate.com/dotnet/forum/051756.html ค่ะ แต่ของหนูเองไม่ได้ทำ checkbox คะ คลิกที่ปุ่ม delete เลย
Code (VB.NET)
Code
Imports System.Data.SqlClient
Public Class EmpForm
Private Sub EmpForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
binddgv()
addCombo()
'ClrText()
'GenID()
End Sub
Public Sub ClrText()
IDTextBox.Text = String.Empty
ComboIns.Text = String.Empty
ComboTitle.Text = String.Empty
EmpNameTextBox.Text = String.Empty
EmpLnameTextBox.Text = String.Empty
AddressTextBox.Text = String.Empty
ProvinceText.Text = String.Empty
ZipcodeText.Text = String.Empty
MobileText.Text = String.Empty
PhoneText.Text = String.Empty
End Sub
Public Sub binddgv() <<<<[u] เงื่อนไขเวลา binding กับ ดาต้ากิด ต้องเพิ่มตรงไหนค่ะ ถ้าเอาแค่ สถานะที่ต้องการ โชว์[/u]
Dim con As New SqlConnection(strCon)
con.Open()
Dim strSql As String = "SELECT Employees.ID,MainInstitute.InsName,Employees.TitleName,Employees.EmpName,Employees.EmpLname,"
strSql = strSql & "Employees.Address,Employees.Province,Employees.Zipcode,Employees.Mobile,Employees.Phone,Employees.Annotation "
strSql = strSql & "FROM Employees,MainInstitute "
strSql = strSql & "WHERE Employees.ID_Ins = MainInstitute.ID_Ins "
Dim command As New SqlCommand(strSql, con)
Dim adapter As New SqlDataAdapter(command)
Dim ds As New DataSet()
adapter.Fill(ds, "EmpIns")
EmpDG.DataSource = ds.Tables("EmpIns")
Dim headers() As String = {"รหัส", "หน่วยงาน", "คำหน้าชื่อ", "ชื่อ", "นามสกุล", "ที่อยู่", "จังหวัด", "รหัสไปรษณีย์", "เบอร์มือถือ", "เบอร์โทรศัพท์", "หมายเหตุ"}
For i = 0 To headers.Length - 1
EmpDG.Columns(i).HeaderText = headers(i)
Next
con.Close()
End Sub
Public Sub addCombo() 'เอาค่าจาก ตาราง MainInstitute ใส่ลง combobox
Dim con As New SqlConnection(strCon)
con.Open()
Dim sql As String = "SELECT * FROM MainInstitute"
Dim command As New SqlCommand(sql, con)
Dim adapter As New SqlDataAdapter(command)
Dim ds As New DataSet()
adapter.Fill(ds, "MainInstitute")
ComboIns.DataSource = ds.Tables("MainInstitute")
'เก็บเป็นรหัสลง เเต่โชว์เป็น ชื่อ
ComboIns.DisplayMember = "InsName"
ComboIns.ValueMember = "ID_Ins"
End Sub
Public Sub AutoID()
Dim con As New SqlConnection(strCon)
con.Open()
Dim strsql As String = String.Empty
strsql = "select max(ID) + 1 as ID from Employees"
Dim da As New SqlDataAdapter()
da.SelectCommand = New SqlCommand(strsql, con)
Dim ds As New DataSet()
Dim rid As Integer = 0
da.Fill(ds)
If (ds.Tables(0).Rows.Count > 0) Then
If ds.Tables(0).Rows(0)("ID").ToString() <> String.Empty Then
rid = CInt(ds.Tables(0).Rows(0)("ID").ToString())
Else
rid = 1
End If
Else
rid = 1
End If
IDTextBox.Text = rid.ToString("00")
End Sub
Private Sub btnAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAll.Click
binddgv()
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim con As New SqlConnection(strCon)
con.Open()
If TextBox1.Text = String.Empty Then
MessageBox.Show("กรุณากรอกชื่อที่ต้องการค้นหา", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
TextBox1.Focus()
End If
Dim strsql As String
strsql = "SELECT Employees.ID,MainInstitute.InsName,Employees.TitleName,Employees.EmpName,Employees.EmpLname,Employees.Address,Employees.Province,Employees.Zipcode,Employees.Mobile,Employees.Phone"
strsql = strsql & " FROM Employees,MainInstitute Where Employees.ID_Ins=MainInstitute.ID_Ins and Employees.EmpName LIKE '%" & TextBox1.Text & "%'"
Dim command As New SqlCommand(strsql, con)
Dim adapter As New SqlDataAdapter(command)
Dim data As New DataSet()
adapter.Fill(data, "SearchName")
EmpDG.DataSource = data.Tables("SearchName")
con.Close()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
ClrText()
AutoID()
End Sub
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
If ComboIns.Text.Trim() = String.Empty Then
MessageBox.Show("กรุณาระบุหน่วยงาน", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
ComboIns.Focus()
Exit Sub
End If
If ComboTitle.Text.Trim() = String.Empty Then
MessageBox.Show("กรุณาระบุคำหน้าชื่อ", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
ComboTitle.Focus()
Exit Sub
End If
If EmpNameTextBox.Text.Trim() = String.Empty Then
MessageBox.Show("กรุณากรอกชื่อ", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
EmpNameTextBox.Focus()
Exit Sub
End If
If EmpLnameTextBox.Text.Trim() = String.Empty Then
MessageBox.Show("กรุณากรอกนามสกุล", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
EmpLnameTextBox.Focus()
Exit Sub
End If
If AddressTextBox.Text.Trim() = String.Empty Then
MessageBox.Show("กรุณากรอกที่อยู่", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
AddressTextBox.Focus()
Exit Sub
End If
If ProvinceText.Text.Trim() = String.Empty Then
MessageBox.Show("กรุณากรอกจังหวัด", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
ProvinceText.Focus()
Exit Sub
End If
If ZipcodeText.Text.Trim() = String.Empty Then
MessageBox.Show("กรุณากรอกรหัสไปรษณีย์", "แจ้งเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning)
ZipcodeText.Focus()
Exit Sub
End If
If MessageBox.Show("คุณต้องการเพิ่มพนักงานชื่อ " & EmpNameTextBox.Text & " ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Dim con As New SqlConnection(strCon)
con.Open()
Dim sqlAdd As String = "INSERT INTO Employees(ID,ID_Ins,TitleName,EmpName,EmpLname,Address,Province,Zipcode,Mobile,Phone)VALUES"
sqlAdd = sqlAdd & "('" & IDTextBox.Text & "','" & ComboIns.SelectedValue & "','" & ComboTitle.Text & "','" & EmpNameTextBox.Text & "','" & EmpLnameTextBox.Text & "','" & AddressTextBox.Text & "','" & ProvinceText.Text & "','" & ZipcodeText.Text & "','" & MobileText.Text & "','" & PhoneText.Text & "')"
Dim command As New SqlCommand(sqlAdd, con)
Try
command.ExecuteNonQuery()
con.Close()
MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
binddgv()
ClrText()
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim con As New SqlConnection(strCon)
con.Open()
If MessageBox.Show("คุณต้องการแก้ไขข้อมูลพนักงานชื่อ " & EmpNameTextBox.Text & " ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
IDTextBox.Enabled = True
Dim sqlEdit As String = " UPDATE Employees SET "
sqlEdit = sqlEdit & "ID_Ins ='" & ComboIns.SelectedValue & "',"
sqlEdit = sqlEdit & "TitleName='" & ComboTitle.Text & "', "
sqlEdit = sqlEdit & "EmpName='" & EmpNameTextBox.Text & "', "
sqlEdit = sqlEdit & "EmpLname='" & EmpLnameTextBox.Text & "', "
sqlEdit = sqlEdit & "Address='" & AddressTextBox.Text & "', "
sqlEdit = sqlEdit & "Province='" & ProvinceText.Text & "', "
sqlEdit = sqlEdit & "Zipcode='" & ZipcodeText.Text & "', "
sqlEdit = sqlEdit & "Mobile='" & MobileText.Text & "', "
sqlEdit = sqlEdit & "Phone= '" & PhoneText.Text & "' "
sqlEdit = sqlEdit & "WHERE ID = '" & IDTextBox.Text & "' "
Dim command As New SqlCommand(sqlEdit, con)
Try
command.ExecuteNonQuery()
con.Close()
MessageBox.Show("แก้ไขข้อมูลเรียบร้อยเเล้ว")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
binddgv()
ClrText()
End Sub
ส่วนของปุ่ม Delete ทำไว้ คร่าวๆ คะ T_T
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MessageBox.Show("คุณต้องการลบข้อมูลพนักงานชื่อ " & EmpNameTextBox.Text & " ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Dim con As New SqlConnection(strCon)
con.Open()
Dim sql As String = "UPDATE Employees SET Status ='Inactive' WHERE ID = '" & IDTextBox.Text & "' "
Dim command As New SqlCommand(sql, con)
Try
command.ExecuteNonQuery()
con.Close()
MessageBox.Show("ลบข้อมูลเรียบร้อยเเล้ว")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
binddgv()
ClrText()
End Sub
Private Sub EmpDG_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles EmpDG.CellClick
If e.RowIndex > -1 Then
IDTextBox.Text = EmpDG.Rows(e.RowIndex).Cells(0).Value.ToString()
ComboIns.Text = EmpDG.Rows(e.RowIndex).Cells(1).Value.ToString()
ComboTitle.Text = EmpDG.Rows(e.RowIndex).Cells(2).Value.ToString()
EmpNameTextBox.Text = EmpDG.Rows(e.RowIndex).Cells(3).Value.ToString()
EmpLnameTextBox.Text = EmpDG.Rows(e.RowIndex).Cells(4).Value.ToString()
AddressTextBox.Text = EmpDG.Rows(e.RowIndex).Cells(5).Value.ToString()
ProvinceText.Text = EmpDG.Rows(e.RowIndex).Cells(6).Value.ToString()
ZipcodeText.Text = EmpDG.Rows(e.RowIndex).Cells(7).Value.ToString()
MobileText.Text = EmpDG.Rows(e.RowIndex).Cells(8).Value.ToString()
PhoneText.Text = EmpDG.Rows(e.RowIndex).Cells(9).Value.ToString()
End If
End Sub
End Class
Tag : .NET, Ms SQL Server 2005, Win (Windows App), VB.NET
|
|
 |
 |
 |
 |
Date :
2012-03-07 03:56:29 |
By :
นางสาวฝึกหัด |
View :
1931 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รบกวน ผู้รู้ช่วยเหลือหน่อยนะคะ
|
 |
 |
 |
 |
Date :
2012-03-07 03:59:36 |
By :
นางสาวฝึกหัด |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลอง เอา texbox.text= sql
แล้วได้แบบนี้อ่ะคะ
UPDATE Employees SET Status ='Inactive' WHERE ID = '1'
|
 |
 |
 |
 |
Date :
2012-03-07 15:46:00 |
By :
นางสาวฝึกหัด |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หรือว่า ต้องเพิ่มเงื่อนไข ในส่วนของ การ binding กับ ดาต้ากิด วิวที่ทำไว้รึเปล่า คะ
ควรทำยังไงดีคะ
Code
Public Sub binddgv() <<<< เงื่อนไขเวลา binding กับ ดาต้ากิด ต้องเพิ่มตรงไหนค่ะ ถ้าเอาแค่ สถานะที่ต้องการ โชว์
Dim con As New SqlConnection(strCon)
con.Open()
Dim strSql As String = "SELECT Employees.ID,MainInstitute.InsName,Employees.TitleName,Employees.EmpName,Employees.EmpLname,"
strSql = strSql & "Employees.Address,Employees.Province,Employees.Zipcode,Employees.Mobile,Employees.Phone,Employees.Annotation "
strSql = strSql & "FROM Employees,MainInstitute "
strSql = strSql & "WHERE Employees.ID_Ins = MainInstitute.ID_Ins "
Dim command As New SqlCommand(strSql, con)
Dim adapter As New SqlDataAdapter(command)
Dim ds As New DataSet()
adapter.Fill(ds, "EmpIns")
EmpDG.DataSource = ds.Tables("EmpIns")
Dim headers() As String = {"รหัส", "หน่วยงาน", "คำหน้าชื่อ", "ชื่อ", "นามสกุล", "ที่อยู่", "จังหวัด", "รหัสไปรษณีย์", "เบอร์มือถือ", "เบอร์โทรศัพท์", "หมายเหตุ"}
For i = 0 To headers.Length - 1
EmpDG.Columns(i).HeaderText = headers(i)
Next
con.Close()
End Sub
Public Sub addCombo() 'เอาค่าจาก ตาราง MainInstitute ใส่ลง combobox
Dim con As New SqlConnection(strCon)
con.Open()
|
 |
 |
 |
 |
Date :
2012-03-07 15:50:29 |
By :
นางสาวฝึกหัด |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|