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