2016-12-13 11 views
0

select * from variableクエリでRequest.QueryStringを変数に設定するにはどうすればよいですか?ここで変数からselect *内の変数にRequest.QueryStringを設定する方法

は、私はそれをやろうとしています方法です:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conn %>" SelectCommand="SELECT [pm1], [s1], [i1], [j1] FROM ([pk1] = @pk1)"> 
    <SelectParameters> 
     <asp:QueryStringParameter Name="pk1" QueryStringField="pk1" DbType = "String" Direction = "Input" /> 
    </SelectParameters> 
</asp:SqlDataSource> 

をしかし、これは私に次のエラー与える:あなたのクエリ内のQueryString値を取得するために

Incorrect syntax near '='.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '='.

+0

SQLの定義は間違っています: 'SELECT FROM table'は正しいですが、' SELECT fields FROM table = param1'ではありません。 何か不足していますか? – Marco

+0

私はテーブルからURLのパラメータを使用したい – ragnat

答えて

0

使用このスニペットを。

//get the value from the querystring 
string qsValue = Request.QueryString["pk1"]; 

//create a new SqlDataSource instance 
SqlDataSource source = new SqlDataSource(); 

//add the connectionstring to the source 
source.ConnectionString = ConfigurationManager.ConnectionStrings["conn"].ToString(); 

//create the query with the selectparameter 
source.SelectCommand = "SELECT s1, i1, j1 FROM myTable WHERE (pk1 = @pk1)"; 

//replace the parameter with the value from the querystring 
source.SelectParameters.Add("pk1", qsValue); 

//bind the gridview 
GridView1.DataSource = source; 
GridView1.DataBind(); 
関連する問題