我正在设计一个允许用户使用信息更新数据库的网站。我遇到的问题是我创建了各种控件,这些控件在页面加载时隐藏,但用户可以单击取消隐藏以添加其他信息。
我遇到的问题是编写sql语句来处理识别使用的控件。我有一个viewstate计数让我知道哪些面板是可见的。
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 server是2008年
感谢任何帮助
答案 0 :(得分:0)
我可能错了,但在第二个陈述中,VALUES
条款出现了两次。
这可能是你问题的根源。
答案 1 :(得分: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
请注意批处理中的分号语句终止符。
答案 2 :(得分:0)
如上所述,但是如果前面的字符串是不可变的,那么第6个案例你可能想要重新设计它以检查> =每个值
答案 3 :(得分:0)
问题不是选择案例,而是视图状态没有正确传递给sql字符串,我知道可能不是最好的解决方案,但将label.text传递给隐藏文本框,sql字符串能够正确读取。理查德建议使用连接的IF语句而不是Select Case是一个好主意!
感谢所有帮助