方法,調用
public DataTable SayHi(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"; OracleConnection con = new OracleConnection(constr); //數組處理--去除空數據 int count = 0; for (int i = 0; i < array.GetLength(0); i++) { if (array[i, 1] != null) { count++; } } ArrayList al = new ArrayList(); ArrayList ar = new ArrayList(); for (int i = 0; i < count; i++) { if (array[i, 1] != string.Empty) { al.Add(array[i, 0]); ar.Add(array[i, 1]); } } string[,] alr = new string[al.Count, 2]; for (int m = 0; m < alr.GetLength(0); m++) { alr[m, 0] = (string)al[m]; alr[m, 1] = (string)ar[m]; } try { //變量賦值 con.Open(); OracleParameter[] myParamArray = new OracleParameter[alr.Length + 2]; OracleCommand command = new OracleCommand(sql, con); OracleDataAdapter da = new OracleDataAdapter(); string ccBTime = @":" + btime; string ccDTime = @":" + dtime; myParamArray[0] = new OracleParameter("ccBTime", OracleType.DateTime); myParamArray[0].Value = endtime; command.Parameters.Add(myParamArray[0]); myParamArray[1] = new OracleParameter("ccDTime", OracleType.DateTime); myParamArray[1].Value = begintime; command.Parameters.Add(myParamArray[1]); for (int j = 2; j < alr.GetLength(0); j++) { string ccStr = @":" + alr[j, 0]; myParamArray[j] = new OracleParameter(ccStr, OracleType.VarChar, 50); myParamArray[j].Value = alr[j, 1]; 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 = 0; string[,] array = new string[10, 2]; string btime = "SCREATDATE"; //定義時間變量值 string dtime = "SDATE"; string sql = string.Format(@"select * from Student where 1=1 and SDATE<=to_date(:SDATE,‘YYYY/MM/DD hh24:mi:ss‘) and SCREATDATE>to_date(:SCREATDATE,‘YYYY/MM/DD hh24:mi:ss‘)"); string sno = textBox1.Text.Trim(); string sname = textBox2.Text.Trim(); string sage = textBox3.Text.Trim(); DateTime beginTime = dateTimePicker1.Value; DateTime endTime = dateTimePicker2.Value; if (sno != "") { sql += string.Format(" and SNO =:SNO"); array[arr, 0] = "SNO"; array[arr, 1] = sno; arr++; } if (sname != "") { sql += string.Format(" and SNAME=:SNAME"); array[arr, 0] = "SNAME"; array[arr, 1] = sname; arr++; } if (sage != "") { sql += string.Format(" and SAGE=:SAGE"); array[arr, 0] = "SAGE"; array[arr, 1] = sage; arr++; } MyMothed mm = new MyMothed(); dataGridView1.DataSource = mm.SayHi(sql, beginTime, endTime, array, btime, dtime);
时间: 2024-12-08 00:36:19