我打算将数据插入mysql。在 mysql 中,格式为:
Device varchar
Quantity varchar
StartDate datetime
EndDate datetime
这是我的代码:
Public Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click
InsertDevice(txtDevice.Text.ToUpper, txtQtt.Text.ToUpper, BasicDatePicker1.DateFormat, BasicDatePicker2.DateFormat)
End Sub
Private Sub InsertDevice(ByVal strAlias As String, ByVal strQtt As String, ByVal dtStart As String, ByVal dtEnd As String)
Dim connectionString As String = "server='...'; user id='...'; password='...'; Database='...'"
Dim sqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection(connectionString)
Dim queryString As String = "INSERT INTO device VALUES " _
& "(@Device, @Quantity, @StartDate, @EndDate) "
Dim sqlCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(queryString, sqlConnection)
sqlCommand.Parameters.Add("@Device", SqlDbType.VarChar).Value = txtDevice.Text
sqlCommand.Parameters.Add("@Quantity", SqlDbType.VarChar).Value = txtQtt.Text
sqlCommand.Parameters.Add("@StartDate", SqlDbType.Date).Value = BasicDatePicker1.DisplayType
sqlCommand.Parameters.Add("@EndDate", SqlDbType.Date).Value = Now()
Try
Dim rowsAffected As Integer = 0
sqlConnection.Open()
rowsAffected = sqlCommand.ExecuteNonQuery
sqlConnection.Close()
Catch ex As Exception
lblError.Text = ex.Message.ToString
End Try