分层开发应用(简单工厂设计模式,数据访问层代码)
Code File A
using
System;
using
System.Data;
using
System.Data.SqlClient;
namespace
Test.DATA
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**//// <summary>
/// DataManage 的摘要说明。
/// </summary>
public class DemoSqlDB
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string strSQL;
//与SQL Server的连接字符串设置
//private string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath("DataBase.mdb"); //data.mdb是你的数据库名称
private string connectionString = "user id=sa;password=sa;initial catalog=John;data source=127.0.0.1;Connect Timeout=30";
private SqlConnection myConnection;
private SqlCommandBuilder sqlCmdBld;
private SqlDataAdapter da;
private DataSet ds = new DataSet();
public DemoSqlDB()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
public DemoSqlDB(string conStr)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
connectionString = conStr;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**///创建了该类的实例时直接用
//根据输入的SQL DML检索数据库数据,返回填充了数据的DataSet
public DataSet SelectDataBase(string tempStrSQL, string tempTableName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.strSQL = tempStrSQL;
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(this.strSQL, this.myConnection);
this.ds.Clear();
this.da.Fill(ds, tempTableName);
return ds;
}
//更新(传DataSet和DataTable的对象),返回更新了的数据库表
public DataSet UpdateDataBase(DataSet changedDataSet, string tableName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(this.strSQL, this.myConnection);
this.sqlCmdBld = new SqlCommandBuilder(da);
this.da.Update(changedDataSet, tableName);
return changedDataSet;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**/////未创建该类的实例时直接用
//检索(传字符串SQL DML)
public DataTable SelectDataBase(string tempStrSQL)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.myConnection = new SqlConnection(connectionString);
DataSet tempDataSet = new DataSet();
this.da = new SqlDataAdapter(tempStrSQL, this.myConnection);
this.da.Fill(tempDataSet);
return tempDataSet.Tables[0];
}
//更新(传字符串SQL DML),返回受影响的行数
public int UpdateDataBase(string tempStrSQL)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand(tempStrSQL);
myCommand.Connection = myConnection;
myConnection.Open();
int intNumber = myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
myConnection.Close();
return intNumber;
}
}
}
Code File B
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Data;
namespace
Test.DATA
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//Base Class///
/// <summary>
/// 简单工厂设计模式
/// </summary>
public abstract class DataExchange
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public DemoSqlDB DemoSqlDB;
public abstract int AddBusiness(); //添加数据
public abstract int ChangeBusiness();//修改更新数据
public abstract DataSet GetBusiness();//查询数据
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//Sub Class
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
#region
public class UsersInfo : DataExchange
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//添加信息内容字段
public string userName = null;
public string passWords = null;
public UsersInfo()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//构造函数 实现LfSqlDB实例化
DemoSqlDB= new DemoSqlDB();
}
public override int AddBusiness()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DemoSqlDB.UpdateDataBase("insert");
}
public override int ChangeBusiness()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DemoSqlDB.UpdateDataBase("update");
}
public override DataSet GetBusiness()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DemoSqlDB.SelectDataBase("select", "tableName");
}
![](/Images/OutliningIndicators/InBlock.gif)
public void UpdatePassword()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DemoSqlDB.UpdateDataBase("update");
}
public DataSet GetUserInfo()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DemoSqlDB.SelectDataBase("select", "tableName");
}
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
#region
public class MoneyInfo : DataExchange
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//添加信息内容字段
public int id = null;
public string titles = null;
private static string suib = null; //系统参数 某些设置用
public MoneyInfo()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//构造函数 实现LfSqlDB实例化
DemoSqlDB= new DemoSqlDB();
}
public override int AddBusiness()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DemoSqlDB.UpdateDataBase();
}
public override int ChangeBusiness()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DemoSqlDB.UpdateDataBase();
}
public override DataSet GetBusiness()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DemoSqlDB.SelectDataBase();
}
public DataSet GetNewMenoyInfo()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DemoSqlDB.SelectDataBase();
}
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
}