views:

22

answers:

2

Hi,

I'm here for a trouble with SQLCE on a C# application, this is a really simple question, at first I was trying to do an INSERT into a table, but it didn't do it, so I searched and the solution was to put the literal string to connect to the database.

try
{
    cnTrupp.Open();
    SqlCeCommand com = new SqlCeCommand("INSERT INTO tipo_venta(nombre) VALUES (@nombre)", cnTrupp);
    com.Parameters.AddWithValue("@nombre", pNombre);
    com.ExecuteNonQuery();
    com.Dispose();
}
catch (SqlCeException e)
{
    LogFile log = new LogFile(e.Message);
}
finally
{
    cnTrupp.Close();
}

After that with the literal string, I was wondering, when I deploy the app, how I'm supposed to change that connection string? so it points to the actual database in the new computer

A: 

Make use of the .NET app.config file. This is a VB article but will get you started.

.NET config files are fairly easy to work with but with information like a db connection, you might want to consider encrypting the string in the config file. Or at least the password. That is if security is a concern. Many times it's not. Especially on mobile devices which are inherently unsecure (at least in the WinCE world... up to CE5 v.6)

Paul Sasik
That was the problem to begin with, it had something like this "Data Source =|DataDirectory|\example.sdf" with that it didn't really commit to the DB, then I changed that part to the real directory to the file.
Osukaa
The config file wasn't the problem the "|DataDirectory|" was. That's not a legal anything as far as i know. It's probably a stub refering to the "permanent" storage folder on a CE device. Each device manufacturer seems to have their own. You'll need to find out which one you're working with and use it. Btw, to deal with this issue, i did write a little algorithm at my last job to test and see which of the common "Data Directories" the current device supported. Was just a simple series of Directory.Exists calls i believe.
Paul Sasik
I think that was the problem, but also, the problem was that I didn't put that part myself, it was automatically done by the VS2010
Osukaa
lol. i know it's 2010, but you still need to cultivate and maintain a healthy distrust of Visual Studio! And that makes even more sense now btw that they would just insert placeholder text since every manufacturer does things differently.
Paul Sasik
I've written an answer about the "|DataDirectory|" part of the connection string you're seeing.
Thorsten Dittmar
+1  A: 

The comments on "Paul Sasik"'s post talk about the Data Source=|DataDirectory|\example.sdf entry in the app.config file of your application.

For the sake of completeness: This |DataDirectory| part is a macro that expands automatically to the folder where your application is running and should not be hardcoded. If you want to change the folder, you may use the following line in Program.cs:

AppDomain.CurrentDomain.SetData("DataDirectory", <New Folder>);

At least this is true for desktop applications. As mobile applications (in VS 2005 and 2008) don't support the same configuration mechanism, you have to create the connection string manually there.

Thorsten Dittmar