Prevents duplicate logic judgments or throws exceptions caused by zero returned data behavior when querying data.
The following code uses the ORACLE database as an example:
1. DataSet data is returned. If the number of returned rows is zero, null is returned.
1 // <summary> 2 // execute the query statement, return DataSet 3 /// </summary> 4 /// <param name = "SQLString"> query statement </param> 5 /// <returns> DataSet </returns> 6 public static DataSet Query (string SQLString, params OracleParameter [] partition parms) 7 {8 using (OracleConnection connection = new OracleConnection (connectionString) 9 {10 OracleCommand cmd = new OracleCommand (); 11 PrepareCommand (cmd, connection, null, SQLString, partition parms); 12 using (OracleDataAdapter da = new OracleDataAdapter (cmd) 13 {14 DataSet ds = new DataSet (); 15 try16 {17 da. fill (ds, "ds"); 18 cmd. parameters. clear (); 19} 20 catch (System. data. oracleClient. oracleException ex) 21 {22 throw new Exception (ex. message); 23}24 // No data is returned. null25 if (ds. Tables [0]. Rows. Count <= 0) 26 {27 return null; 28} is returned}29 return ds; 30} 31} 32}
2. DataTable data is returned. If the number of rows returned by the query is zero, null is returned.
1 // <summary> 2 // execute the query statement, return DataTable 3 /// </summary> 4 /// <param name = "SQLString"> query statement </param> 5 /// <returns> DataTable </returns> 6 public static DataTable Query (string SQLString, params OracleParameter [] partition parms) 7 {8 using (OracleConnection connection = new OracleConnection (connectionString) 9 {10 OracleCommand cmd = new OracleCommand (); 11 PrepareCommand (cmd, connection, null, SQLString, partition parms); 12 using (OracleDataAdapter da = new OracleDataAdapter (cmd) 13 {14 DataTable dt = new DataTable (); 15 try16 {17 da. fill (dt); 18 cmd. parameters. clear (); 19} 20 catch (System. data. oracleClient. oracleException ex) 21 {22 throw new Exception (ex. message); 23}24 // No data is returned. null25 if (dt. Rows. Count <= 0) 26 {27 return null; 28} is returned}29 return dt; 30} 31} 32}
Implementation of PrepareCommand (useful in both 1 and 2)
1 // <summary> 2 // open the database connection, PASS Parameters and other pre-processing 3 /// </summary> 4 /// <param name = "cmd"> OracleCommand </param> 5 /// <param name = "conn "> database connection </param> 6 // <param name =" trans "> database transactions </param> 7 /// <param name =" plain text "> SQL statement </param> 8 // <param name = "param parms"> passed parameter </param> 9 private static void PrepareCommand (OracleCommand cmd, oracleConnection conn, 10 OracleTransaction trans, string plain text, OracleParame Ter [] parms) 11 {12 if (conn. State! = ConnectionState. Open) 13 conn. Open (); 14 cmd. Connection = conn; 15 cmd. CommandText = plain text; 16 if (trans! = Null) 17 cmd. Transaction = trans; 18 cmd. CommandType = CommandType. Text; 19 if (partition parms! = Null) 20 {21 foreach (OracleParameter parm in parallel parms) 22 {23 if (parm. value = null) 24 {25 parm. value = DBNull. value; 26} 27 cmd. parameters. add (parm); 28} 29} 30}
Application scenarios
1. prevent multiple judgments, such:
If (dt = null | dt. Rows. Count <= 0) {return ;}
Write only
If (dt = null) {return ;}
2. prevent exceptions caused by null data reference:
This. Maid = dt;
// If the number of data rows returned by dt is zero, the following assignment will cause an exception.
This. Maid [0]. Selected = false;