1.首先創建一個測試數據表

CREATE TABLE people
(
SNO VARCHAR2(10 BYTE),
SNAME VARCHAR2(10 BYTE),
SSEX VARCHAR2(10 BYTE),
SAGE number,
SDEPT VARCHAR2(10 BYTE),
BTDATE DATE
) SET DEFINE OFF;
Insert into PEOPLE
(SNO, SNAME, SSEX, SAGE, SDEPT,BTDATE)
Values
('', '劉晨', '女', '', 'CS', TO_DATE('12/31/2014 18:25:19', 'MM/DD/YYYY HH24:MI:SS'));
Insert into PEOPLE
(SNO, SNAME, SSEX, SAGE, SDEPT,BTDATE)
Values
('', '王敏', '女', '', 'MA', TO_DATE('12/31/2014 18:25:19', 'MM/DD/YYYY HH24:MI:SS'));
Insert into PEOPLE
(SNO, SNAME, SSEX, SAGE, SDEPT,BTDATE)
Values
('', '張力', '男', '', 'IS', TO_DATE('12/31/2014 18:25:19', 'MM/DD/YYYY HH24:MI:SS'));
Insert into PEOPLE
(SNO, SNAME, SSEX, SAGE, SDEPT,BTDATE)
Values
('', '小趙', '男', '', 'MA', TO_DATE('12/31/2015 18:25:19', 'MM/DD/YYYY HH24:MI:SS'));
Insert into PEOPLE
(SNO, SNAME, SSEX, SAGE, SDEPT,BTDATE)
Values
('', '李勇', '男', '', 'CS', TO_DATE('1/31/2015 18:25:20', 'MM/DD/YYYY HH24:MI:SS'));
Insert into PEOPLE
(SNO, SNAME, SSEX, SAGE, SDEPT,BTDATE)
Values
('', '小翠', '男', '', 'CS', TO_DATE('12/31/2014 18:25:20', 'MM/DD/YYYY HH24:MI:SS'));
COMMIT;

2.設計一個WINFROM的查詢介面

3.後臺主要代碼如下:

public DataTable XxSelect(string sql, DateTime? begintime, DateTime? endtime, string[,] array, string btime, string dtime)
{
string constr = @"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.93.64.242)(PORT=1521))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=wmstest)));User ID=asrs;Password=asrs123";
//string constr = @"Data Source=orcl;Persist Security Info=True;User ID=DBSNMP;Password=dbsnmp";
OracleConnection con = new OracleConnection(constr); //數組處理--去除空數據
int count = ;
for (int i = ; i < array.GetLength(); i++)
{
if (array[i, ] != null)
{
count++;
}
}
ArrayList al = new ArrayList();
ArrayList ar = new ArrayList();
for (int i = ; i < count; i++)
{
if (array[i, ] != string.Empty)
{
al.Add(array[i, ]);
ar.Add(array[i, ]);
}
}
string[,] alr = new string[al.Count, ];
for (int m = ; m < alr.GetLength(); m++)
{
alr[m, ] = (string)al[m];
alr[m, ] = (string)ar[m];
}
try
{ //變量賦值
con.Open();
OracleParameter[] myParamArray = new OracleParameter[alr.Length + ];
OracleCommand command = new OracleCommand(sql, con);
OracleDataAdapter da = new OracleDataAdapter();
string ccBTime = @":" + btime;
string ccDTime = @":" + dtime;
if (begintime==null && endtime==null)
{
for (int j = ; j < alr.GetLength(); j++)
{
string ccStr = @":" + alr[j, ];
myParamArray[j] = new OracleParameter(ccStr, OracleType.VarChar, );
myParamArray[j].Value = alr[j, ];
command.Parameters.Add(myParamArray[j]);
}
}
if (begintime != null && endtime == null)
{
myParamArray[] = new OracleParameter(ccBTime, OracleType.DateTime);
myParamArray[].Value = begintime;
command.Parameters.Add(myParamArray[]);
for (int j = ; j < alr.GetLength() + ; j++)
{
string ccStr = @":" + alr[j - , ];
myParamArray[j] = new OracleParameter(ccStr, OracleType.VarChar, );
myParamArray[j].Value = alr[j - , ];
command.Parameters.Add(myParamArray[j]);
}
}
if (begintime == null && endtime != null)
{
myParamArray[] = new OracleParameter(ccDTime, OracleType.DateTime);
myParamArray[].Value = endtime;
command.Parameters.Add(myParamArray[]);
for (int j = ; j < alr.GetLength() + ; j++)
{
string ccStr = @":" + alr[j - , ];
myParamArray[j] = new OracleParameter(ccStr, OracleType.VarChar, );
myParamArray[j].Value = alr[j - , ];
command.Parameters.Add(myParamArray[j]);
}
}
if (begintime != null && endtime != null)
{
myParamArray[] = new OracleParameter(ccBTime, OracleType.DateTime);
myParamArray[].Value = begintime;
command.Parameters.Add(myParamArray[]);
myParamArray[] = new OracleParameter(ccDTime, OracleType.DateTime);
myParamArray[].Value = endtime;
command.Parameters.Add(myParamArray[]); for (int j = ; j < alr.GetLength() + ; j++)
{
string ccStr = @":" + alr[j - , ];
myParamArray[j] = new OracleParameter(ccStr, OracleType.VarChar, );
myParamArray[j].Value = alr[j - , ];
command.Parameters.Add(myParamArray[j]);
}
}
da.SelectCommand = command;
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
catch (Exception e)
{
MessageBox.Show("異常" + e.Message);
return null;
}
finally
{
con.Close();
}
}
int arr = ;
string[,] array = new string[, ];
string btime = "SBEGINDATE"; //时间参数
string dtime = "SENDDATE"; //时间参数
string sql = string.Format(@"select * from people where 1=1");
string sno = textBox1.Text.Trim();
string sname = textBox2.Text.Trim();
string sage = textBox3.Text.Trim();
DateTime? beginDate = null;
DateTime? endDate = null;
if (this.dateTimePicker1.Checked==true)
{
sql += string.Format(" and BTDATE>=to_date(to_char(:SBEGINDATE,'YYYY/MM/DD hh24:mi:ss'),'yyyy/MM/dd HH24:mi:ss')");
beginDate = dateTimePicker1.Value;
} if (this.dateTimePicker2.Checked == true)
{
sql += string.Format(" and BTDATE<=to_date(to_char(:SENDDATE,'YYYY/MM/DD hh24:mi:ss'),'yyyy/MM/dd HH24:mi:ss')");
endDate = dateTimePicker2.Value;
}
if (sno != "")
{
sql += string.Format(" and SNO =:SNO");
array[arr, ] = "SNO";
array[arr, ] = sno;
arr++;
}
if (sname != "")
{
sql += string.Format(" and SNAME=:SNAME");
array[arr, ] = "SNAME";
array[arr, ] = sname;
arr++;
}
if (sage != "")
{
sql += string.Format(" and SAGE=:SAGE");
array[arr, ] = "SAGE";
array[arr, ] = sage;
arr++;
}
MyMothed mySel = new MyMothed();
dataGridView1.DataSource = mySel.XxSelect(sql, beginDate, endDate, array, btime, dtime);

附注:是否提高查詢速度,減輕DB服務器負擔,還需實際上線觀察。

最新文章

  1. 1. Activiti 运行时表信息总结
  2. iptables 思维导图 (zz)
  3. oracle11g AUD$维护
  4. 《CSS3实战》读书笔记 第三章:选择器:样式实现的标记
  5. python以指定宽度及格式输出字符串
  6. Apache Solr 访问权限控制
  7. (基础篇)php中理解print EOT分界符和echo EOT的用法区别
  8. [CF442C] Artem and Array (贪心+单调栈优化)
  9. scala学习笔记2
  10. Linux scp 使用详解
  11. Linux shell 脚本攻略之文件查找与文件列表
  12. Bernese单点定位数据准备及处理
  13. 打造 通用的 支持多数据库 操作的 DBHelper
  14. Maven快速搭建GUI项目
  15. 使用 visualstudio code 编辑器调试执行在 homestead 环境中的 laravel 程序
  16. 百度地图api的用法
  17. [HAOI2016]食物链
  18. POJ 2234 Matches Game(Nim博弈裸题)
  19. layui小封装方法
  20. Android—— Animation动画(很详细)

热门文章

  1. UVA11419 SAM I AM —— 最小点覆盖 + 输出覆盖点集
  2. POJ1661 Help Jimmy —— DP
  3. git unstage
  4. JFreeChart教程(一)(转)
  5. 如何判断http服务器是否支持range寻址
  6. Burnside&amp;Polya
  7. bzoj 3218: a + b Problem【主席树+最小割】
  8. 洛谷P2787 语文1(chin1)- 理理思维(珂朵莉树)
  9. 【笔记】对自定义异常的理解(Java)
  10. 【POJ - 2251】Dungeon Master (bfs+优先队列)