ถึงคุณ tungman ครับ
ผมลองcodingได้ประมาณนี้ครับ
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
With conn
Dim conn As New MySqlConnection("Database=table;Data Source=localhost;User Id=root;Password=admin")
If .State = ConnectionState.Open Then .Close()
.Open()
End With
Try
Dim strsql As String = TextBox1.Text
da = New MySqlDataAdapter(strsql, conn)
Dim dt As New DataTable
da.Fill(dt)
Dim row As DataRow = dt.Rows(0)("workercode")
dt.Rows(0)("workercode").ToString()
row("workercode") = "insert workercode from table_work where sum(income) as income asc"
DataGridView1.DataSource = dt
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString, "error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sqlConnection As SqlConnection
Dim sqlSelectCommand As SqlCommand
Dim sqlInsertCommand As SqlCommand
Dim WorkerCode As String
Dim sqlConnectionString As String
sqlConnectionString = "Database=table;Data Source=localhost;User Id=root;Password=admin"
sqlConnection = New SqlConnection(sqlConnectionString)
Dim sqlSelectCommandString As String
sqlSelectCommandString = "Select Sum(Income) As [Income], [WorkerCode] From [Table_Work] Group By [WorkerCode] Order By [Income] ASC"
sqlSelectCommand = New SqlCommand(sqlSelectCommandString, sqlConnection)
Try
Dim DtWorker As DataTable
DtWorker = New DataTable
Dim sqlDataAdapter As SqlDataAdapter
sqlDataAdapter = New SqlDataAdapter(sqlSelectCommand)
sqlDataAdapter.Fill(DtWorker)
WorkerCode = DtWorker.Rows(0)("WorkerCode")
Catch ex As Exception
MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Dim sqlInsertCommandString As String
sqlInsertCommandString = "Insert Into [Table_Work] ([WorkName], [InCome], [WorkerCode]) Values (@WorkName, @InCome, @WorkerCode)"
sqlInsertCommand = New SqlCommand(sqlInsertCommandString, sqlConnection)
sqlInsertCommand.Parameters.AddWithValue("@WorkName", TextBoxWorkName.Text)
sqlInsertCommand.Parameters.AddWithValue("@InCome", Double.Parse(TextBoxInCome.Text))
sqlInsertCommand.Parameters.AddWithValue("@WorkerCode", WorkerCode)
Try
sqlConnection.Open()
sqlInsertCommand.ExecuteNonQuery()
sqlConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub