我在封装数据库操作的类中是这样写该函数的:
public SqlConnection SqlCon;
public SqlCommand SqlCom;
public SqlDataAdapter SqlDa;
public SqlDataReader SqlDr;
public string sqlconnection = string.Empty;
#region 执行T-SQL语句,返回object
public object ObjSql(string mysql)
{
object i;
SetConnection();
SqlCom = SqlCon.CreateCommand();
SqlCom.CommandText = mysql;
this.ConnOpen();
i = SqlCom.ExecuteScalar();
this.ConnClose();
return i;
}
#endregion
引用该函数的.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
//数据帮定
public void BindGrid()
{
int ID = Convert.ToInt16(Request.QueryString[ "id "].ToString());
string sign = "% ";
string sqlstr = string.Empty;
//根据主页面提交过来的搜索条件,进行相应的数据帮定
if (ID == 1)
{
string keywords = Request.QueryString[ "keys "].ToString();
string style = Request.QueryString[ "style "].ToString();
//此处QG_STflInfo为视图
sqlstr = "select * from articeMemuView ";
sqlstr += " where ISPublic=1 and " + style + " like ' " + sign + keywords + sign + " ' ";