Asp.net 在页面加载中传递SQL数据源变量?

Asp.net 在页面加载中传递SQL数据源变量?,asp.net,sql,Asp.net,Sql,我有一个SQL查询,但它只知道页面加载中的电影类型。这将连接到数据列表 我的问题是 select * from movies where movieType = @type 有没有办法在运行时将参数传递给sql数据源 谢谢在进行SQL调用时,您需要将参数添加到SqlCommand中 myCommand.Parameters.AddWithValue("@type", type); 在本例中,从.Net传入的变量为“type” 这将有助于理解参数传递。进行SQL调用时,需要将参数添加到SqlC

我有一个SQL查询,但它只知道页面加载中的电影类型。这将连接到数据列表

我的问题是

select * from movies where movieType = @type
有没有办法在运行时将参数传递给sql数据源


谢谢

在进行SQL调用时,您需要将参数添加到SqlCommand中

myCommand.Parameters.AddWithValue("@type", type);
在本例中,从.Net传入的变量为“type”


这将有助于理解参数传递。

进行SQL调用时,需要将参数添加到SqlCommand中

myCommand.Parameters.AddWithValue("@type", type);
在本例中,从.Net传入的变量为“type”

这应该有助于您理解参数传递。

如果您想了解,可以使用使用参数的SQL查询和语句

例如:

var source = new SqlDataSource(myConnectionString, 
     "SELECT * FROM TableName WHERE ColName = @M);
source.SelectParameters.Add("@M", DbType.Int32, ddl.SelectedValue);
MSDN在这个主题上做了很多工作。

如果您想了解,可以使用SQL查询和使用参数的语句

例如:

var source = new SqlDataSource(myConnectionString, 
     "SELECT * FROM TableName WHERE ColName = @M);
source.SelectParameters.Add("@M", DbType.Int32, ddl.SelectedValue);
MSDN已对此主题进行了详细说明。

这可以在标记或代码隐藏中完成

 <asp:SqlDataSource
      id="SqlDataSource1"
      runat="server"
      DataSourceMode="DataReader"
      ConnectionString="<%$ ConnectionStrings:MyConnection %>"
      SelectCommand="select * from movies where movieType = @type">

       <SelectParameters>
            <asp:ControlParameter name="type" ControlID="MyDropdownList" PropertyName="SelectedValue" />
       </SelectParameters>
  </asp:SqlDataSource>

这可以在标记或代码隐藏中完成

 <asp:SqlDataSource
      id="SqlDataSource1"
      runat="server"
      DataSourceMode="DataReader"
      ConnectionString="<%$ ConnectionStrings:MyConnection %>"
      SelectCommand="select * from movies where movieType = @type">

       <SelectParameters>
            <asp:ControlParameter name="type" ControlID="MyDropdownList" PropertyName="SelectedValue" />
       </SelectParameters>
  </asp:SqlDataSource>

也与问题相关:Select方法将返回数据行的IEnumerable列表。所以请记住。。。代码happilyAlso与问题相关:Select方法将返回数据行的IEnumerable列表。所以请记住。。。愉快地编码