Referencing connection string

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
User avatar
igavemybest
Captain (CAPT)
Captain (CAPT)
Posts: 388
Joined: Sun Apr 06, 2008 5:47 pm

Referencing connection string

Post by igavemybest » Tue Jan 05, 2010 11:19 pm

I have a .cs file where I would normally set it up like this:

Code: Select all

    public string Connection()
    {

        string con = @"Data Source=localhost;Initial Catalog=ac7_etc..etc...*";
        return con;

    }
Instead of declaring the same connection string and leaving it unencrypted, what would be a better way to do this. It is late and probably a simple thing, so sorry if it is a "duh"?

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

Re: Referencing connection string

Post by mazhar » Wed Jan 06, 2010 5:11 am

If this is something withing AbleCommerce applicatoin context then you do not need to worry about the database connection at all. All you is to get your Database object from token and then execute your commands via it. It will take care of every thing.

User avatar
igavemybest
Captain (CAPT)
Captain (CAPT)
Posts: 388
Joined: Sun Apr 06, 2008 5:47 pm

Re: Referencing connection string

Post by igavemybest » Sat Jan 09, 2010 3:04 pm

So would it be something like this? Sorry, I am not the usual person that works on AbleCommerce cause the other guy is out. Could you give an exampleif this is incorrect?

Code: Select all

Database database = Token.Instance.Database; 
            DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString()); 
            database.AddInParameter(selectCommand, "@storeId", System.Data.DbType.Int32, storeId); 

User avatar
jmestep
AbleCommerce Angel
Posts: 8164
Joined: Sun Feb 29, 2004 8:04 pm
Location: Dayton, OH
Contact:

Re: Referencing connection string

Post by jmestep » Sun Jan 10, 2010 1:23 pm

Here is a post from Able's wiki - be sure to use the "using" to release the memory.

CommerceBuilder.Data.Database database = Token.Instance.Database;
string sql = ("SELECT COUNT(*) As RecordCount FROM ac_Affiliates WHERE StoreId = @storeId");
using (System.Data.Common.DbCommand selectCommand = database.GetSqlStringCommand(sql))
{
database.AddInParameter(selectCommand, "@storeId", System.Data.DbType.Int32, Token.Instance.StoreId);
int affiliateCount = (int)database.ExecuteScalar(selectCommand);
}
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx

User avatar
igavemybest
Captain (CAPT)
Captain (CAPT)
Posts: 388
Joined: Sun Apr 06, 2008 5:47 pm

Re: Referencing connection string

Post by igavemybest » Wed Jan 13, 2010 3:23 pm

This is what I have, but it is throwing the error: "error CS0103: The name 'Token' does not exist in the current context "

Code: Select all

using System;
using System.Collections;
using System.Configuration;
using System.Data;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CommerceBuilder.Catalog;
using System.Data.SqlClient;
//using System.Xml.Linq;
using System.Data.Common;

public partial class ConLib_Custom_help : System.Web.UI.UserControl
{


    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            trWelcome.Visible = true;
            trDetail.Visible = false;

            BindNode();

            string i = "";
            i = Request.QueryString["id"];
            if (i != null)
            {
                //NewConnection newcon = new NewConnection();
                //string connect = newcon.Connection();
                //SqlConnection con = new SqlConnection(connect);
                //con.Open();

                string bind1 = "SELECT * FROM [ac_WebPages] where WebPageId=" + i;
                //DataSet ds1 = new DataSet();
                //DataTable dt = new DataTable();
                //SqlDataAdapter adt1 = new SqlDataAdapter(bind1, con);
               // adt1.Fill(ds1);
                //dt = ds1.Tables[0];
                
                CommerceBuilder.Data.Database database = Token.Instance.Database;
                using (System.Data.Common.DbCommand selectCommand = database.GetSqlStringCommand(bind1))
                {
                    System.Data.DataSet dsDiscountedProducts = (System.Data.DataSet)Token.Instance.Database.ExecuteDataSet(selectCommand);
                    System.Data.DataTable dt = dsDiscountedProducts.Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        lblName.Text = dt.Rows[0]["Name"].ToString();
                        ltrlDescription.Text = dt.Rows[0]["Description"].ToString();

                        trWelcome.Visible = false;
                        trDetail.Visible = true;
                        trErr.Visible = false;
                    }
....etc....

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

Re: Referencing connection string

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

In order to make use of Token class you need to add using statement for CommerceBulider.Common namespace. So add

Code: Select all

using CommerceBuilder.Common; 

in the using statements to resolve Token class reference.

User avatar
igavemybest
Captain (CAPT)
Captain (CAPT)
Posts: 388
Joined: Sun Apr 06, 2008 5:47 pm

Re: Referencing connection string

Post by igavemybest » Thu Jan 14, 2010 7:22 pm

Ah.... Thank you!

Post Reply