数据库编程与C#编程互译
今天有一段代码,先是用程序实现。
闲来无聊,又用存储过程实现了一次。
程序中实现。

/// <summary> /// 根据区域和用户名获取可访问的国家 /// </summary> public DataTable GetCountry(string area,string user) { try { NHibernate.Engine.ISessionImplementor FactoryImpl = FrameWork.Repository.NHb.SessionBuilder.CreateSession().GetSessionImplementation(); OracleCommand cmd = new OracleCommand(); string strWhere1 = "1=1 "; string strWhere2 = "1=1 "; if (!String.IsNullOrEmpty(user)) { strWhere1 += String.Format( "and vrs.u_logname = '{0}'",user); } if (!String.IsNullOrEmpty(area)) { strWhere2 += String.Format("and cc.sales_area = '{0}'", area); } string sqlstr =String.Format( @"select distinct scg.customer_country,cc.sales_area,cc.countrye,cc.countryc from ifce.vw_rsh_user vru, ele_bill.sap_customer_general scg, shipuser.countryce cc where vru.sap_cust_no = scg.customer_number and scg.customer_country = cc.short_e and vru.u_loginid in (select column_value countryId from table(splitstr_fun((select vrs.u_loginid || ',' || vrs.agent_user_id from ifce.vw_rsh_user vrs where {0}), ','))) and {1}", strWhere1, strWhere2); cmd.Parameters.Clear(); OracleConnection conn = (OracleConnection)FactoryImpl.Connection; cmd.Connection = conn; cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = sqlstr; cmd.ExecuteNonQuery(); OracleDataAdapter da = new OracleDataAdapter(cmd); System.Data.DataSet ds = new System.Data.DataSet(); da.Fill(ds); return ds.Tables[0]; } catch (Exception e) { throw new FrameWork.THrException("Fail!", e); } }
存储过程中实现。
/*******************************************************/ /* 根据区域和用户名获取可访问的国家 */ /* create bby wufei 2013 11 26 */ /*******************************************************/ procedure Query_Country(p_area varchar2, p_user varchar2, x_cur out cur_forlog) is strWhere1 varchar2(100); strWhere2 varchar2(100); sqlstr varchar2(1000); begin strWhere1 := '1=1 '; strWhere2 := '1=1 '; if (p_user is not null) then strWhere1 := strWhere1 || ' and vrs.u_logname = ''''||''' || p_user || '''||'''''; end if; /* if p_area is not null then strWhere2 := strWhere2 || 'and cc.sales_area =''' || p_area ''''; end if;*/ sqlstr:='select distinct scg.customer_country, cc.sales_area, cc.countrye, cc.countryc from ifce.vw_rsh_user vru, ele_bill.sap_customer_general scg, shipuser.countryce cc where vru.sap_cust_no = scg.customer_number and scg.customer_country = cc.short_e and vru.u_loginid in (select column_value countryId from table(splitstr_fun((select vrs.u_loginid || '','' || vrs.agent_user_id from ifce.vw_rsh_user vrs where ' || strWhere1 ||'), '',''))) and '|| strWhere2 ; dbms_output.put_line(sqlstr); open x_cur for sqlstr; exception when others then null; end Query_Country;
其中存储过程中还是有一些亮点的,比如将将查询table化,比如splitstr_fun。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述