欢迎访问我的博客 https://javascript.shop

对象数据源objectdatasource的使用,类的编写实现查询增删改的方法

原文发布时间为:2008-08-01 —— 来源于本人的百度文章 [由搬家工具导入]

using System;
using System.Data;
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;

using System.Data.SqlClient;

/// <summary>
/// 对象数据源的使用例子
/// </summary>
public class db
{
    private SqlConnection con;
    private SqlCommand cmd;
    private SqlDataAdapter sda;
    private DataSet ds;
    private SqlDataReader sdr;

    public db()
{
        con = new SqlConnection(ConnString());
        cmd = new SqlCommand();
        sda = new SqlDataAdapter();
        ds = new DataSet();
}

    public static string ConnString()
    {
        return ConfigurationManager.ConnectionStrings["pubsConn"].ConnectionString;
    }

    public void clear()
    {
        if (con.State != ConnectionState.Closed)
        {
            con.Close();
            con.Dispose();
        }
        if (cmd != null)
            cmd.Dispose();
        if (sda != null)
            sda.Dispose();
        if (ds != null)
            ds.Dispose();
        if (sdr != null)
        {
            sdr.Close();
            sdr.Dispose();
        }
    }

    public DataTable dt(string query)
    {
        sda.SelectCommand = new SqlCommand(query, con);
        sda.Fill(ds, "temp");
        return ds.Tables["temp"];
    }
    public DataTable dt()
    {
        sda.SelectCommand = new SqlCommand("select top(5) * from authors", con);
        sda.Fill(ds, "temp");
        return ds.Tables["temp"];
    }

    public DataSet TdataSet(string query)
    {
        sda.SelectCommand = new SqlCommand(query, con);
        sda.Fill(ds, "temp");
        return ds;
    }

    public DataSet TdataSet()
    {
        sda.SelectCommand = new SqlCommand("select top(5) * from authors", con);
        sda.Fill(ds, "temp");
        return ds;
    }

    public SqlDataReader reader(string query)
    {
        cmd.CommandText = query;
        cmd.Connection = con;
        con.Open();
        sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return sdr;
    }

    public SqlDataReader reader()
    {
        cmd.CommandText = "select top(5) au_lname,zip,au_id from authors";
        cmd.Connection = con;
        con.Open();
        sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return sdr;
    }

    public void Update( string au_lname, string zip,string au_id)
    {
       
        //cmd.CommandText = "update authors set au_lname='" + au_lname + "',zip='" + zip + "' where au_id='"+au_id+"'";
       
        cmd.CommandText = "update authors set where 这种书写可以防止更新输入单引号时出错
        cmd.Parameters.AddWithValue("@name", au_lname);
        cmd.Parameters.AddWithValue("@zip", zip);
        cmd.Parameters.AddWithValue("@id", au_id);

        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
    }

    public void Delete(string au_id)
    {
        cmd.CommandText = "delete from authors where au_id='" + au_id + "'";
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
    }

}

posted @ 2017-07-10 00:48  孑孓子  阅读(181)  评论(0编辑  收藏  举报
欢迎访问我的博客 https://javascript.shop