下面讲解Winform界面怎样调用WebApi接口实现增、删、改、查(CRUD),下图是CSFramework.WebApi后端开发框架测试程序主界面,以单表数据操作界面演示(Customer:客户管理)为例进行讲解。
CRUD是什么?
CRUD是指在做计算处理时的增加(Create)、读取(Retrieve)、更新(Update)和删除(Delete)几个单词的首字母简写。CRUD主要被用在描述软件系统中数据库或者持久层的基本操作功能。
CSFramework.WebApi后端开发框架测试程序主界面:
点【基础资料(Customer,Object Entity)】按钮打开【客户资料管理】管理界面。
一、实现新增功能(Create)
新增按钮事件:
C# Code:
//新增客户
private void btnAdd_Click(object sender, EventArgs e)
{
_Mode = "Add";
//创建客户实体对象
_Current = new Entity_Customer();
_Current.CreatedBy = "admin";
_Current.CreationDate = DateTime.Now;
//绑定主表文本框数据源
DoBindingEditorPanel(panel3, _Current);
//设置按钮状态
SetButtonState();
tabControl1.SelectedTab = tabPage2;
txtCustomerCode.ReadOnly = false;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
private void btnAdd_Click(object sender, EventArgs e)
{
_Mode = "Add";
//创建客户实体对象
_Current = new Entity_Customer();
_Current.CreatedBy = "admin";
_Current.CreationDate = DateTime.Now;
//绑定主表文本框数据源
DoBindingEditorPanel(panel3, _Current);
//设置按钮状态
SetButtonState();
tabControl1.SelectedTab = tabPage2;
txtCustomerCode.ReadOnly = false;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
新建一条记录,首先要创建实体对象,然后绑定编辑界面文本框架的数据源(显示数据)。
C# Code:
/// <summary>
/// 动态绑定资料编辑组件的数据源
/// </summary>
/// <param name="editorPanel"></param>
/// <param name="dataSource"></param>
private void DoBindingEditorPanel(Control editorPanel, object dataSource)
{
string fieldName = "";
for (int i = 0; i <= editorPanel.Controls.Count - 1; i++)
{
//匹配:txt字段名(属性名)的组件,如:txtCustomerCode, txtCustomerName
if (editorPanel.Controls[i].Name.Substring(0, 3) != "txt") continue;
if (editorPanel.Controls[i] is TextBoxBase)
{
TextBoxBase edit = editorPanel.Controls[i] as TextBoxBase;
fieldName = edit.Name.Substring(3, edit.Name.Length - 3);
edit.DataBindings.Clear();
Binding b = new Binding("Text", dataSource, fieldName);
edit.DataBindings.Add(b);//绑定数据源
b.ReadValue();
}
}
}
/// 动态绑定资料编辑组件的数据源
/// </summary>
/// <param name="editorPanel"></param>
/// <param name="dataSource"></param>
private void DoBindingEditorPanel(Control editorPanel, object dataSource)
{
string fieldName = "";
for (int i = 0; i <= editorPanel.Controls.Count - 1; i++)
{
//匹配:txt字段名(属性名)的组件,如:txtCustomerCode, txtCustomerName
if (editorPanel.Controls[i].Name.Substring(0, 3) != "txt") continue;
if (editorPanel.Controls[i] is TextBoxBase)
{
TextBoxBase edit = editorPanel.Controls[i] as TextBoxBase;
fieldName = edit.Name.Substring(3, edit.Name.Length - 3);
edit.DataBindings.Clear();
Binding b = new Binding("Text", dataSource, fieldName);
edit.DataBindings.Add(b);//绑定数据源
b.ReadValue();
}
}
}
保存按钮事件:
C# Code:
//保存事件
private void btnSave_Click(object sender, EventArgs e)
{
//调用业务层保存方法
ModelResponse result = new bllCustomer().Post(_Current, _Mode);
if (result.Code == 0)
{
//刷新缓存
if (dataGridView1.DataSource == null)
{
List<Entity_Customer> list = new List<Entity_Customer>();
list.Add(_Current);
dataGridView1.DataSource = list;
}
else
{
if (_Mode == "Add")
{
List<Entity_Customer> list = dataGridView1.DataSource as List<Entity_Customer>;
list.Add(_Current);
dataGridView1.DataSource = null;
dataGridView1.DataSource = list;
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
dataGridView1.CurrentCell = dataGridView1[0, dataGridView1.Rows.Count - 1];
}
if (_Mode == "Edit")
{
Entity_Customer customer = dataGridView1.SelectedCells[0].OwningRow.DataBoundItem asEntity_Customer;
CSFramework.WebApi.Core.WebApiDataConverter.CopyObject(_Current, customer);
}
}
_Mode = "View";
SetButtonState();
MessageBox.Show("保存成功!");
}
else
{
MessageBox.Show("保存失败! " + result.Message);
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
private void btnSave_Click(object sender, EventArgs e)
{
//调用业务层保存方法
ModelResponse result = new bllCustomer().Post(_Current, _Mode);
if (result.Code == 0)
{
//刷新缓存
if (dataGridView1.DataSource == null)
{
List<Entity_Customer> list = new List<Entity_Customer>();
list.Add(_Current);
dataGridView1.DataSource = list;
}
else
{
if (_Mode == "Add")
{
List<Entity_Customer> list = dataGridView1.DataSource as List<Entity_Customer>;
list.Add(_Current);
dataGridView1.DataSource = null;
dataGridView1.DataSource = list;
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
dataGridView1.CurrentCell = dataGridView1[0, dataGridView1.Rows.Count - 1];
}
if (_Mode == "Edit")
{
Entity_Customer customer = dataGridView1.SelectedCells[0].OwningRow.DataBoundItem asEntity_Customer;
CSFramework.WebApi.Core.WebApiDataConverter.CopyObject(_Current, customer);
}
}
_Mode = "View";
SetButtonState();
MessageBox.Show("保存成功!");
}
else
{
MessageBox.Show("保存失败! " + result.Message);
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
提交资料,调用WebApi接口:
C# Code:
/// <summary>
/// 保存数据
/// </summary>
/// <param name="model"></param>
/// <param name="mode"></param>
/// <returns></returns>
internal ModelResponse Post(Entity_Customer model, string mode)
{
string data = JsonConvert.SerializeObject(model);
ModelRequestAction request = ModelExample.GetRequestAction("admin", "", "WebApi_TestDB", 800001, mode, data, false);
string result = WebApiTools.Post(_URL, JsonConvert.SerializeObject(request, JsonSettings.Current), HttpContentType.Application_JSON);
ModelResponse response = JsonConvert.DeserializeObject<ModelResponse>(result);
return response;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// <summary>
/// 保存数据
/// </summary>
/// <param name="model"></param>
/// <param name="mode"></param>
/// <returns></returns>
internal ModelResponse Post(Entity_Customer model, string mode)
{
string data = JsonConvert.SerializeObject(model);
ModelRequestAction request = ModelExample.GetRequestAction("admin", "", "WebApi_TestDB", 800001, mode, data, false);
string result = WebApiTools.Post(_URL, JsonConvert.SerializeObject(request, JsonSettings.Current), HttpContentType.Application_JSON);
ModelResponse response = JsonConvert.DeserializeObject<ModelResponse>(result);
return response;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
二、实现删除功能(Delete)
首先在表格中选择一条记录,点【删除】按钮。
C# Code:
//删除按钮事件
private void btnDelete_Click(object sender, EventArgs e)
{
if (dataGridView1.RowCount <= 0) return;
Entity_Customer customer = dataGridView1.SelectedCells[0].OwningRow.DataBoundItem asEntity_Customer;
//调用接口删除记录
bool ok = new bllCustomer().Delete(customer.CustomerCode);
if (ok)
{
List<Entity_Customer> data = (dataGridView1.DataSource as List<Entity_Customer>);
data.Remove(customer);
dataGridView1.DataSource = null;
dataGridView1.DataSource = data;
//显示下一条记录
if (tabControl1.SelectedIndex == 1 && dataGridView1.Rows.Count > 0)
btnView_Click(btnView, new EventArgs());
MessageBox.Show("删除成功");
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
private void btnDelete_Click(object sender, EventArgs e)
{
if (dataGridView1.RowCount <= 0) return;
Entity_Customer customer = dataGridView1.SelectedCells[0].OwningRow.DataBoundItem asEntity_Customer;
//调用接口删除记录
bool ok = new bllCustomer().Delete(customer.CustomerCode);
if (ok)
{
List<Entity_Customer> data = (dataGridView1.DataSource as List<Entity_Customer>);
data.Remove(customer);
dataGridView1.DataSource = null;
dataGridView1.DataSource = data;
//显示下一条记录
if (tabControl1.SelectedIndex == 1 && dataGridView1.Rows.Count > 0)
btnView_Click(btnView, new EventArgs());
MessageBox.Show("删除成功");
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
C# Code:
/// <summary>
/// 删除数据,调用WebApi接口
/// </summary>
/// <param name="PONO"></param>
/// <returns></returns>
internal bool Delete(string customerCode)
{
ModelRequestAction request = ModelExample.GetRequestAction("admin", "", "WebApi_TestDB", 800001, "Delete", PONO, false);
string result = WebApiTools.Post(_URL, JsonConvert.SerializeObject(request, JsonSettings.Current), HttpContentType.Application_JSON);
ModelResponse response = JsonConvert.DeserializeObject<ModelResponse>(result);
return response.Code == 0;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// 删除数据,调用WebApi接口
/// </summary>
/// <param name="PONO"></param>
/// <returns></returns>
internal bool Delete(string customerCode)
{
ModelRequestAction request = ModelExample.GetRequestAction("admin", "", "WebApi_TestDB", 800001, "Delete", PONO, false);
string result = WebApiTools.Post(_URL, JsonConvert.SerializeObject(request, JsonSettings.Current), HttpContentType.Application_JSON);
ModelResponse response = JsonConvert.DeserializeObject<ModelResponse>(result);
return response.Code == 0;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
三、实现修改功能(Update)
修改一条记录,首先要获取当前记录的数据并转换为实体对象,然后在界面显示数据(绑定对象的数据源)。
C# Code:
//查看详情按钮事件
private void btnView_Click(object sender, EventArgs e)
{
if (dataGridView1.RowCount <= 0) return;
_Mode = "View";
Entity_Customer customer = dataGridView1.SelectedCells[0].OwningRow.DataBoundItem asEntity_Customer;
_Current = new bllCustomer().GetDataByKey(customer.CustomerCode);
//绑定主表文本框数据源
DoBindingEditorPanel(panel3, _Current);
SetButtonState();
tabControl1.SelectedTab = tabPage2;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
private void btnView_Click(object sender, EventArgs e)
{
if (dataGridView1.RowCount <= 0) return;
_Mode = "View";
Entity_Customer customer = dataGridView1.SelectedCells[0].OwningRow.DataBoundItem asEntity_Customer;
_Current = new bllCustomer().GetDataByKey(customer.CustomerCode);
//绑定主表文本框数据源
DoBindingEditorPanel(panel3, _Current);
SetButtonState();
tabControl1.SelectedTab = tabPage2;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
根据主键获取客户资料:
C# Code:
/// <summary>
/// 获取客户资料
/// </summary>
/// <param name="customerCode"></param>
/// <returns></returns>
public Entity_Customer GetDataByKey(string customerCode)
{
ModelRequestAction request = ModelExample.GetRequestAction("admin", "", "WebApi_TestDB", 800001, "View", customerCode, false);
string result = WebApiTools.Post(_URL, JsonConvert.SerializeObject(request, JsonSettings.Current), HttpContentType.Application_JSON);
ModelResponse response = JsonConvert.DeserializeObject<ModelResponse>(result);
if (response.Code == 0)
return JsonConvert.DeserializeObject<Entity_Customer>(response.Data);
else
return null;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// 获取客户资料
/// </summary>
/// <param name="customerCode"></param>
/// <returns></returns>
public Entity_Customer GetDataByKey(string customerCode)
{
ModelRequestAction request = ModelExample.GetRequestAction("admin", "", "WebApi_TestDB", 800001, "View", customerCode, false);
string result = WebApiTools.Post(_URL, JsonConvert.SerializeObject(request, JsonSettings.Current), HttpContentType.Application_JSON);
ModelResponse response = JsonConvert.DeserializeObject<ModelResponse>(result);
if (response.Code == 0)
return JsonConvert.DeserializeObject<Entity_Customer>(response.Data);
else
return null;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
四、实现查询功能 (Retrieve)
查询按钮事件:
C# Code:
//查询客户资料
private void btnQuery_Click(object sender, EventArgs e)
{
//查询参数
dynamic queryParam = new
{
CustomerCode = txt_CustomerCode.Text,
NativeName = txt_NativeName.Text
};
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = new bllCustomer().Query(queryParam);
if (dataGridView1.DataSource == null) MessageBox.Show("查询资料失败!");
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
private void btnQuery_Click(object sender, EventArgs e)
{
//查询参数
dynamic queryParam = new
{
CustomerCode = txt_CustomerCode.Text,
NativeName = txt_NativeName.Text
};
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = new bllCustomer().Query(queryParam);
if (dataGridView1.DataSource == null) MessageBox.Show("查询资料失败!");
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
BLL.Query方法:
C# Code:
/// <summary>
/// 查询客户资料
/// </summary>
/// <param name="paramObject">查询参数,动态对象模型</param>
/// <returns></returns>
public List<Entity_Customer> Query(dynamic paramObject)
{
string dataJson = JsonConvert.SerializeObject(paramObject, JsonSettings.Current);
//创建WebApi接口请求对象
ModelRequestAction request = ModelExample.GetRequestAction("admin", "", "WebApi_TestDB", 800001, "Query", dataJson, false);
//提交,POST
string result = WebApiTools.Post(_URL, JsonConvert.SerializeObject(request, JsonSettings.Current), HttpContentType.Application_JSON);
ModelResponse response = JsonConvert.DeserializeObject<ModelResponse>(result);
if (response.Code == 0)
return JsonConvert.DeserializeObject<List<Entity_Customer>>(response.Data);
else
return null;
}
/// 查询客户资料
/// </summary>
/// <param name="paramObject">查询参数,动态对象模型</param>
/// <returns></returns>
public List<Entity_Customer> Query(dynamic paramObject)
{
string dataJson = JsonConvert.SerializeObject(paramObject, JsonSettings.Current);
//创建WebApi接口请求对象
ModelRequestAction request = ModelExample.GetRequestAction("admin", "", "WebApi_TestDB", 800001, "Query", dataJson, false);
//提交,POST
string result = WebApiTools.Post(_URL, JsonConvert.SerializeObject(request, JsonSettings.Current), HttpContentType.Application_JSON);
ModelResponse response = JsonConvert.DeserializeObject<ModelResponse>(result);
if (response.Code == 0)
return JsonConvert.DeserializeObject<List<Entity_Customer>>(response.Data);
else
return null;
}
创建WebApi接口请求对象:
C# Code:
/// <summary>
/// 创建WebApi接口请求对象
/// </summary>
/// <param name="userID">用户账号,对应用户表的Account字段</param>
/// <param name="token">令牌,登录成功后自动分配</param>
/// <param name="actionID">接口编号、功能编号</param>
/// <param name="op">具体操作,如:Add/Delete/Edit/Query</param>
/// <param name="data">本次操作数据,如Op=Query,Data可以是查询条件</param>
/// <param name="dataEncrypt">数据是否加密</param>
/// <returns></returns>
public static ModelRequestAction GetRequestAction(string userID, string token, string dbid, intactionID, string op, string data, bool dataEncrypt = true)
{
//Request.Data数据对象
ModelRequestClientData M = new ModelRequestClientData();
M.UserID = userID;
M.Token = token;
M.DBID = dbid;
string clientData = JsonConvert.SerializeObject(M, JsonSettings.Current);
//Request请求主体对象
ModelRequestAction mr = new ModelRequestAction();
mr.Timestamp = DateTime.Now.ToString("yyyyMMddHHmmssfff");
mr.Operation = op;
mr.Action = actionID;
mr.ApiKey = PrivateData.apikey;//公钥
mr.Data = dataEncrypt ? CryptoHelper.DESEncrypt(data, PrivateData.DES_Key, PrivateData.DES_iv) : data;
mr.Sign = CryptoHelper.ToMD5(mr.ApiKey + mr.Data + PrivateData.secret + mr.Timestamp);//Sign数字签名
mr.DataIsEncrypted = dataEncrypt;
mr.ClientData = clientData;
return mr;
}
/// 创建WebApi接口请求对象
/// </summary>
/// <param name="userID">用户账号,对应用户表的Account字段</param>
/// <param name="token">令牌,登录成功后自动分配</param>
/// <param name="actionID">接口编号、功能编号</param>
/// <param name="op">具体操作,如:Add/Delete/Edit/Query</param>
/// <param name="data">本次操作数据,如Op=Query,Data可以是查询条件</param>
/// <param name="dataEncrypt">数据是否加密</param>
/// <returns></returns>
public static ModelRequestAction GetRequestAction(string userID, string token, string dbid, intactionID, string op, string data, bool dataEncrypt = true)
{
//Request.Data数据对象
ModelRequestClientData M = new ModelRequestClientData();
M.UserID = userID;
M.Token = token;
M.DBID = dbid;
string clientData = JsonConvert.SerializeObject(M, JsonSettings.Current);
//Request请求主体对象
ModelRequestAction mr = new ModelRequestAction();
mr.Timestamp = DateTime.Now.ToString("yyyyMMddHHmmssfff");
mr.Operation = op;
mr.Action = actionID;
mr.ApiKey = PrivateData.apikey;//公钥
mr.Data = dataEncrypt ? CryptoHelper.DESEncrypt(data, PrivateData.DES_Key, PrivateData.DES_iv) : data;
mr.Sign = CryptoHelper.ToMD5(mr.ApiKey + mr.Data + PrivateData.secret + mr.Timestamp);//Sign数字签名
mr.DataIsEncrypted = dataEncrypt;
mr.ClientData = clientData;
return mr;
}
WebApi服务端实现:
实现命令层,Cmd_Test_Customer:
Cmd_Test_Customer.Execute执行方法:
C# Code:
/// <summary>
/// 执行命令
/// </summary>
/// <returns></returns>
public override IUserResponse Execute()
{
//新增
if (_UserRequest.Operation == CommandOperation.Add.ToString())
{
Entity_Customer customer = _UserRequest.GetDataObject<Entity_Customer>();
DataTable dtTmp = _DAL.GetDataByKey("-");
WebApiDataConverter.AddObject2Table(customer, dtTmp);
bool ok = _DAL.Update(dtTmp);
return new ModelResponse { Code = ok ? 0 : -1, Message = "操作" + (ok ? "成功" : "失败") };
}
//删除
if (_UserRequest.Operation == CommandOperation.Delete.ToString())
{
bool ok = _DAL.Delete(_UserRequest.Data);
return new ModelResponse { Code = ok ? 0 : -1, Message = "操作" + (ok ? "成功" : "失败") };
}
//修改
if (_UserRequest.Operation == CommandOperation.Edit.ToString())
{
Entity_Customer customer = _UserRequest.GetDataObject<Entity_Customer>();
DataTable dtTmp = _DAL.GetDataByKey("-");
WebApiDataConverter.AddObject2Table(customer, dtTmp);
dtTmp.AcceptChanges();
dtTmp.Rows[0].SetModified();
bool ok = _DAL.Update(dtTmp);
return new ModelResponse { Code = ok ? 0 : -1, Message = "操作" + (ok ? "成功" : "失败") };
}
//查询
if (_UserRequest.Operation == CommandOperation.Query.ToString())
{
dynamic data = JsonConvert.DeserializeObject<dynamic>(_UserRequest.Data);//测试动态对象
List<Entity_Customer> list = _DAL.Query(data);
return new ModelResponse
{
Code = 0,
Message = "操作成功",
Data = JsonConvert.SerializeObject(list)
};
}
//查询-根据主键查询一条记录
if (_UserRequest.Operation == "GetCustomer")
{
List<Entity_Customer> list = _DAL.QueryByKey(_UserRequest.Data);
return new ModelResponse
{
Code = 0,
Message = "操作成功",
Data = list.Count > 0 ? JsonConvert.SerializeObject(list) : ""
};
}
//根据主键值获取详情
if (EqualsOP(_UserRequest.Operation, CommandOperation.View))//GetDataByKey
{
Entity_Customer customer = _DAL.Get(_UserRequest.Data);
return new ModelResponse
{
Code = 0,
Message = "操作成功",
Data = JsonConvert.SerializeObject(customer)
};
}
return new ModelResponse
{
Code = ErrorCodes.InterfaceIdInvalide,
Message = ErrorCodes.InterfaceIdInvalide_Msg
};
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// 执行命令
/// </summary>
/// <returns></returns>
public override IUserResponse Execute()
{
//新增
if (_UserRequest.Operation == CommandOperation.Add.ToString())
{
Entity_Customer customer = _UserRequest.GetDataObject<Entity_Customer>();
DataTable dtTmp = _DAL.GetDataByKey("-");
WebApiDataConverter.AddObject2Table(customer, dtTmp);
bool ok = _DAL.Update(dtTmp);
return new ModelResponse { Code = ok ? 0 : -1, Message = "操作" + (ok ? "成功" : "失败") };
}
//删除
if (_UserRequest.Operation == CommandOperation.Delete.ToString())
{
bool ok = _DAL.Delete(_UserRequest.Data);
return new ModelResponse { Code = ok ? 0 : -1, Message = "操作" + (ok ? "成功" : "失败") };
}
//修改
if (_UserRequest.Operation == CommandOperation.Edit.ToString())
{
Entity_Customer customer = _UserRequest.GetDataObject<Entity_Customer>();
DataTable dtTmp = _DAL.GetDataByKey("-");
WebApiDataConverter.AddObject2Table(customer, dtTmp);
dtTmp.AcceptChanges();
dtTmp.Rows[0].SetModified();
bool ok = _DAL.Update(dtTmp);
return new ModelResponse { Code = ok ? 0 : -1, Message = "操作" + (ok ? "成功" : "失败") };
}
//查询
if (_UserRequest.Operation == CommandOperation.Query.ToString())
{
dynamic data = JsonConvert.DeserializeObject<dynamic>(_UserRequest.Data);//测试动态对象
List<Entity_Customer> list = _DAL.Query(data);
return new ModelResponse
{
Code = 0,
Message = "操作成功",
Data = JsonConvert.SerializeObject(list)
};
}
//查询-根据主键查询一条记录
if (_UserRequest.Operation == "GetCustomer")
{
List<Entity_Customer> list = _DAL.QueryByKey(_UserRequest.Data);
return new ModelResponse
{
Code = 0,
Message = "操作成功",
Data = list.Count > 0 ? JsonConvert.SerializeObject(list) : ""
};
}
//根据主键值获取详情
if (EqualsOP(_UserRequest.Operation, CommandOperation.View))//GetDataByKey
{
Entity_Customer customer = _DAL.Get(_UserRequest.Data);
return new ModelResponse
{
Code = 0,
Message = "操作成功",
Data = JsonConvert.SerializeObject(customer)
};
}
return new ModelResponse
{
Code = ErrorCodes.InterfaceIdInvalide,
Message = ErrorCodes.InterfaceIdInvalide_Msg
};
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
数据访问层:
C# Code:
/// <summary>
/// 客户管理,数据层
/// </summary>
public class dalCustomer : dalBaseDataDict
{
public dalCustomer(IUserRequestClientLogin loginer)
{
if (loginer != null)
_Database = DatabaseProvider.GetDatabase(loginer.DBID);
else
_Database = DatabaseProvider.GetDatabase("WebApi_TestDB");
this.ORM = typeof(Entity_Customer);
}
public override DataTable GetSummaryData()
{
string sql = "SELECT * FROM tb_Customer ORDER BY CreationDate DESC";
return _Database.GetTable(sql, _TableName);
}
protected override IGenerateSqlCommand CreateSqlGenerator(string tableName)
{
Type ORM = null;
if (tableName == Entity_Customer.__TableName) ORM = typeof(Entity_Customer);
if (ORM == null) throw new Exception(tableName + "表没有ORM模型!");
return new GenerateSqlCmdByObjectClass(_Database, ORM);
}
public List<Entity_Customer> Query(dynamic data)
{
string sql = "SELECT * FROM dbo.tb_Customer WHERE 1=1";
if (data.CustomerCode.ToString() != "") sql += " AND CustomerCode LIKE '%" + data.CustomerCode + "%'";
if (data.NativeName.ToString() != "") sql += " AND NativeName LIKE '%" + data.NativeName + "%'";
List<Entity_Customer> list = _Database.ExecuteReader<Entity_Customer>(sql, row =>WebApiDataConverter.Convert2Object<Entity_Customer>(row));
return list;
}
public List<Entity_Customer> QueryByKey(string key)
{
string sql = "SELECT * FROM dbo.tb_Customer WHERE CustomerCode ='" + key + "'";
List<Entity_Customer> list = _Database.ExecuteReader<Entity_Customer>(sql, row =>WebApiDataConverter.Convert2Object<Entity_Customer>(row));
return list;
}
public Entity_Customer Get(string customerCode)
{
string sql = "SELECT * FROM dbo.tb_Customer WHERE CustomerCode='" + customerCode + "'";
Entity_Customer customer = _Database.ExecuteReader<Entity_Customer>(sql);
return customer;
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// 客户管理,数据层
/// </summary>
public class dalCustomer : dalBaseDataDict
{
public dalCustomer(IUserRequestClientLogin loginer)
{
if (loginer != null)
_Database = DatabaseProvider.GetDatabase(loginer.DBID);
else
_Database = DatabaseProvider.GetDatabase("WebApi_TestDB");
this.ORM = typeof(Entity_Customer);
}
public override DataTable GetSummaryData()
{
string sql = "SELECT * FROM tb_Customer ORDER BY CreationDate DESC";
return _Database.GetTable(sql, _TableName);
}
protected override IGenerateSqlCommand CreateSqlGenerator(string tableName)
{
Type ORM = null;
if (tableName == Entity_Customer.__TableName) ORM = typeof(Entity_Customer);
if (ORM == null) throw new Exception(tableName + "表没有ORM模型!");
return new GenerateSqlCmdByObjectClass(_Database, ORM);
}
public List<Entity_Customer> Query(dynamic data)
{
string sql = "SELECT * FROM dbo.tb_Customer WHERE 1=1";
if (data.CustomerCode.ToString() != "") sql += " AND CustomerCode LIKE '%" + data.CustomerCode + "%'";
if (data.NativeName.ToString() != "") sql += " AND NativeName LIKE '%" + data.NativeName + "%'";
List<Entity_Customer> list = _Database.ExecuteReader<Entity_Customer>(sql, row =>WebApiDataConverter.Convert2Object<Entity_Customer>(row));
return list;
}
public List<Entity_Customer> QueryByKey(string key)
{
string sql = "SELECT * FROM dbo.tb_Customer WHERE CustomerCode ='" + key + "'";
List<Entity_Customer> list = _Database.ExecuteReader<Entity_Customer>(sql, row =>WebApiDataConverter.Convert2Object<Entity_Customer>(row));
return list;
}
public Entity_Customer Get(string customerCode)
{
string sql = "SELECT * FROM dbo.tb_Customer WHERE CustomerCode='" + customerCode + "'";
Entity_Customer customer = _Database.ExecuteReader<Entity_Customer>(sql);
return customer;
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
CSFramework.WebApi后端开发框架测试程序,VS2017解决方案完整版:
<本文完>
.NET WebApi开发框架|MVC框架|后端框架|服务端框架-标准版V1.0
适用开发:快速构建支持多种客户端的服务端程序,支持APP、B/S、C/S跨平台移动终端等。
运行平台:Windows + .NET Framework 4.5
开发工具:Visual Studio 2015+,C#语言
数据库:Microsoft SQLServer 2008R2+(支持多数据库:Oracle/MySql)
运行平台:Windows + .NET Framework 4.5
开发工具:Visual Studio 2015+,C#语言
数据库:Microsoft SQLServer 2008R2+(支持多数据库:Oracle/MySql)