0

我打算将数据插入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
4

2 回答 2

0
sqlCommand.Parameters.Add("@StartDate", SqlDbType.Date).Value =   BasicDatePicker1.DisplayType

将其更改为

 sqlCommand.Parameters.Add("@StartDate", SqlDbType.Date).Value =   BasicDatePicker1.Value

因为你想传递价值

于 2013-04-26T01:50:37.180 回答
0

这一行看起来不对:

Parameters.Add("@StartDate", SqlDbType.Date).Value = BasicDatePicker1.DisplayType

您可能想在那里传递日期值。

于 2013-04-26T01:26:15.543 回答