0

如何将查询字符串值传递到 ASP.NET 中的选择语句中?

这是我尝试过的:

{ 
    string myID = (Request.QueryString["ID"] ?? "0").ToString(); 

    SqlConnection con = new SqlConnection
        (ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString); 
    SqlDataAdapter da = new SqlDataAdapter("SELECT ID, Name, Date, Description FROM MyTable     
                                            where ID=@ID", con); 
    DataTable dt = new DataTable(); 
    da.Fill(dt); 
    GV_InlineEditing.DataSource = dt; 
    GV_InlineEditing.DataBind(); 
}
4

1 回答 1

3

使用 aSqlCommand并向其添加参数:

string query = "SELECT ID, Name, Date, Description FROM MyTable where ID=@ID";
var cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@ID", myID);
var da = new SqlDataAdapter(cmd);
于 2012-10-19T02:14:18.603 回答