01.
Private
Sub
Button1_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
Button1.Click
02.
Dim
sqlConnection
As
SqlConnection
03.
Dim
sqlSelectCommand
As
SqlCommand
04.
Dim
sqlInsertCommand
As
SqlCommand
05.
06.
Dim
WorkerCode
As
String
07.
08.
Dim
sqlConnectionString
As
String
09.
sqlConnectionString =
"Database=table;Data Source=localhost;User Id=root;Password=admin"
10.
sqlConnection =
New
SqlConnection(sqlConnectionString)
11.
12.
Dim
sqlSelectCommandString
As
String
13.
sqlSelectCommandString =
"Select Sum(Income) As [Income], [WorkerCode] From [Table_Work] Group By [WorkerCode] Order By [Income] ASC"
14.
sqlSelectCommand =
New
SqlCommand(sqlSelectCommandString, sqlConnection)
15.
16.
Try
17.
Dim
DtWorker
As
DataTable
18.
DtWorker =
New
DataTable
19.
20.
Dim
sqlDataAdapter
As
SqlDataAdapter
21.
sqlDataAdapter =
New
SqlDataAdapter(sqlSelectCommand)
22.
sqlDataAdapter.Fill(DtWorker)
23.
24.
WorkerCode = DtWorker.Rows(0)(
"WorkerCode"
)
25.
Catch
ex
As
Exception
26.
MessageBox.Show(ex.Message,
"error"
, MessageBoxButtons.OK, MessageBoxIcon.
Error
)
27.
End
Try
28.
29.
Dim
sqlInsertCommandString
As
String
30.
sqlInsertCommandString =
"Insert Into [Table_Work] ([WorkName], [InCome], [WorkerCode]) Values (@WorkName, @InCome, @WorkerCode)"
31.
sqlInsertCommand =
New
SqlCommand(sqlInsertCommandString, sqlConnection)
32.
sqlInsertCommand.Parameters.AddWithValue(
"@WorkName"
, TextBoxWorkName.Text)
33.
sqlInsertCommand.Parameters.AddWithValue(
"@InCome"
,
Double
.Parse(TextBoxInCome.Text))
34.
sqlInsertCommand.Parameters.AddWithValue(
"@WorkerCode"
, WorkerCode)
35.
36.
Try
37.
sqlConnection.Open()
38.
sqlInsertCommand.ExecuteNonQuery()
39.
sqlConnection.Close()
40.
Catch
ex
As
Exception
41.
MessageBox.Show(ex.Message,
"error"
, MessageBoxButtons.OK, MessageBoxIcon.
Error
)
42.
End
Try
43.
End
Sub