简单的ASP.NET三层架构数据库后台

来源:互联网 发布:mac暂存盘已满怎么办 编辑:程序博客网 时间:2024/04/30 12:17

 

 

简单的ASP.NET三层架构数据库后台

 

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace xxxxx
{
    
public class xxxx
    
{
        
private SqlConnection objSqlConn; 
        
private SqlCommand objSqlCmd;
        
private SqlAdapter objSqlAdapter;
                                           
private SqlTransaction sqlTrans;
        
private bool c_connect;
        
private strint s_connect;
                                        

        
        
public xxx():this(Configuration.AppSettings["xxx"]) 
        
{

        }

        
        
public xxx(string str)
        
{
            
if(s_connect==null)
            
{
                s_connect
=str;
                c_connect
=true;
            }

            
this.DataBaseOpen();
        }

        
public bool DataBaseOpen()              //连接数据库
        {
            
if(c_connect)
            
{
                
try
                
{
                    
if(objSqlConn==null)
                    
{
                        objSqlConn
=new SqlConnection(s_connect);
                        objSqlConn.Open();
                    }

                    
if(objSqlCmd==null)
                    
{
                        objSqlCmd
=new SqlCommand();
                                
                    }

                    objSqlCmd.Connection
=objSqlConn;
                }

                
catch(SqlExection ex)
                
{
                    
return flase;
                    
throw ex;
                }


            }

            
return true;

        }

        
        
public bool Execute(string strSql)       //执行数据库操作增删改查
        {
            
bool flag;
            
if(!DataBaseOpen())
            
{
                             
throw new Application("没有和数据库建立连接");
            }

            objSqlCmd.CommandType
=CommandType.Text;
            objSqlCmd.CommandText
=strSql;
            
try
            
{
                
int index=objSqlCmd.ExecuteNonQuery()
                
if(index==1)
                
{
                    flag
=true;
                }

                
else
                
{
                    flag
=false;
                }

            }

            
catch(SqlExection ex)
            
{
                flag
=false;
                
throw ex;
            }

            
return flag;
        }

        
        
public void StartTransation()        //开始事务
        {
        
            
if (!DataBaseOpen())
            
{
                
throw(new ApplicationException("没有建立数据库连接。"));
            }

        
            
            sqlTrans
=sqlCn.BeginTransaction();
            sqlCmd.Transaction
=sqlTrans;

            
        }

        
public bool Commit()
        
{
            
if(this.flagEx)
            
{
                
try
                
{
                    sqlTrans.Commit();          
//提交事务
            
                }

                
catch(SqlException ex)
                
{
                    sqlTrans.Rollback();       
// 回滚事务
                    return false;
                    
                }

                
return true;
            }

            
            
else
            
{
                sqlTrans.Rollback();
                
return false;
            }


            
            
        }



        
public DataSet GetData(string strSql,string strTable)   //数据集
        {
            DataSet ds
=new DataSet();
            objSqlAdapter
=new SqlDataAdapter(strSql,objSqlConn);
            ds
=objSqlAdapter.Fill(ds,strTable);
            
return ds;
        }

        
        
public void GetDisponse()   //回收
        {
            objSqlCmd.Disponse();
            objSqlAdapter.Disponse();
            objSqlConn.Disponse();
            objSqlCmd
=null;
            objSqlAdapter
=null;
            objSqlConn
=null;
            
        
        }


        



    }



}

还 要前台的一个配置文件App.config

用于写连接数据库字符串

 

App.config

<? xml version=1.0 encoding=UTF-8>
<configuration>
<appSettings>
<add key="xxx" value="Data Source=主机;Initial Catalog=数据库名称;uid=x;pwd=x" >
</add>
</appSettings>
</configuration>

key="xxx"于后台Configuration.AppSettings["xxx"]

xxx要相同了. 

 

原创粉丝点击