zoukankan      html  css  js  c++  java
  • 对象数据源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();
        }

    }

  • 相关阅读:
    十几个remote control software
    chromedriver bug
    跟我一起学ruby (转)
    ruby两套教程
    java把函数作为参数传递
    java如何在函数中调用主函数的数组
    Java HashSet和LinkedHashSet的用法
    java文件读写操作
    RAID详细介绍
    Apriori算法
  • 原文地址:https://www.cnblogs.com/handboy/p/7143822.html
Copyright © 2011-2022 走看看