0

我正在设计一个允许用户使用信息更新数据库的网站。我遇到的问题是我创建了在页面加载时隐藏的各种控件,但用户可以单击取消隐藏以添加其他信息。

我遇到的问题是编写 sql 语句来处理识别使用的控件。我有一个视图状态计数,让我知道哪些面板可见或不可见。

ViewState("count") = CInt(ViewState("count")) + 1
    Label1.Text = ViewState("count").ToString()

然后后面的代码编写我的 SQL 查询看起来像这样

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim myConn As SqlConnection
    Dim cmd As SqlCommand
    Dim sqlstring As String
    Dim dd As String
    Dim mm As String
    Dim yy As String


    dd = Left(TextBox1.Text, 2)
    mm = Mid(TextBox1.Text, 4, 2)
    yy = Right(TextBox1.Text, 4)


    myConn = New SqlConnection("xxx")

    myConn.Open()

    Select Case Label1.Text
        Case Is = "0"
            sqlstring = "INSERT INTO Transactions (Date, Account, Payee, Chq_Num, Reference, GST_Rate, Amount, Document_Number, Bank_Account) VALUES ('" & yy & "-" & mm & "-" & dd & "','" & DropDownList3.SelectedValue & "','" & TextBox3.Text & "','" & TextBox5.Text & "','" & TextBox4.Text & "'," & DropDownList2.SelectedValue & "," & TextBox6.Text & "," & TextBox27.Text & ",'" & DropDownList1.SelectedValue & "')"
        Case Is = "1"
            sqlstring = "INSERT INTO Transactions (Date, Account, Payee, Chq_Num, Reference, GST_Rate, Amount, Document_Number, Bank_Account) VALUES ('" & yy & "-" & mm & "-" & dd & "','" & DropDownList3.SelectedValue & "','" & TextBox3.Text & "','" & TextBox5.Text & "','" & TextBox4.Text & "'," & DropDownList2.SelectedValue & "," & TextBox6.Text & "," & TextBox27.Text & ",'" & DropDownList1.SelectedValue & "') VALUES ('" & yy & "-" & mm & "-" & dd & "','" & DropDownList4.SelectedValue & "','" & TextBox7.Text & "','" & TextBox9.Text & "','" & TextBox8.Text & "'," & DropDownList5.SelectedValue & "," & TextBox10.Text & "," & TextBox27.Text & ",'" & DropDownList1.SelectedValue & "')"
    End Select


    cmd = New SqlCommand(sqlstring, myConn)
    cmd.ExecuteNonQuery()

    myConn.Close()

    Response.Redirect(Request.RawUrl, True)

End Sub

我的问题在于选择案例,因为当案例为 1 时,它只插入第一个值而不是第二个集合。我最多有 6 个“案例”可供选择,每次都添加一组额外的值。sql服务器是2008

任何帮助表示赞赏

4

4 回答 4

0

从 SQL Server 2008 开始,您可以使用如下所示的多值插入子句

INSERT [INTO] tbl [(...columns...)]
VALUES (....),(....),(....);

您已经构建了这样的查询

INSERT [INTO] tbl [(...columns...)]
VALUES (....)
VALUES (....)
VALUES (....)

我个人也会使用 SELECT-CASE 块构建字符串连接:

If Label1.Text >= "0" Then
        sqlstring = "('" & yy & "-" & mm & "-" & dd & "','" & DropDownList3.SelectedValue & "','" & TextBox3.Text & "','" & TextBox5.Text & "','" & TextBox4.Text & "'," & DropDownList2.SelectedValue & "," & TextBox6.Text & "," & TextBox27.Text & ",'" & DropDownList1.SelectedValue & "')"
End If
If Label1.Text >= "1" Then
        sqlstring = sqlstring + "," + "('" & yy & "-" & mm & "-" & dd & "','" & DropDownList4.SelectedValue & "','" & TextBox7.Text & "','" & TextBox9.Text & "','" & TextBox8.Text & "'," & DropDownList5.SelectedValue & "," & TextBox10.Text & "," & TextBox27.Text & ",'" & DropDownList1.SelectedValue & "')"
End If
' etc

如果由于某种神秘的原因,多值语句不起作用,您始终可以依赖经过验证的真实多语句批处理,即

If Label1.Text >= "0" Then
    sqlstring = "INSERT ... VALUES (...);"
End If
If Label1.Text >= "1" Then
    sqlstring = sqlstring + "INSERT ... VALUES (...);"
End If
If Label1.Text >= "2" Then
    sqlstring = sqlstring + "INSERT ... VALUES (...);"
End If
' etc

请注意批处理中的分号语句终止符。

于 2013-04-20T11:28:43.810 回答
0

如上所述,但是在第 6 种情况下,您最终会得到一个非常长的字符串

于 2013-04-20T11:30:37.797 回答
0

我可能错了,但在第二个语句中,该子句VALUES出现了两次。这可能是你问题的根源。

于 2013-04-20T11:27:11.947 回答
0

问题不是选择案例,而是视图状态没有正确传递给 sql 字符串,我知道可能不是最好的解决方案,而是将 label.text 传递给了一个隐藏的文本框,sql 字符串能够正确读取该文本框。Richard 建议使用串联的 IF 语句而不是 Select Case 是一个好主意!

感谢所有的帮助

于 2013-04-21T17:34:18.350 回答