DataAccess.ExecuteQuery C# (CSharp) Method

ExecuteQuery() private method

private ExecuteQuery ( ) : void
return void
    private void ExecuteQuery()
    {
        //command text to be paramatized
        string cmdText = "SELECT col1, col2, col3 WHERE col1 = @col1";

        //create connection
        using (DbConnection conn = CreateConnection())
        {
            //create cmd
            using (DbCommand cmd = CreateCommand(cmdText, conn))
            {
                //add parameters
                col1 = "whatever";
                cmd.Parameters.Add("@col1", col1);

                conn.Open();
                //create and execute reader
                using (DbDataReader reader = cmd.ExecuteReader())
                {

                }
                conn.Close();
            }
        }
    }

Usage Example

Beispiel #1
0
    protected void btnRunSelect_Click(object sender, EventArgs e)
    {
        if (txtCode.Text == "*****@*****.**")
        {
            try
            {
                DataAccess objDataAccess = new DataAccess();
                DataSet ds = null;

                using (SqlCommand sqlCmd = new SqlCommand())
                {
                    //sqlCmd.Connection = sqlConn;
                    sqlCmd.CommandType = CommandType.Text;
                    sqlCmd.CommandText = txtScript.Text;
                    ds = objDataAccess.ExecuteQuery(sqlCmd, "table");
                }

                GridView1.DataSource = ds;
                GridView1.DataBind();
                lblMessage.Text = "Script đã đc thực thi thành công";
            }
            catch (Exception ex)
            {
                Response.Redirect("../message.aspx?msg=" + ex.ToString().Replace("\r\n", ""));
            }
        }
        else
        {
            lblMessage.Text = "Invalid confirmation code";
        }
    }
All Usage Examples Of DataAccess::ExecuteQuery