当前位置:编程学习 > C#/ASP.NET >>

初学者写一个数据库类,请大家提提意见

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace FsVbBoyLinkDataBaseLibrary
{
    
    public class LinkClass:Attribute 
    {
       
        private DataTable NewTable;
        private DataSet NewDataSet;
        private SqlConnection SqlConn;
        private SqlCommand SqlCom;
        private SqlDataAdapter SqlApapter;
        private SqlCommandBuilder SqlCmdBuilder;
        
        private string LinkStr;
        public bool State ;
        //LinkClass的构造函数
        public LinkClass()
        {
        }
           //获得数据库连接字符串
        public virtual string LinkString
        {
            get
            {
                return LinkStr;
            }
            set
            {
             this.LinkStr  = value;
            }
        }
        //获得数据表
        public virtual DataTable GetDataTable(string selectStr)
        {
            if (this.LinkDataBase() == true)
            {
                this.SqlApapter = new SqlDataAdapter(selectStr, this.SqlConn);
                this.NewTable = new DataTable();
                this.SqlApapter.Fill(this.NewTable);
                this.SqlConn.Close();
                return this.NewTable;
            }
            else
            {
                return null;
            }
        }
        //获得数据集
        public virtual DataSet GetDataSet(string selectStr, string tableName)
        {
            if (this.LinkDataBase() == true)
            {
                this.SqlApapter = new SqlDataAdapter(selectStr, this.SqlConn);
                this.NewDataSet = new DataSet();
                this.SqlApapter.Fill(this.NewDataSet, tableName);
                this.SqlConn.Close();
                return this.NewDataSet;
            }
            else
            {
                return null;
            }
        }
        //更新数据表,当表有主键时可用
        public virtual bool UpDateTable(string updateStr, DataTable changeTable)
        {
            if (this.LinkDataBase() == true)
            {
                this.SqlApapter = new SqlDataAdapter(updateStr, this.SqlConn);
                this.SqlCmdBuilder = new SqlCommandBuilder(this.SqlApapter);
                if (this.SqlApapter.Update(changeTable) != 0)
                {
                    this.SqlConn.Close();
                    return true;
                }
                else
                {
                    this.SqlConn.Close();
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        //更新数据集,当表有主键时可用
        public virtual bool UpDataDataSet(string updateStr, DataSet changeDataSet)
        {
            if (this.LinkDataBase() == true)
            {
                this.SqlApapter = new SqlDataAdapter(updateStr, this.SqlConn);
                this.SqlCmdBuilder = new SqlCommandBuilder(this.SqlApapter);
                if (this.SqlApapter.Update(changeDataSet) != 0)
                {
                    this.SqlConn.Close();
                    return true;
                }
                else
                {
                    this.SqlConn.Close();
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        //直接对数据库进行数据操作
        public virtual bool CmdModiyDataBase(string modiyStr)
        {
            if (this.LinkDataBase() == true)
            {
                this.SqlCom = this.SqlConn.CreateCommand();
                this.SqlCom.CommandText = modiyStr ;
                this.SqlCom.CommandType = CommandType.Text;
                if (this.SqlCom.ExecuteNonQuery() != 0)
                {
                    this.SqlConn.Close();
                    return true;
                }
                else
                {
                    this.SqlConn.Close();
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
     
        //返回字符类型,数据库中的第一值
        public virtual string DataReaderScalar(string readerStr)
        {
            if (this.LinkDataBase() == true)
            {
                this.SqlCom = this.SqlConn.CreateCommand();
                this.SqlCom.CommandText = readerStr;
                this.SqlCom.CommandType = CommandType.Text;
                return this.SqlCom.ExecuteScalar().ToString();
            }
            else
            {
                return "";
            }
        }
        //返回一个SqlDataReader对象
        public virtual SqlDataReader DataReader (string selectStr)
        {
            if (this.LinkDataBase() == true)
            {
                this.SqlCom = this.SqlConn.CreateCommand();
                this.SqlCom.CommandText = selectStr;
                this.SqlCom.CommandType = CommandType.Text;
                return this.SqlCom.ExecuteReader();
             }
               
            else
            {
                return null;
            }
        }
        //连接数据库
        private bool LinkDataBase()
        {
            this.SqlConn = new SqlConnection(this.LinkStr );
            this.SqlConn.Open();
            if (this.SqlConn.State == ConnectionState.Open)
            {
                this.State = true;
                return this.State;
            }

            else
            {
                this.State = false;
                return this.State;
            }
          
          
        }
        // 获得数据库的连接状态
        public string GetLinkState
        {
            get
            {
                return this.SqlConn.State.ToString(); ;
            }
        }
        //关闭数据库的连接,释放资源
        public bool CloseDataBaseLink(bool state)
        {
            this.SqlConn.Close();
            return true;
        }
    }
}

--------------------编程问答-------------------- 顶一下!请大家支持一下 --------------------编程问答-------------------- 再顶一下,请大家支持一下,提一下意见! --------------------编程问答-------------------- 比我利害多了,我看到代码有些头痛,代码老是学不好 --------------------编程问答-------------------- 神仙啊。。。。。。
首先,你写代码注释不够详细;其次,你的代码可读性非常糟糕;再次,没有满足功能原子性,我该怎么称呼你的类?数据库连接类?数据库查询类? --------------------编程问答-------------------- 那这位大侠,那该如何写呢?对类的设计该看什么书,还有,你能贴一段代码,让我接荐一下,让我这个菜鸟学习学习!谢谢你的意见! --------------------编程问答-------------------- 顶一下! --------------------编程问答-------------------- 最起码的,你的代码的缩进要符合规范。不要说你一句,你就这样,谢谢。 --------------------编程问答--------------------
using System;

class IfApp
{
    public static int Main(string[] args)
    {
        //如果命令行参数小于一个,则显示程序正确用法,退出程序 
        if (args.Length < 1)
        {
            Console.WriteLine("Usage:   ifApp   char");
            return 1;
        }

        //获得第一个命令行参数第一个字母 
        //把该字母赋给变量chLetter 
        char chLetter = args[0][0];

        //如果字母大于等于字符'A' 
        if (char.IsUpper(chLetter))
        {
            Console.WriteLine("{0}   是个大写字母", chLetter);
            return 0;

        }

        //如果字母界与字符'a'和'z'之间 
        //则该字母为小写字母 
        if (char.IsLower(chLetter))
        {
            Console.WriteLine("{0}   是个小写字母", chLetter);
            return 0;
        }

        //如果字母为数字 
        if (Char.IsDigit(chLetter))
        {
            Console.WriteLine("{0}   是个数字", chLetter);
            return 0;
        }

        //缺省地(以上条件都不符合),则该字母为特殊字符 
        Console.WriteLine("{0}   是个特殊字符", chLetter);
        return 1;
    }
}
上面代码可以写成这样
--------------------编程问答-------------------- 抱歉,贴错了
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,