2016-12-13 6 views
0

Wie kann ich Request.QueryString in einer select * from variable Abfrage in Variable setzen?Wie setze ich Request.QueryString in Variable in * aus Variable

Hier ist, wie ich versuche, es zu tun:

<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> 

Aber das gibt mir die folgende Fehlermeldung:

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

Nun, SQL-Definition ist falsch: 'SELECT Felder FROM Tabelle' ist korrekt, aber nicht' SELECT Felder FROM Tabelle = Param1'. Fehle ich etwas? – Marco

+0

Ich möchte Parameter von URL als Tabelle verwenden – ragnat

Antwort

0

Verwenden Sie dieses Snippet die Abfrage-Zeichenfolge-Wert in der Abfrage zu erhalten.

//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(); 
Verwandte Themen