Page 1 of 1
Referencing connection string
Posted: Tue Jan 05, 2010 11:19 pm
by igavemybest
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"?
Re: Referencing connection string
Posted: Wed Jan 06, 2010 5:11 am
by mazhar
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.
Re: Referencing connection string
Posted: Sat Jan 09, 2010 3:04 pm
by igavemybest
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);
Re: Referencing connection string
Posted: Sun Jan 10, 2010 1:23 pm
by jmestep
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);
}
Re: Referencing connection string
Posted: Wed Jan 13, 2010 3:23 pm
by igavemybest
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....
Re: Referencing connection string
Posted: Thu Jan 14, 2010 4:15 am
by mazhar
In order to make use of Token class you need to add using statement for CommerceBulider.Common namespace. So add
in the using statements to resolve Token class reference.
Re: Referencing connection string
Posted: Thu Jan 14, 2010 7:22 pm
by igavemybest
Ah.... Thank you!