Set a string that can be access from the entire winform.

Hi Experts,

I have a winform that currently uses MS access as its database.  I am in the process of converting the database to SQL compact.

My current method of accessing the Access database is that I set a string at the beginning of the application and use it throughout the App.

see here:

//set the Connection String at the top of the app so it is accessible throughout the application.

 string conn1 = "Provider=Microsoft.JET.OLEDB.4.0;data source=C:\\Users\\Public\\Documents\\MCRS\\Resources\\DB\\MCRS_Data.mdb";

string conn2 = "Provider=Microsoft.JET.OLEDB.4.0;data source=C:\\Users\\Public\\Documents\\MCRS\\Resources\\DB\\MCRS_OTHER.mdb";




public WhateverTheFunctionIs() {

//call the connection string when accessing the database

   OleDbConnection objConn = new OleDbConnection(conn1 ));[/b]

            OleDbDataAdapter objComm = new OleDbDataAdapter("select Company + ',' + username AS MerchID,FullName, AD_Name from MCRS_LU_Users where active like 'y' and Merchandiser like 'y'", objConn);
            objConn.Open();

}

Open in new window



My SQL Lite db is within the resources folder.
My Question is this:

How do I do the same with my SQL Compact database?
SmashAndGrabAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ol muserTechnology GeneralistCommented:
Instead of using OleDbConnection use SqlCeConnection. And SqlCeCommand for querying.


string conn3 = "Data Source = C:\\what\\ever\\path\\MCRS_data.sdf;Persist Security Info=False";
SqlCeConnection sqlConnection1 = new SqlCeConnection();
sqlConnection1.ConnectionString = conn3;

Open in new window


Remember to add a reference to the SQL Server Compact ADO.NET provider, System.Data.SqlServerCe.dll
SmashAndGrabAuthor Commented:
Thanks for this.

I just need clarification on how I put this in my code.

Here is what I have so far:

Connections to the .SDF files:
Lookup path screenshot

Current code  - How do I add the SQL connection to this?
ol muserTechnology GeneralistCommented:
Are you having trouble finding the location of your SDF file (the SQl Compact database file)?

If you right click on the SDF file in "Solution Explorer" and click on "Properties" that will give you the full path for the SDF file in one of the property fields. For example if the full path is
C:\Database\Customer\MCRS_Data.sdf
your connection string should look as below:
string connLocalData = @"Data Source = C:\\Database\\Customer\\MCRS_Data.sdf;Persist Security Info=False"

Open in new window


Your ChecklastSync method could be altered as below:

            SqlCeConnection objConn = new SqlCeConnection(connLOCDAT);

            using (SqlCeCommand objComm = objConn.CreateCommand())
            {
                objConn.Open();
                //commands represent a query or a stored procedure       
                objComm.CommandText = "Select LAST_SYNC_DATE from MCRS_ADMIN_CONTROLS";
                using (SqlCeDataReader rd = objComm.ExecuteReader())
                {
                    //...read and fill variables
                    //remember SqlCeCommand  does not have a fill command
                }
                objConn.Close();
            }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

SmashAndGrabAuthor Commented:
That is superb - thanks!

I've made most of the changes.

I get an error here however:

error
ol muserTechnology GeneralistCommented:
Before filling the adapter try adding the table to the dataset.

ds.Tables.Add("table name ")
SmashAndGrabAuthor Commented:
Hi,

Thanks -

How do I incorporate that here?
error.JPG
ol muserTechnology GeneralistCommented:
Your SqlCeCommand object, how did you create it? That line seems truncated in your screenshot. I would expect that to have the connection object as the second parameter. Like below:

SqlCeCommand command = new SqlCeCommand("...query text...",connection );

Open in new window


If that's not it, can you pls print that whole line here? It could also be a malformed SQL statement.

HasRows is not supported, as noted here. You should be fine skipping that check.

Also using using is recommended in these situations.

using(SqlCeDataReader rdr = cmd.ExecuteReader())
{
    while (rdr.Read())
    {
         //my code
    }

Open in new window


Bonus:
And, please DO NOT call a reader object, adapter. That's gonna drive someone nuts during maintenance. :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.