请耐心看完,分不够再加!这里有一个实体层:
using System;
using System.Data;
namespace BWebShippersDemo
{
public class DataEntity:DataSet
{
public const string ShipperTableName="Shippers";
public const string ShipperID="ShipperID";
public const string CompanyName="CompanyName";
public const string Phone="Phone";
public DataEntity()
{
this.BuildDataTable();
}
private void BuildDataTable()
{
DataTable dt=new DataTable(ShipperTableName);
dt.Columns.Add(ShipperID,typeof(int));
dt.Columns.Add(CompanyName,typeof(string));
dt.Columns.Add(Phone,typeof(string));
this.Tables.Add(dt);
}
}
}然后有相应的数据操作层:
using System;
using System.Data;
using System.Data.SqlClient;
namespace BWebShippersDemo
{
public class DataAccess
{
private SqlCommand selectCommand;
private SqlCommand insertCommand;
private SqlCommand updateCommand;
private SqlCommand deleteCommand;
private SqlDataAdapter dsCommand; private string connString="server=dotnet;database=northwind;integrated security=sspi";
private string selectString="select * from shippers";
private string insertString="insert into shippers (CompanyName,Phone) values (@name,@phone)";
private string updateString="update shippers set CompanyName =@name,Phone=@phone where ShipperID=@id";
private string deleteString="delete from shippers where shipperid=@id"; public DataAccess()
{
dsCommand=new SqlDataAdapter();
dsCommand.TableMappings.Add("Table",DataEntity.ShipperTableName);
}
private SqlCommand GetSelectCommand()
{
if(selectCommand==null)
{
selectCommand=new SqlCommand(selectString,new SqlConnection(connString));
}
return selectCommand;
}
private SqlCommand GetInsertCommand()
{
if(insertCommand==null)
{
insertCommand=new SqlCommand(insertString,new SqlConnection(connString));
insertCommand.Parameters.Add(new SqlParameter("@name",SqlDbType.NVarChar,40));
insertCommand.Parameters.Add(new SqlParameter("@phone",SqlDbType.NVarChar,24));
insertCommand.Parameters["@name"].SourceColumn=DataEntity.CompanyName;
insertCommand.Parameters["@phone"].SourceColumn=DataEntity.Phone;
}
return insertCommand;
}
private SqlCommand GetUpdateCommand()
{
if(updateCommand==null)
{
updateCommand=new SqlCommand(updateString,new SqlConnection(connString));
updateCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4));
updateCommand.Parameters.Add(new SqlParameter("@name",SqlDbType.NVarChar,40));
updateCommand.Parameters.Add(new SqlParameter("@phone",SqlDbType.NVarChar,24));
updateCommand.Parameters["@id"].SourceColumn=DataEntity.ShipperID;
updateCommand.Parameters["@name"].SourceColumn=DataEntity.CompanyName;
updateCommand.Parameters["@phone"].SourceColumn=DataEntity.Phone;
}
return updateCommand;
}
private SqlCommand GetDeleteCommand()
{
if(deleteCommand==null)
{
deleteCommand=new SqlCommand(deleteString,new SqlConnection(connString));
deleteCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4));
deleteCommand.Parameters["@id"].SourceColumn=DataEntity.ShipperID;
}
return deleteCommand;
}
public DataEntity GetShippersList()
{
DataEntity de=new DataEntity();
if(dsCommand==null)
throw new Exception("Error");
dsCommand.SelectCommand=this.GetSelectCommand();
dsCommand.Fill(de);
return de;
}
public bool InsertShippers(DataEntity de)
{
if(dsCommand==null)
throw new Exception("Error");
dsCommand.InsertCommand=this.GetInsertCommand();
dsCommand.Update(de,DataEntity.ShipperTableName);
if(de.HasErrors)
{
de.Tables[DataEntity.ShipperTableName].GetErrors()[0].ClearErrors();
return false;
}
else
{
de.AcceptChanges();
return true;
}
}
public bool UpdateShippers(DataEntity de)
{
if(dsCommand==null)
throw new Exception("Error");
dsCommand.UpdateCommand=this.GetUpdateCommand();
dsCommand.Update(de,DataEntity.ShipperTableName);
if(de.HasErrors)
{
de.Tables[DataEntity.ShipperTableName].GetErrors()[0].ClearErrors();
return false;
}
else
{
de.AcceptChanges();
return true;
}
}
public bool DeleteShippers(DataEntity de)
{
if(dsCommand==null)
throw new Exception("Error");
dsCommand.DeleteCommand=this.GetDeleteCommand();
dsCommand.Update(de,DataEntity.ShipperTableName);
if(de.HasErrors)
{
de.Tables[DataEntity.ShipperTableName].GetErrors()[0].ClearErrors();
return false;
}
else
{
de.AcceptChanges();
return true;
}
}
}
}我的问题是这是一个数据表的访问,如果表多了,是不是每个表都得写一个实体层?那么操作层又该怎样?这样写起来是不是搞麻烦了?