Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportdata.Click
Using con As New SqlConnection("data source=tnhkwg-wf;initial catalog=MedicalExpense;uid=sa;pwd=password")
con.Open()
Dim tr As SqlTransaction = con.BeginTransaction
Dim cm As New SqlCommand("insert into MT_Employee(EMP_CODE,EMP_NAME,EMP_SECTION,EMP_WORK,EMP_TYPE,ST_FATHER,ST_MOTHER) values (@a,@b,@c,@d,@e,@f,@g)", con, tr)
With cm
.Parameters.Add("@a", SqlDbType.Int)
.Parameters.Add("@b", SqlDbType.NVarChar)
.Parameters.Add("@c", SqlDbType.NVarChar, 50)
.Parameters.Add("@d", SqlDbType.NVarChar, 50)
.Parameters.Add("@e", SqlDbType.NVarChar, 50)
.Parameters.Add("@f", SqlDbType.NVarChar, 50)
.Parameters.Add("@g", SqlDbType.NVarChar, 50)
'.Parameters.Add("@h", SqlDbType.NVarChar, 50)
''.Parameters.Add("@i", SqlDbType.NVarChar, 50)
'.Parameters.Add("@i", SqlDbType.NVarChar, 50)
End With
For i As Integer = 0 To DataGridView1.RowCount - 1
If Not DataGridView1.Rows(i).IsNewRow Then
cm.Parameters("@a").Value = DataGridView1.Rows(i).Cells(0).Value
cm.Parameters("@b").Value = DataGridView1.Rows(i).Cells(1).Value
cm.Parameters("@c").Value = DataGridView1.Rows(i).Cells(2).Value
cm.Parameters("@d").Value = DataGridView1.Rows(i).Cells(3).Value
cm.Parameters("@e").Value = DataGridView1.Rows(i).Cells(4).Value
cm.Parameters("@f").Value = DataGridView1.Rows(i).Cells(5).Value
cm.Parameters("@g").Value = DataGridView1.Rows(i).Cells(6).Value
'cm.Parameters("@h").Value = DataGridView2.Rows(i).Cells(7).Value
'cm.Parameters("@i").Value = DataGridView2.Rows(i).Cells(8).Value
cm.ExecuteNonQuery()
'Label1.Text = ""
End If
Next
tr.Commit()
Windows.Forms.MessageBox.Show("บันทึกการแก้ไขข้อมูลเรียบร้อยแล้ว", "Database", Windows.Forms.MessageBoxButtons.OK)
DataGridView1.DataSource = dt
End Using
End Sub
Function CheckEMP(emp As String) As Boolean
Dim result As Boolean
Dim con As New SqlConnection("data source=tnhkwg-wf;initial catalog=MedicalExpense;uid=sa;pwd=password")
con.Open()
Dim cmd As New SqlCommand("SELECT EMP_CODE FROM MT_Employee WHERE EMP_CODE like '%" & emp & "%'", con)
Dim count As Integer = cmd.ExecuteNonQuery
result = IIf(count <= 0, False, True)
con.Close()
Return result
End Function
แล้วเปลี่ยน แบบนี้
Code (VB.NET)
Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportdata.Click
Using con As New SqlConnection("data source=tnhkwg-wf;initial catalog=MedicalExpense;uid=sa;pwd=password")
con.Open()
Dim tr As SqlTransaction = con.BeginTransaction
Dim cm As New SqlCommand("insert into MT_Employee(EMP_CODE,EMP_NAME,EMP_SECTION,EMP_WORK,EMP_TYPE,ST_FATHER,ST_MOTHER) values (@a,@b,@c,@d,@e,@f,@g)", con, tr)
With cm
.Parameters.Add("@a", SqlDbType.Int)
.Parameters.Add("@b", SqlDbType.NVarChar)
.Parameters.Add("@c", SqlDbType.NVarChar, 50)
.Parameters.Add("@d", SqlDbType.NVarChar, 50)
.Parameters.Add("@e", SqlDbType.NVarChar, 50)
.Parameters.Add("@f", SqlDbType.NVarChar, 50)
.Parameters.Add("@g", SqlDbType.NVarChar, 50)
'.Parameters.Add("@h", SqlDbType.NVarChar, 50)
''.Parameters.Add("@i", SqlDbType.NVarChar, 50)
'.Parameters.Add("@i", SqlDbType.NVarChar, 50)
End With
For i As Integer = 0 To DataGridView1.RowCount - 1
If CheckEMP(DataGridView1(0,i).Value.ToString()) = True Then
cm.Parameters("@a").Value = DataGridView1(0,i).Value
cm.Parameters("@b").Value = DataGridView1(1,i).Value
cm.Parameters("@c").Value = DataGridView1(2,i).Value
cm.Parameters("@d").Value = DataGridView1(3,i).Value
cm.Parameters("@e").Value = DataGridView1(4,i).Value
cm.Parameters("@f").Value = DataGridView1(5,i).Value
cm.Parameters("@g").Value = DataGridView1(6,i).Value
'cm.Parameters("@h").Value = DataGridView2(7,i).Value
'cm.Parameters("@i").Value = DataGridView2(8,i).Value
cm.ExecuteNonQuery()
'Label1.Text = ""
End If
Next
tr.Commit()
Windows.Forms.MessageBox.Show("บันทึกการแก้ไขข้อมูลเรียบร้อยแล้ว", "Database", Windows.Forms.MessageBoxButtons.OK)
DataGridView1.DataSource = dt
End Using
End Sub
Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportdata.Click
Using con As New SqlConnection("data source=tnhkwg-wf;initial catalog=MedicalExpense;uid=sa;pwd=password")
con.Open()
Dim tr As SqlTransaction = con.BeginTransaction
Dim cm As New SqlCommand("insert into MT_Employee(EMP_CODE,EMP_NAME,EMP_SECTION,EMP_WORK,EMP_TYPE,ST_FATHER,ST_MOTHER) values (@a,@b,@c,@d,@e,@f,@g)", con, tr)
With cm
.Parameters.Add("@a", SqlDbType.Int)
.Parameters.Add("@b", SqlDbType.NVarChar)
.Parameters.Add("@c", SqlDbType.NVarChar, 50)
.Parameters.Add("@d", SqlDbType.NVarChar, 50)
.Parameters.Add("@e", SqlDbType.NVarChar, 50)
.Parameters.Add("@f", SqlDbType.NVarChar, 50)
.Parameters.Add("@g", SqlDbType.NVarChar, 50)
'.Parameters.Add("@h", SqlDbType.NVarChar, 50)
''.Parameters.Add("@i", SqlDbType.NVarChar, 50)
'.Parameters.Add("@i", SqlDbType.NVarChar, 50)
End With
For i As Integer = 0 To DataGridView1.RowCount - 1
If Not String.IsNullOrEmpty(DataGridView1(0, i).Value.ToString()) And CheckEMP(DataGridView1(0, i).Value.ToString()) = False Then
cm.Parameters("@a").Value = DataGridView1(0, i).Value
cm.Parameters("@b").Value = DataGridView1(1, i).Value
cm.Parameters("@c").Value = DataGridView1(2, i).Value
cm.Parameters("@d").Value = DataGridView1(3, i).Value
cm.Parameters("@e").Value = DataGridView1(4, i).Value
cm.Parameters("@f").Value = DataGridView1(5, i).Value
cm.Parameters("@g").Value = DataGridView1(6, i).Value
'cm.Parameters("@h").Value = DataGridView2(7,i).Value
'cm.Parameters("@i").Value = DataGridView2(8,i).Value
cm.ExecuteNonQuery()
'Label1.Text = ""
End If
Next
tr.Commit()
Windows.Forms.MessageBox.Show("บันทึกการแก้ไขข้อมูลเรียบร้อยแล้ว", "Database", Windows.Forms.MessageBoxButtons.OK)
DataGridView1.DataSource = dt
End Using
End Sub
Function CheckEMP(emp As String) As Boolean
Dim result As Boolean
Dim con As New SqlConnection("data source=tnhkwg-wf;initial catalog=MedicalExpense;uid=sa;pwd=password")
con.Open()
Dim cmd As New SqlCommand("SELECT EMP_CODE FROM MT_Employee WHERE EMP_CODE like '%" & emp & "%'", con)
result = IIf(cmd.ExecuteReader().HasRows(), True, False)
con.Close()
Return result
End Function