码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-07-10 01:18:14      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:exec   zip   new   weight   ring   obj   let   client   system   

原文发布时间为: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();
    }

}

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

标签:exec   zip   new   weight   ring   obj   let   client   system   

原文地址:http://www.cnblogs.com/handboy/p/7143822.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!