【问题标题】:How do I pass a querystring value into a select statement in ASP.NET?如何将查询字符串值传递到 ASP.NET 中的选择语句中?
【发布时间】:2012-10-19 02:08:16
【问题描述】:

如何将查询字符串值传递到 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(); 
}

【问题讨论】:

    标签: asp.net sql ado.net


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      相关资源
      最近更新 更多