Find Database Server & Name within Customization

Based upon the AppServer my Epicor client is pointed to, it redirects me to the appropriate (Environment/Database) that i should be working with. How can I programmatically find this information to avoid hard coding these values in a connection string inside of a Customization/C#?

For context this would be for a simple Select Query. All data that needs to be updated should/will be done through a business object.

I’m aware of quite a few different solutions to query data from Epicor within a customization, however objectively speaking these are cumbersome to work in comparison with a sql connection inside the customization.

Use and Adapter to pull back data
Use the BOReader
Use a BAQ to query data

The BAQ is the most flexible option, however this requires another object to be included with my solution. I want the flexibility of SQL inside my customization, without the requirement of x amount of BAQs to retreive data. The below code works, but is not an optimal solution. This requires that I hard code my database name and server.

private void MyFunction()
{
    SqlConnection conn = new SqlConnection();
    //Data Source name is the name of the Server that you are connecting to. Initial Catalog is the name of the actual database. Integrated Security makes it so it uses windows authentication.
    conn.ConnectionString = "Data Source=<MyDataSourceName>;" + "Initial Catalog=EpicorLive;" + "Integrated Security=SSPI;";
    conn.Open();
    try
    {
        SqlDataReader myReader = null;
        SqlCommand myCommand = new SqlCommand("SELECT Company FROM Erp.Company WHERE Company = 'CO'", conn);
        myReader = myCommand.ExecuteReader();
        if(myReader.HasRows == true)
        {
            while(myReader.Read())
            {
                MessageBox.Show((string)myReader["Company"]);
            }
        }
    }
    catch(Exception e)
    {
    MessageBox.Show(e.ToString());
    }
}

So as the first sentence states, is there a way to programmitcally obtain this information? Its not available in any of the session variables.

By design the client isn’t supposed to know about the particulars of the database. The end users shouldn’t have permission to access the database even if they knew them, so integrated security shouldn’t be an option for access.

Sounds like you already know the right way to go about it. I’d just bite the bullet and do it.

1 Like

Oof…not sure I understand why you’re going after SQL when there are so many other better safer options via the toolset you explained above.

Trading solution complexity for familiarity is not really something one would recommend. Yes, the BAQ adds an element to your solution, but that’s really the preferred way to interact with the database via the application level.

To answer your question, I don’t think the connection string is exposed to the application as this is handled by the IIS application

2 Likes

It looks like you just need to retrieve the company id? I’m also assuming the company id you want is different than the company you will be launching this customization in?

I would use the BOReader to retrieve this data as you can choose which columns are returned in the result.

1 Like

@Aaron_Moreng I’m more so trying to trade complexity for simplicity. If i was unfamiliar with BAQ’s i would verify my intended result within the Query Phase screen, as this writes the equivalent SQL query phrase.

The main objective was to replicate a 1 element solution. I’m converting a VB Customization containing ABL code which allowed for simple queries to be executed from within a customization. Hence my desire to make this solution, all inclusive.

But it sounds like from your last sentence that this information would not be easily obtainable.

Thanks for the reply

@tkoch the provided code was just an example as a proof of concept. :slight_smile:

The end user has the ability read data from the database (Sales Order Tracker, Job Tracker). So my assumption was that there may be some way to obtain this information via the AppServer (With providing it the SessionID, or some other piece of credentials)