How to use stored procedure

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
Zohaib
Ensign (ENS)
Ensign (ENS)
Posts: 3
Joined: Sat Jan 09, 2010 2:13 pm

How to use stored procedure

Post by Zohaib » Sun Jan 10, 2010 7:52 am

I have a piece of code
string sel="select * from ableco";
SqlCommand command = new SqlCommand("storedprocedureName", con);
command.CommandType = CommandType.StoredProcedure;

if I do not want to use connection and use a code like this
DbCommand selectCommand = Token.Instance.Database.GetSqlStringCommand(sel)

My question is how i mentioned the stored procedure in this code.

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: How to use stored procedure

Post by mazhar » Mon Jan 11, 2010 4:44 am

Here is the example code

Code: Select all

System.Data.Common.DbCommand dbCommand = Token.Instance.Database.GetStoredProcCommand("NameOfYourStoredProceedure");
        Token.Instance.Database.AddInParameter(dbCommand, "@value1", System.Data.DbType.Int32, 2331);
        
        //For Insert/Update/Delte you can use execute non-query like
        int affectedRows = Token.Instance.Database.ExecuteNonQuery(dbCommand);
        
        //For Query that returns only a single value
        Object returnedValue = Token.Instance.Database.ExecuteScalar(dbCommand);
        
        //For query that returns a tabular structure
        System.Data.DataSet resultSet = Token.Instance.Database.ExecuteDataSet(dbCommand);
        
        //For quer where you want a connected read only reqader
        using (System.Data.IDataReader dr = Token.Instance.Database.ExecuteReader(dbCommand))
        {
            while (dr.Read())
            { 
                //You can access every row of your returned data here
                int name = AlwaysConvert.ToString(dr["Name"]);
            }
            dr.Close();
        }

Zohaib
Ensign (ENS)
Ensign (ENS)
Posts: 3
Joined: Sat Jan 09, 2010 2:13 pm

Re: How to use stored procedure

Post by Zohaib » Wed Jan 13, 2010 10:18 pm

string size = "select p1 from ac_CloudSettings where Description='Size'";
CommerceBuilder.Data.Database database = Token.Instance.Database;
using (System.Data.Common.DbCommand selectCommand = database.GetSqlStringCommand(size))
{
System.Data.DataSet dsDiscountedProducts = (System.Data.DataSet)Token.Instance.Database.ExecuteDataSet(selectCommand);
System.Data.DataTable table = dsDiscountedProducts.Tables[0];
CloudSize = Convert.ToInt32(table.Rows[0][0].ToString());
}

I am writting above lines to do fetch dataset from the database.My question is whether is it right way to do things or not?

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: How to use stored procedure

Post by mazhar » Thu Jan 14, 2010 4:31 am

Its OK but about a complete information about AbleCommerce DAL and best practices you need to read following topics
http://wiki.ablecommerce.com/index.php/ ... cess_Layer
http://wiki.ablecommerce.com/index.php/Custom_Queries

There is also some code templates available in forums to generate data access code. You can get these templates from this thread
viewtopic.php?f=47&t=9530

Post Reply