/// <summary> /// Process a script from a file /// </summary> /// <param name="path"></param> /// <returns></returns> public Results RunScript(string path) { ResetError(); Query oQuery = new Query(); Results oResults = new Results(); try { if (!System.IO.File.Exists(path)) { Results oNoResults = new Results(); Exception oFileNotFound = new System.IO.FileNotFoundException("File not found", path); oNoResults.SqlException = oFileNotFound; return oNoResults; } oQuery.Sql = System.IO.File.ReadAllText(path); oResults = Process_SQL(oQuery, CommandType.Text); } catch (Exception ex) { oResults.SqlException = ex; Error = ex; } return oResults; }
/// <summary> /// Processes the passed in query. The Type of request is also passed in /// </summary> /// <param name="query">Query Object that contains the Sql String and any parameters</param> /// <param name="oType">Query Type</param> /// <returns>Results object</returns> private Results Process_SQL(Query query, CommandType oType) { ResetError(); OpenConnection(); Results oResult = new Results(); string sSql = query.Sql; try { using (OleDbCommand oCmd = new OleDbCommand(sSql, Connection)) { oCmd.CommandType = oType; if (query.ParameterList != null) { for (int iCount = 0; iCount < query.ParameterList.Count; iCount++) { oCmd.Parameters.Add(query.ParameterList[iCount].Name, query.ParameterList[iCount].Type).Value = query.ParameterList[iCount].Value; } } oResult.Rows_Affected = oCmd.ExecuteNonQuery(); } CloseConnection(); return oResult; } catch (Exception ex) { CloseConnection(); oResult.SqlException = ex; Error = ex; return oResult; } }
/// <summary> /// Selects data based on the passed in query. /// </summary> /// <param name="query"></param> /// <returns></returns> public Results Select(Query query) { ResetError(); DataTable dTable = new DataTable(); Results oResults = new Results(); OpenConnection(); string sSql = query.Sql; oResults.SqlString = sSql; // Adds the passed in parameters to the query. try { using (OleDbCommand oCmd = new OleDbCommand(sSql, Connection)) { if (query.ParameterList != null) { for (int iCount = 0; iCount < query.ParameterList.Count; iCount++) { oCmd.Parameters.AddWithValue(query.ParameterList[iCount].Name, query.ParameterList[iCount].Value); } } dTable.Load(oCmd.ExecuteReader()); } CloseConnection(); // Sets the results obj oResults.Table = dTable; oResults.Rows_Affected = dTable.Rows.Count; } catch (Exception ex) { CloseConnection(); oResults.SqlException = ex; Error = ex; } return oResults; }