The Forums on slxdeveloper.com are now retired. The forum archive will remain available for the time being. Thank you for your participation on slxdeveloper.com!
|
|
DB connection from SmartPart
Posted: 20 Mar 08 8:37 AM
|
I am running some code from a smart part that requires connecting to the DB. Currently I am using this method: SqlConnection myConnection = new SqlConnection("Data Source=WIN2KVANILLA\\SQLEXPRESS;Initial Catalog=Saleslogix_Eval;User Id=sysdba assword=masterkey;");
If there a way I can open the connection without hardcoding the connection string, so when I move to production I don't have to change the connection string? I seem to recall someone at the dev class mentioning this, but cannot remember the details. Thanks
|
|
|
|
Re: DB connection from SmartPart
Posted: 20 Mar 08 9:31 AM
|
Sage.Platform.Data.IDataService service = Sage.Platform.Application.ApplicationContext.Current.Services.Get<Sage.Platform.Security.IDataService>(); string connstring = service.GetConnectionString();
-Ryan |
|
|
| |
|
Re: DB connection from SmartPart
Posted: 20 Mar 08 10:05 AM
|
Solid Gold! I am getting this error - I must need a reference.. The type or namespace name 'IDataService' does not exist in the namespace 'Sage.Platform.Security' (are you missing an assembly reference?)
I tried adding this and got this error: @ Register Assembly="Sage.Platform.Security" Namespace="Sage.Platform.Security.IDataService" TagPrefix="SalesLogix"
Could not load file or assembly 'Sage.Platform.Security' or one of its dependencies. The system cannot find the file specified.
|
|
|
|
Re: DB connection from SmartPart
Posted: 20 Mar 08 10:59 AM
|
Oops. That is my fault. I was typing it from memory before and some wires got crossed
Here's what it is *supposed* to be:
Sage.Platform.Data.IDataService service = ApplicationContext.Current.Services.Get<Sage.Platform.Data.IDataService>(); string connstring = service.GetConnectionString();
IDataService is "Sage.Platform.Data.IDataService", not "Sage.Platform.Security.IDataService". Doh! Sorry about that (but at least I typed it right the first time in that post ).
-Ryan |
|
|
| |
|
Re: DB connection from SmartPart
Posted: 20 Mar 08 11:09 AM
|
Almost? I now get this error (starting to feel like a pest.. sorry about that): error CS0103: The name 'ApplicationContext' does not exist in the current context |
|
|
|
Re: DB connection from SmartPart
Posted: 20 Mar 08 11:31 AM
|
Ok, I am by the 'ApplicatonContext' error but am having another problem..
Sage.Platform.Data.IDataService service = Sage.Platform.Application.ApplicationContext.Current.Services.Get(); string myConnection = service.GetConnectionString(); myConnection.Open();
I am unsure of how to open the connection.. I am getting this error with the above code: 'string' does not contain a definition for 'Open' |
|
|
|
Re: DB connection from SmartPart
Posted: 20 Mar 08 11:51 AM
|
The GetConnectionString() returns just that, a connection *string*. You'll need to create a connection and use that string to open.
Something like this:
Sage.Platform.Data.IDataService service = Sage.Platform.Application.ApplicationContext.Current.Services.Get<Sage.Platform.Data.IDataService>(); System.Data.OleDbConnection conn = new System.Data.OleDbConnection(service.GetConnectionString()); conn.Open(); //...
Make sense?
BTW, no, you're not being a pest. This is what the forums are for!  |
|
|
|
Re: DB connection from SmartPart
Posted: 21 Mar 08 6:21 AM
|
Hey Steve,
The IDataService can provide either a Connection or a Connection String. I believe that the GetConnection method returns an IDbConnection object so it is also possible to;
Sage.Platform.Data.IDataService service = ..... using(IDbConnection conn = service.GetConnection()) { conn.Open();
IDbCommand command = conn.CreateConnection(); command.CommandText = "Update ..... "; command.Execute......;
}
- Mark |
|
|
|
Re: DB connection from SmartPart
Posted: 21 Mar 08 9:44 AM
|
Gentlemen, A million thanks. Here is what I finally went with:
Sage.Platform.Data.IDataService service = Sage.Platform.Application.ApplicationContext.Current.Services.Get(); IDbConnection myConnection = service.GetConnection(); myConnection.Open(); IDbCommand MyCommand = myConnection.CreateCommand(); MyCommand.CommandText = "select Email from Contact where accountID='AGHEA0002669'"; IDataReader MyDataReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection); |
|
|
|
Re: DB connection from SmartPart
Posted: 21 Mar 08 12:08 PM
|
Btw... speaking from a painful experience... don't forget to make absolutely sure your data reader gets closed... something like this:
using(IDataReader MyDataReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection)){ // code here }
Otherwise it will bite you hard 2 hours after you push it to production. |
|
|
|
Re: DB connection from SmartPart
Posted: 21 Mar 08 1:43 PM
|
Thanks for the advice Nicolas. I implemented in my code. New to C# and rusty on ASP.net so I really appreciate the tips. |
|
|
|
Re: DB connection from SmartPart
Posted: 26 Oct 09 9:49 AM
|
Hi Ryan,
I am on 7.52 Web Client trying to create a BizRule using OleDBConnection.
I get following error when trying to get the OleDBConnection or IDbConnection The type or namespace name 'Data' does not exist in the namespace 'System' (are you missing an assembly reference?)
???
Any idea why?
|
|
|
|