如何将查询字符串值传递给ASP.NET中的select语句?

时间:2012-10-19 02:08:17

标签: asp.net sql ado.net

如何将查询字符串值传递给ASP.NET中的select语句?

这是我尝试过的:

{ 
    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(); 
}

1 个答案:

答案 0 :(得分:3)

使用SqlCommand并将参数添加到其中:

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