Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
strsql = "insert into OrderDetail(OrderID,ProductID,NumberOrder,UnitID,Weight,UnitWeightID,Driver,RegisDriver) values(@OrderId,@ProductID,@ProductName,@NumberOrder,@UnitID,@Weight,@UnitWeightID,@Driver,@RegisDriver)"
objCmd = New System.Data.SqlClient.SqlCommand(strsql, DbConn)
For i As Integer = 0 To DataGridOrder.RowCount - 1
If Not DataGridOrder.Rows(i).IsNewRow Then
With objCmd
.Parameters.Add(New SqlParameter("@OrderId", orderID.Text))
.Parameters.Add(New SqlParameter("@ProductID", DataGridOrder.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@NumberOrder", DataGridOrder.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@UnitId", DataGridOrder.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@Weight", DataGridOrder.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@UnitWeightID", DataGridOrder.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@Driver", Driver.Text))
.Parameters.Add(New SqlParameter("@RegisDriver", RegisDriver.Text))
End With
End If
Next
objCmd.ExecuteNonQuery()
MessageBox.Show("บันทึกข้อมูลแล้ว")
DbConn.Close()
DataGridOrder.Rows.Clear()
End Sub
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
strsql = "insert into OrderDetail(OrderID,ProductID,NumberOrder,UnitID,Weight,UnitWeightID,Driver,RegisDriver) values(@OrderId,@ProductID,@ProductName,@NumberOrder,@UnitID,@Weight,@UnitWeightID,@Driver,@RegisDriver)"
objCmd = New System.Data.SqlClient.SqlCommand(strsql, DbConn)
For i As Integer = 0 To DataGridOrder.RowCount - 1
If Not DataGridOrder.Rows(i).IsNewRow Then
With objCmd
.Parameters.Add(New SqlParameter("@OrderId", orderID.Text))
.Parameters.Add(New SqlParameter("@ProductID", DataGridOrder.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@NumberOrder", DataGridOrder.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@UnitId", DataGridOrder.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@Weight", DataGridOrder.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@UnitWeightID", DataGridOrder.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@Driver", Driver.Text))
.Parameters.Add(New SqlParameter("@RegisDriver", RegisDriver.Text))
End With
objCmd.ExecuteNonQuery()
objCmd.Parameters.Clear()
End If
Next
MessageBox.Show("บันทึกข้อมูลแล้ว")
DbConn.Close()
End Sub
หรือ
Code (VB.NET)
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
strsql = "insert into OrderDetail(OrderID,ProductID,NumberOrder,UnitID,Weight,UnitWeightID,Driver,RegisDriver) values(@OrderId,@ProductID,@ProductName,@NumberOrder,@UnitID,@Weight,@UnitWeightID,@Driver,@RegisDriver)"
For i As Integer = 0 To DataGridOrder.RowCount - 1
If Not DataGridOrder.Rows(i).IsNewRow Then
objCmd = New System.Data.SqlClient.SqlCommand(strsql, DbConn)
With objCmd
.Parameters.Add(New SqlParameter("@OrderId", orderID.Text))
.Parameters.Add(New SqlParameter("@ProductID", DataGridOrder.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@NumberOrder", DataGridOrder.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@UnitId", DataGridOrder.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@Weight", DataGridOrder.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@UnitWeightID", DataGridOrder.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@Driver", Driver.Text))
.Parameters.Add(New SqlParameter("@RegisDriver", RegisDriver.Text))
End With
objCmd.ExecuteNonQuery()
End If
Next
MessageBox.Show("บันทึกข้อมูลแล้ว")
DbConn.Close()
DataGridOrder.Rows.Clear()
End Sub
If myCon.State = ConnectionState.Open Then
myCon.Close()
End If
myCon.ConnectionString = strConn
myCon.Open()
strSql = "Insert Into employees (emp_id,emp_name,emp_address,emp_phone,emp_gender,Status,Username,Password) values(@id,@emp_name,@emp_address,@emp_phone,@emp_gender,@Status,@Username,@Password)"
objCmd = New System.Data.SqlClient.SqlCommand(strSql, myCon)
For i As Integer = 0 To dgvEmp.RowCount - 1
If Not dgvEmp.Rows(i).IsNewRow Then
objCmd = New System.Data.SqlClient.SqlCommand(strSql, myCon)
With objCmd
.Parameters.Add(New SqlParameter("@id", txtId.Text))
.Parameters.Add(New SqlParameter("@emp_name", dgvEmp.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@emp_address", dgvEmp.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@emp_phone", dgvEmp.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@emp_gender", dgvEmp.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@Status", dgvEmp.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@Username", dgvEmp.Rows(i).Cells(5).Value))
.Parameters.Add(New SqlParameter("@Password", dgvEmp.Rows(i).Cells(6).Value))
End With
objCmd.ExecuteNonQuery()
End If
Next
MessageBox.Show("บันทึกข้อมุลแล้ว")
myCon.Close()
dgvEmp.Rows.Clear()
Date :
2015-02-27 14:14:10
By :
onjira
No. 10
Guest
Quote:
Code (VB.NET)
Imports System.Data.SqlClient
Public Class Form1
Public employee_ID As String
Dim AddItemValue As New List(Of keypair)
' Dim AddItemDate As New List(Of keypair)
Dim gender As String '
' Dim vardate As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
setddgender()
End Sub
Private Sub setddGender()
AddItemValue.Add(New keypair("ชาย", "M"))
AddItemValue.Add(New keypair("หญิง", "F"))
For Each AddItemValues As keypair In AddItemValue
ComboBox1.Items.Add(AddItemValues.Name)
Next
End Sub
Private Sub ctrecord_Click(sender As Object, e As EventArgs) Handles ctrecord.Click
Dim Con As New conn()
Dim strCon As New SqlConnection(Con.strConn)
strCon.Open()
Dim datadate As String = ""
Dim Conm As New SqlCommand("insemployee_data", strCon)
Conm.CommandType = CommandType.StoredProcedure
Conm.Parameters.Add("@employee", SqlDbType.VarChar, 50).Value = TextBox1.Text.Trim
Conm.Parameters.Add("@name", SqlDbType.VarChar, 100).Value = TextBox2.Text.Trim
Conm.Parameters.Add("@startdate", SqlDbType.VarChar).Value = DateTimePicker1.Text.Trim
Conm.Parameters.Add("@department", SqlDbType.VarChar, 50).Value = TextBox3.Text.Trim
Conm.Parameters.Add("@sex", SqlDbType.VarChar, 50).Value = ComboBox1
Conm.Parameters.Add("@phon", SqlDbType.VarChar, 50).Value = TextBox4.Text.Trim
Conm.ExecuteNonQuery()
strCon.Close()
clear_text()
viewemployee.Show()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim SelectedText As String = ComboBox1.Items(ComboBox1.SelectedIndex).ToString
For Each AddItemValues As keypair In AddItemValue
If SelectedText = AddItemValues.Name Then
gender = AddItemValues.KeyName
End If
Next
End Sub
Private Sub clear_text()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
DateTimePicker1.Text = ""
ComboBox1.Text = ""
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
viewemployee.Show()
End Sub
End Class
ผมใช้วิธีนี้ครับ
'//////////////// สำหรับการแสดงผลใน datagrid ครับ
Dim sqlstr As String = "SELECT * FROM Table"
Dim cs As New SqlConnection
Dim adp as SqlDataAdapter
adp = New SqlDataAdapter(sqlstr, cs)
adp.Fill(ds, "tablename")
adp.FillSchema(ds, SchemaType.Source, "tablename")
Me.DataGridView1.DataSource = ds
Me.DataGridView1.DataMember = "tablename"
'////////////// สำหรับ update ข้อมูลจาก datagrid ลง database ครับ ตอบสนองต่อ event ที่ต้องการนะครับ
Dim cmdbuilder As New SqlCommandBuilder(adp)
Dim i As Integer
Try
i = adp.Update(ds, "tablename")
MsgBox("Records Updated= " & i)
Catch ex As Exception
MsgBox(ex.Message)
End Try