Imports MySql.Data.MySqlClient
Public Class e3jobinsert
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer
Dim datMonday As Date
Dim sqlConnection As MySqlConnection
Dim sqlInsertCommand As MySqlCommand
Dim sqlInsertCommandString As String
Dim sqlConnectionString As String
sqlConnectionString = "Database=carproject;Data Source=localhost;User Id=root;Password=admin; Allow Zero Datetime=True"
sqlConnection = New MySqlConnection(sqlConnectionString)
For x = 0 To 51
sqlConnection.Open()
datMonday = DateAdd("ww", x, CDate("2010/1/11"))
sqlInsertCommandString = "INSERT INTO contract (contract_id, job_type, company_name, vehicle_type, vehicle_amount, start_date, end_date, start_time, end_time, start_place, end_place, amount, day_use) VALUES(@contract_id, 'r' , @company_name, @vehicle_type, @vehicle_amount, @start_date, @end_date, @start_time, @end_time, @start_place, @end_place, amount,'" & datMonday & "')"
sqlInsertCommand = New MySqlCommand(sqlInsertCommandString, sqlConnection)
sqlInsertCommand.Parameters.AddWithValue("@contract_id", TextBox1.Text)
sqlInsertCommand.Parameters.AddWithValue("@company_name", TextBox2.Text)
sqlInsertCommand.Parameters.AddWithValue("@vehicle_type", TextBox3.Text)
sqlInsertCommand.Parameters.AddWithValue("@vehicle_amount", TextBox4.Text)
sqlInsertCommand.Parameters.AddWithValue("@start_date", DateTimePicker1.Value)
sqlInsertCommand.Parameters.AddWithValue("@end_date", DateTimePicker2.Value)
sqlInsertCommand.Parameters.AddWithValue("@start_time", DateTimePicker3.Value)
sqlInsertCommand.Parameters.AddWithValue("@end_time", DateTimePicker4.Value)
sqlInsertCommand.Parameters.AddWithValue("@start_place", TextBox7.Text)
sqlInsertCommand.Parameters.AddWithValue("@end_place", TextBox8.Text)
sqlInsertCommand.Parameters.AddWithValue("@amount", TextBox9.Text)
sqlInsertCommand.ExecuteNonQuery()
sqlConnection.Close()
Next
MessageBox.Show("Insert Completed", "Insertion", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
'Loop through each week of the year
'Start at 0 because for the first week you dont want to add any weeks
' For x = 0 To 51
'Get Monday's date for the week
' datMonday = DateAdd("ww", x, CDate("11/01/2010")) 'Check date for UK/US
'Build your SQL query
' sqlInsertCommandString = "INSERT INTO `TableName` (`DateField`) VALUES('" & datMonday & "')"
'Now run your query to insert that day
'Next
End Sub
End Class