字符串或二进制数据将被截断。该语句已终止

时间:2011-07-04 09:47:22

标签: asp.net sql vb.net

Protected Sub cmdOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        Dim medID, medName, comment As String
        Dim power, period, qtty As Integer
        'Dim nextDateOfDelivery As Date = getNextDateOfDelivery(Val(txtDays.Text))
        Dim today As System.DateTime
        Dim answer As System.DateTime
        today = System.DateTime.Now
        answer = today.AddDays(Val(txtDays.Text))
        Dim nextDateOfDelivery As Date = answer


        medName = Trim(txtMedName.Text)
        qtty = Val(txtQuantity.Text)
        comment = txtComment.Text
        power = txtPower.Text
        medID = lblMedID.Text
        period = Val(txtDays.Text)
        '............setting priorities
        '.......value of p(0) indicates priority for tablest and so on so forth.....
        Dim p() As Integer = {0, 0, 0, 0}
        For i = 0 To LBPriorities.Items.Count - 1
            Select Case LBPriorities.Items(i).Text
                Case "Tablet"
                    p(0) = i + 1
                Case "Capsule"
                    p(1) = i + 1
                Case "Liquid"
                    p(2) = i + 1
                Case "Injection"
                    p(3) = i + 1

            End Select

        Next

        '............if not available
        Dim strictlyThis As String = radNotAvailable.SelectedItem.Value
        Dim connStr As String = ConfigurationManager.ConnectionStrings("databaseConnectionString").ConnectionString

        Dim con As New SqlClient.SqlConnection(connStr)
        'Dim con As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;" & _
        '                                       "AttachDbFilename=|DataDirectory|\database.mdf;" & _
        '                                       "Integrated Security=True;User Instance=True")

        Dim sql As New SqlClient.SqlCommand("INSERT INTO medicine" & _
            "(medID, userID, medName, quantity, dateOfOrder, power, tabPriority, capPriority, liqPriority, " & _
            "injPriority, period, notAvailable, prescription, comment, nextDateOfDelivery)" & _
            "VALUES('" & medID & "','" & User.Identity.Name & "','" & medName & "'," & qtty & ",'" & today & "'," & power & _
            "," & p(0) & "," & p(1) & "," & p(2) & "," & p(3) & ",'" & period & "','" & strictlyThis & "', '' ,'" & comment & "','" & nextDateOfDelivery & "')", con)

        con.Open()
        sql.ExecuteNonQuery()
        con.Close()
        con.Dispose()

        lblMessage.Text = "The details of the medicine have been saved. You may upload a prescription"
        FUPrescription.Enabled = True
        cmdUpload.Enabled = True
    End Sub

这就是我现在正在使用的...但我得到一个错误说“字符串或二进制数据将被截断。 声明已被终止。“这是什么意思?我的sql语句有什么问题???

1 个答案:

答案 0 :(得分:2)

我会说这是你在dateOfOrder中存储的价值。

根据您的屏幕截图,这是一个VarChar(10),但您在那里存储DateTime.Now.ToString(),其默认值的格式为:

5/1/2008 6:32:06 PM

以19个字符出现。

我建议您将日期和时间存储在more suitable field中,例如DateDateTime字段。

如果您必须将其存储为文本,那么您至少应将其存储在format that is suitable for sorting中,并且明确无误,例如:

DateTime.Now.ToString("o") // Round-trip format

哪个会给你:

2008-06-15T21:15:07.0000000