结构函数意义
定义: 结构函数是以Struct声明的 实现:
在日常写代码的过程中,可能会遇到以下的问题:往往调用结构体中的属性的时候,因为属性较多,比较繁琐,这时就会用到结构函数
结构函数不多说,直接上代码
public struct GetSQLString
{
public string assetnumber; //资产编号
public string mac; //电脑MAC地址
public string recievename; //领用人
public string outname; //领出人
public string RecieveBegin; //领用开始时间
public string RecieveEnd; //领用结束时间
public string OutBegin; //领出开始时间
public string OutEnd; //领出结束时间
public string GetSQL()
{
StringBuilder strSql = new StringBuilder();
strSql.AppendFormat(@"select a.assetnumber as 资产编号,b.computername as 电脑名称,a.computermac as MAC地址,b.computerip as IP地址,b.computeruser as 登录用户名,a.recievename as 领用人,a.recievetime as 领用时间,
a.outname as 领出人, a.outtime as 领出时间
from computer_position_info a,computer_cell_info b where a.computermac = b.computermac");
if (!string.IsNullOrEmpty(assetnumber)) strSql.AppendFormat(@" and a.assetnumber='{0}'", assetnumber);
if (!string.IsNullOrEmpty(mac)) strSql.AppendFormat(@" and a.computermac='{0}'", mac);
if (!string.IsNullOrEmpty(recievename)) strSql.AppendFormat(@" and a.recievename='{0}'", recievename);
if (!string.IsNullOrEmpty(outname)) strSql.AppendFormat(@" and a.outname='{0}'", outname);
if (!string.IsNullOrEmpty(RecieveBegin) && !string.IsNullOrEmpty(RecieveEnd)) strSql.AppendFormat(@" and (a.recievetime >=to_date('{0}','yyyy-MM-dd hh24:mi:ss') and a.recievetime <=to_date('{1}','yyyy-MM-dd hh24:mi:ss')) ", RecieveBegin, RecieveEnd);
if (!string.IsNullOrEmpty(OutBegin) && !string.IsNullOrEmpty(OutEnd)) strSql.AppendFormat(@" and (a.outtime >=to_date('{0}','yyyy-MM-dd hh24:mi:ss') and a.outtime <=to_date('{1}','yyyy-MM-dd hh24:mi:ss')) ", OutBegin, OutEnd);
strSql.AppendFormat(@" order by recievetime desc");
return strSql.ToString();
}
}
调用方式
GetSQLString strSql = new GetSQLString();
strSql.assetnumber = assetnumber; strSql.mac = mac; strSql.recievename = recievename; strSql.outname = outname; strSql.RecieveBegin = RecieveBegin; strSql.RecieveEnd = RecieveEnd; strSql.OutBegin = OutBegin; strSql.OutEnd = OutEnd;
DataSet ds = PubLibrary.objDB.GetDataSet(strSql.GetSQL());