gpt4 book ai didi

asp.net - 将值从文本框中传递给 SQL 查询参数

转载 作者:行者123 更新时间:2023-12-04 06:15:02 24 4
gpt4 key购买 nike

我正在使用以下代码连接到我的数据库。

Dim conStr As String = "Data Source=.\SQLEXPRESS;    ![enter image description here][1]
AttachDbFilename=|DataDirectory|\dbTest.mdf;
Integrated Security=True;
User Instance=True"

Dim sqlQry As String = "SELECT * FROM [tblTest] WHERE ([Name] = @Name)"
Dim dAdt As New SqlDataAdapter(sqlQry, conStr)
Dim dSet As New DataSet()

然后填充适配器
dAdt.Fill(dSet, "tblTest")

and then I can use the data the way I want.

My question is: How to pass the value of parameter that user will give through a text box on my webform.

Dim sqlQry As String = "SELECT * FROM [tblTest] <b>WHERE ([Name] = @Name)

我的意思是如何将参数值传递给我的查询?
请修改我的代码以告诉我如何操作。
非常感谢。
enter image description here

最佳答案

您需要添加参数对象并将您的代码放入 Button 的 Click 处理程序或您想要使用的任何其他事件中。 (阅读@cjk 帖子)。

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click

Dim conStr As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbTest.mdf;Integrated Security=True;User Instance=True"
Dim sqlQry As String = "SELECT * FROM [tblTest] WHERE ([Name] = @Name)"
Dim Cn As New SqlConnection(conStr)
Dim SelectCmd As New SqlCommand(sqlQry, Cn)
SelectCmd.Parameters.AddWithValue("@Name", txtName.Text)
'Or
'SelectCmd.Parameters.Add("@Name",System.Data.SqlDbType.VarChar).Value=value_here
Dim dAdt As New SqlDataAdapter(SelectCmd)

Dim dSet As New DataSet
dAdt.Fill(dSet, "tblTest")
......
End Sub

关于asp.net - 将值从文本框中传递给 SQL 查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7359643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com