首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

ASP.NET 给数据库查询字符串平添参数

2012-12-30 
ASP.NET 给数据库查询字符串添加参数string id dr[PostID].ToString()//保存Post的IDSQLServer SQLRe

ASP.NET 给数据库查询字符串添加参数


string id = dr["PostID"].ToString();//保存Post的ID
SQLServer SQLResponse = new SQLServer();
string ResponseSQLString = "SELECT ResponseTable.Responser,ResponseTable.ResponseContent FROM ResponseTable WHERE ReponseTable.PostID = @id";//这是查询字符串

但是应该如何将id这个变量的值添加到字符串中呢?
我的SQL类的SELECT方法是如下实现:

public DataTable select(string Connectionstring, string sql)
    {
        SqlConnection conn = new SqlConnection(Connectionstring);
        DataTable dt = new DataTable();
        SqlDataAdapter adpter = new SqlDataAdapter(sql,conn);
        try
        {
            conn.Open();
            adpter.Fill(dt);
        }
        catch (SqlException)
        { }
        finally {
            conn.Close();
        }
        return dt;
    }

用的是Adapter,请问如何改SQL类能把变量加到字符串中去?
[解决办法]

SqlCommand command = connection.CreateCommand();
            command.CommandText = "sql query";
            command.Parameters.Add(new SqlParameter("@id", "value"));
            SqlDataAdapter adpter = new SqlDataAdapter(command);

热点排行