首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SqlDataSource SelectParameters代码背后

SqlDataSource SelectParameters代码背后
EN

Stack Overflow用户
提问于 2015-02-21 16:02:01
回答 1查看 3.7K关注 0票数 0

我有一个SqlDataSource用这个SelectCommand给ListBox喂食

代码语言:javascript
运行
复制
<asp:SqlDataSource ID="DSPatients"
    ConnectionString="<%$ ConnectionStrings:CSMain %>"
    ProviderName="<%$ ConnectionStrings:CSMain.ProviderName %>"
    SelectCommand="SELECT Id, dbo.PatientDescriptive(Id) Descriptive FROM Patient ORDER BY Id"
    ...
    runat="server">

<asp:ListBox Rows="10" ID="patientsList" DataSourceID="DSPatients" DataValueField="Id" DataTextField="Descriptive" AutoPostBack="false" runat="server" />

效果很好。

我有这个TextBox

代码语言:javascript
运行
复制
<asp:TextBox ID="tbPatient" runat="server" MaxLength="100" />

还有一个搜索按钮

代码语言:javascript
运行
复制
<asp:Button ID="btnSearch" runat="server" Text="Search" 
     OnClick="btnSearch_Click" CausesValidation="false" />

现在,我想修改SelectCommand,以便当用户单击btnSearch按钮时,它只搜索病人名称喜欢 (传递到tbPatient文本框中)。

为了做到这一点,我查看了后面的代码并尝试:

代码语言:javascript
运行
复制
protected void btnSearch_Click(object sender, EventArgs e)
{
    DSPatients.SelectParameters.Add("Ptrn", tbPatient.Text);
    DSPatients.SelectCommand = "SELECT Id, dbo.PatientDescriptive(Id) Descriptive FROM Patient WHERE Name LIKE @Ptrn ORDER BY Id";
    DSPatients.Select(DataSourceSelectArguments.Empty); // Is the problem here? What I have to put inside?
    DSPatients.SelectParameters.Clear();
}

当我运行时,我会得到以下错误:

异常详细信息: System.Data.SqlClient.SqlException:必须声明标量变量"@Ptrn“。

我想要的ListBox,显示,只有病人的名字,像一个输入文本框。我怎么才能解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2015-05-12 14:32:19

不知道这是不是问题所在,但是这个,

代码语言:javascript
运行
复制
DSPatients.SelectParameters.Add("Ptrn", tbPatient.Text);

参数应该包括@符号,如下所示

代码语言:javascript
运行
复制
DSPatients.SelectParameters.Add("@Ptrn", tbPatient.Text);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28647797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档