//Closing the connection
 public static void DBClose(this SqlConnection sqlConnection)
 {
     try
     {
         sqlConnection.Close();
     }
     catch (Exception e)
     {
         LogHelpers.Write("ERROR :: " + e.Message);
     }
 }
Esempio n. 2
0
 // Open the connection
 public static SqlConnection DBConnect(this SqlConnection sqlConnection, string connectionString)
 {
     try
     {
         sqlConnection = new SqlConnection(connectionString);
         sqlConnection.Open();
         return(sqlConnection);
     }
     catch (Exception e)
     {
         LogHelpers.Write("ERROR :: " + e.Message);
     }
     return(null);
 }
Esempio n. 3
0
        public static string ReadData(int rowNumber, string columnName)
        {
            try
            {
                //Retriving Data using LINQ to reduce much of iterations
                string data = (from colData in _dataCol
                               where colData.colName == columnName && colData.rowNumber == rowNumber
                               select colData.colValue).SingleOrDefault();

                //var datas = dataCol.Where(x => x.colName == columnName && x.rowNumber == rowNumber).SingleOrDefault().colValue;
                return(data.ToString());
            }
            catch (Exception e)
            {
                LogHelpers.Write("Exception: " + e.Message);
                return(null);
            }
        }
        //Execution
        public static DataTable ExecuteQuery(this SqlConnection sqlConnection, string queryString)
        {
            DataSet dataset;

            try
            {
                //Checking the state of the connection
                if (sqlConnection == null || ((sqlConnection != null && (sqlConnection.State == ConnectionState.Closed ||
                                                                         sqlConnection.State == ConnectionState.Broken))))
                {
                    sqlConnection.Open();
                }

                SqlDataAdapter dataAdaptor = new SqlDataAdapter();
                dataAdaptor.SelectCommand             = new SqlCommand(queryString, sqlConnection);
                dataAdaptor.SelectCommand.CommandType = CommandType.Text;

                dataset = new DataSet();
                dataAdaptor.Fill(dataset, "table");
                sqlConnection.Close();
                return(dataset.Tables["table"]);
            }

            catch (Exception e)
            {
                dataset = null;
                sqlConnection.Close();
                LogHelpers.Write("ERROR :: " + e.Message);
                return(null);
            }

            finally
            {
                sqlConnection.Close();
                dataset = null;
            }
        }