使用vb.net中的基本日期选择器将数据插入数据库mysql

时间:2013-04-26 01:13:21

标签: asp.net vb.net

我要将数据插入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

2 个答案:

答案 0 :(得分:0)

这一行看起来不对:

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

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

答案 1 :(得分:0)

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

将其更改为

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

因为您想要传递值