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

SmashAndGrab
SmashAndGrab used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ol muserTechnology Generalist

Commented:
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

Author

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?
Technology Generalist
Commented:
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

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
That is superb - thanks!

I've made most of the changes.

I get an error here however:

error
ol muserTechnology Generalist

Commented:
Before filling the adapter try adding the table to the dataset.

ds.Tables.Add("table name ")

Author

Commented:
Hi,

Thanks -

How do I incorporate that here?
error.JPG
ol muserTechnology Generalist

Commented:
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. :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial