
这种绑定暂时支持单表,并且不支持主键自增长!保存,删除,查看,修改用框架现成的。
1、先生成tb、bll、dal三个类。框架有生成工具,在debug文件里面有个叫CSFramework.Tools.ClassGenerator的应用程序。
tb类不需要修改什么,如下图:
数据库如果连接不成功,在debug文件里有个连接数据库的工具先连接一下。
按照图中这样设置好之后,点击生成类。一种是静态类,一种是实体类。区别在于保存的时候有一段代码不一样。一般生成静态类就可以了。
复制生成的类名作为保存文件的名字。
bll图:
点击生成基础数据BLL代码
dal图:
2、把生成的文件放到对应的项目文件里面:
tb文件放到CSFramework3.Models里面
bll文件放到CSFramework3.Business里面
dal文件放到CSFramework3.Server.DataAccess里面
3、创建实现数据库接口的方法类。找到CSFramework3.Interfaces这个项目。创建一个类似 IBridge_demo 这样的接口类。里面创建几个方法。当然接口可以共用。
如果业务需求一样,只是数据不一样就可以共用接口。只要创建实现接口类指向不同的查询类就可以了。我这里就创建新接口了。如果出现红色波浪线,右键看看是不是缺少引用。
代码例子:
public interface IBridge_demo
{
//模糊查询测试数据
DataTable SearchBy(string CustomerFrom, string CustomerTo, string Name, string Attribute);
//类别获取查询数据
DataTable GetCustomerByAttributeCodes(string attributeCodes, bool nameWithCode);
//模糊查询
DataTable FuzzySearch(string content);
//模糊查询
DataTable FuzzySearch(string attributeCodes, string content);
}
4、然后打开生成的BLL文件,加入这句代码:
public class blltest : bllBaseDataDict
{
private IBridge_Test _MyBridge; //桥接/策略接口
注销这句//_DAL = new daldemo(Loginer.CurrentUser);//数据层的实例
换成
_DataDictBridge = BridgeFactory.CreateDataDictBridge(typeof(tb_demo));//由ORM自动查询DAL类
_MyBridge = this.CreateBridge();
加完引用还有报错暂时别管,因为这个时候还没有实现接口的方法。选择我们去实现接口的方法。
5、找到CSFramework3.Bridge这个项目,创建一个实现之前接口的实现类。(这次是创建类不是接口、窗口)类似 demo_IBridge 这样的类。
为了减少操作直接替换下面的引用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSFramework3.Interfaces;
using CSFramework.Core;
using CSFramework.Common;
using System.Data;
using System.ServiceModel;
using CSFramework3.WebServiceReference;
using CSFramework3.Server.DataAccess.DAL_DataDict;
using CSFramework3.WebServiceReference.WCF_DataDictService;
using CSFramework3.Interfaces.Interfaces_Bridge;
using CSFramework3.Server.DataAccess;
//引用下面所有代码:
namespace CSFramework3.Bridge.DataDictModule
{
public class demo_IBridge
{
//数据层实例,实现桥接口IBridge_Document
private IBridge_demo _DAL_Demo = null;
//构造器
public ADODirect_demo()
{
_DAL_Demo = new daldemo(Loginer.CurrentUser);
}
//获取桥接功能实例
public IBridge_demo GetInstance()
{
return _DAL_Demo;
}
}
/// <summary>
/// 测试管理WebService桥接功能
/// </summary>
public class WebService_Demo : IBridge_demo
{
public WebService_Demo()
{
}
#region IBridge_demo 成员
public DataTable SearchBy(string CustomerFrom, string CustomerTo, string Name, string Attribute)
{
using (DataDictServiceClient client = SoapClientFactory.CreateDataDictClient())
{
byte[] loginTicket = WebServiceSecurity.EncryptLoginer(Loginer.CurrentUser);
byte[] receivedData = client.FuzzySearchCustomer(loginTicket, CustomerFrom, CustomerTo, Name, Attribute);
return ZipTools.DecompressionDataSet(receivedData).Tables[0];
}
}
public DataTable GetCustomerByAttributeCodes(string attributeCodes, bool nameWithCode)
{
using (DataDictServiceClient client = SoapClientFactory.CreateDataDictClient())
{
byte[] loginTicket = WebServiceSecurity.EncryptLoginer(Loginer.CurrentUser);
byte[] receivedData = client.GetCustomerByAttributeCodes(loginTicket, attributeCodes, nameWithCode);
return ZipTools.DecompressionDataSet(receivedData).Tables[0];
}
}
public DataTable FuzzySearch(string content)
{
using (DataDictServiceClient client = SoapClientFactory.CreateDataDictClient())
{
byte[] loginTicket = WebServiceSecurity.EncryptLoginer(Loginer.CurrentUser);
byte[] receivedData = client.FuzzySearchCustomerByContent(loginTicket, content);
return ZipTools.DecompressionDataSet(receivedData).Tables[0];
}
}
public DataTable FuzzySearch(string attributeCodes, string content)
{
using (DataDictServiceClient client = SoapClientFactory.CreateDataDictClient())
{
byte[] loginTicket = WebServiceSecurity.EncryptLoginer(Loginer.CurrentUser);
byte[] receivedData = client.FuzzySearchCustomerByAttributes(loginTicket, attributeCodes, content);
return ZipTools.DecompressionDataSet(receivedData).Tables[0];
}
}
#endregion
}
}
正常情况下代码中红色部分会报错,因为指向的dal类中还没有实现它的方法。下面我们去dal中实现它的方法。
6、找到CSFramework3.Server.DataAccess这个项目中的daldemo这个类打开,报错的地方右键添加引用。
在类中的这个地方加入红色代码:
//a、
[DefaultORM_UpdateMode(typeof(tb_demo), true)]
public class daldemo : dalBaseDataDict, IBridge_demo
{
//b、
return new GenerateSqlCmdByTableFields(ORM);
}
public DataTable SearchBy(string CustomerFrom, string CustomerTo, string Name, string Attribute)
{
StringBuilder sb = new StringBuilder();
sb.Append("SELECT * FROM demo WHERE name LIKE '%" + CustomerFrom + "%'");
sb.Append(" ORDER BY ID;");
SqlCommandBase cmd = SqlBuilder.BuildSqlCommandBase(sb.ToString());
return DataProvider.Instance.GetTable(_Loginer.DBName, cmd.SqlCommand, tb_demo.__TableName);
}
public DataTable GetCustomerByAttributeCodes(string attributeCodes, bool nameWithCode)
{
throw new NotImplementedException();
}
public DataTable FuzzySearch(string content)
{
throw new NotImplementedException();
}
public DataTable FuzzySearch(string attributeCodes, string content)
{
throw new NotImplementedException();
}
可以联系QQ:78474580 一起研究