Contact Form

Name

Email *

Message *

Some Common Function C# Easy Way for Qury Execution with SQL Server

No comments

 public DataTable GetDataTable(string query)
        {
            dynamic da = new SqlDataAdapter(query, strConnectionString);
            dynamic ds = new DataSet();
            da.Fill(ds);
            return ds.Tables(0);
        }
        public DataTable GetDataTableSP(string cSPName, string cwhere)
        {
            DataSet dataset = new DataSet();
            SqlCommand ExecCommand = new SqlCommand();
            try
            {
                SqlConnection objConnection = new SqlConnection(strConnectionString);
                ExecCommand = new SqlCommand(cSPName, objConnection);
                ExecCommand.CommandType = CommandType.StoredProcedure;
                ExecCommand.Connection.Open();
                ExecCommand.CommandTimeout = 0;
                ExecCommand.Parameters.Add(new SqlParameter("@cwhere", cwhere));
                SqlDataAdapter myadapter = new SqlDataAdapter(ExecCommand);
                myadapter.Fill(dataset);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ExecCommand.Connection.Close();
            }
            return dataset.Tables[0];
        }
       
        public DataTable runSP(string cSPName, ListDictionary MyListDic)
        {
            SqlCommand ExecCommand = default(SqlCommand);
            DataSet dataset = new DataSet();
            try
            {
                SqlConnection objConnection = new SqlConnection(strConnectionString);
                ExecCommand = new SqlCommand(cSPName, objConnection);
                ExecCommand.CommandType = CommandType.StoredProcedure;
                ExecCommand.Connection.Open();
                ExecCommand.CommandTimeout = 0;
                foreach (DictionaryEntry item in MyListDic)
                {
                    ExecCommand.Parameters.AddWithValue(item.Key.ToString(), item.Value.ToString());
                }
                SqlDataAdapter myadapter = new SqlDataAdapter(ExecCommand);
                myadapter.Fill(dataset);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ExecCommand.Connection.Close();
            }
            return dataset.Tables[0];
        }

        public string ExecuteScalar(string SqlString)
        {
            object str = "";
            SqlConnection objConnection = new SqlConnection(strConnectionString);
            SqlCommand dbcmd = new SqlCommand(SqlString, objConnection);
            try
            {
                dbcmd.Connection.Open();
                str = dbcmd.ExecuteScalar();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw ex;
            }
            finally
            {
                dbcmd.Connection.Close();
            }
            return Convert.ToString(str);
        }
        public void ExecuteNonQuery(string SqlString)
        {
            SqlConnection objConnection = new SqlConnection(strConnectionString);
            SqlCommand dbcmd = new SqlCommand(SqlString, objConnection);
            try
            {
                dbcmd.Connection.Open();
                dbcmd.ExecuteNonQuery();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw ex;
            }
            finally
            {
                dbcmd.Connection.Close();
            }
        }
        //   f

No comments :

Post a Comment