首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

asp.net的数据库操作类文件 SqlOperate

Posted on 2009-09-17 08:25  唐糖  阅读(967)  评论(4编辑  收藏  举报

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// SqlOperate 的摘要说明
/// </summary>
public class SqlOperate
{
 public SqlOperate()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
 }
SqlConnection sqlconn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);

 //通过sqlcommand对象执行所传递的SQL语句   
public void DataCom(string sqlstr)      {
        sqlconn.Open();
        SqlCommand sqlcom = new SqlCommand(sqlstr,sqlconn);
        sqlcom.ExecuteNonQuery();
        sqlconn.Close();
    }
//主要执行sqldataadapter中的sql语句,并将结果绑定到gridview控件上
  public void gvDataBind(GridView gv, string sqlstr)
    {
        sqlconn.Open();
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr,sqlconn);
        DataSet myds = new DataSet();
        myda.Fill(myds);
        gv.DataSource = myds;
        gv.DataBind();
        sqlconn.Close();
    }
//主要执行sqldataadapter中的sql语句,并将结果绑定到datalist控件上
  public void dlDataBind(DataList dl, string sqlstr)
   qlconn.Open();
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlconn);
        DataSet myds = new DataSet();
        myda.Fill(myds);
        dl.DataSource = myds;
        dl.DataBind();
        sqlconn.Close();
    }
//主要执行sqldataadapter中的sql语句,并将结果绑定到dropdownlist控件上
    public void ddlDataBind(DropDownList ddl, string sqlstr,string dvf )
      sqlconn.Open();
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlconn);
        DataSet myds = new DataSet();
        myda.Fill(myds);
        ddl.DataSource = myds;
        ddl.DataValueField = dvf;
        ddl.DataBind();
        sqlconn.Close();
    }
}