I have a Windows CE Form (NETCF 3.5) application that I've created via Visual Studio 2008 (c#) - (However, the core of my question probably applies to normal Windows application, too).
The application implements a SQLite database that I've placed within the project - i.e. The database location is in the sub-directory within my project. So the connection string to the database looks something like:
#"Data Source = C:\src\myApp\data\mydb.s3db"
The connection to the database is fine and works.
After I created the database, I added a Data Set via VS2008's "Data Source Configuration Wizard" (in VS2008, go to Data > Add New Data Source...). The wizard allows us to create a connection to an existing database and, then, choose tables from within that database that will be used in the Data Set that the wizard is about to create.
So the wizard does a fine job of creating the data set which allows us to program against it. The problem that I have is when I deploy the application to my WinCE device.
The problem that I encounter is when the application attempts to access the database when it needs to fill the Data Set with data.
The root of the problem is, the connection string to the database that is defined in the Data Set is per the dev machine - not the local path per the WinCE device.
For example: The connection string in the Data Set is set to
#"Data Source = C:\src\myApp\data\mydb.s3db"
instead of the full path of the target device similar to
#"Data Source = \Program Files\myApp\data\mydb.s3db"
I was able to fix the problem by manually editing the Data Set's .Designer.cs file (at the InitConnection() method). However, it would be nice if VS2008 can manage the connection string for me somehow - i.e. when in dev mode, the connection string points to the database on my dev machine. When in deploy mode, the connection string points to the database on the device.
Is there a way to specify the connection string to point to a relative path rather than the full path, maybe? Or?
Use "|DataDirectory|" in your connection string.
Quote from http://social.msdn.microsoft.com/Forums/en-US/sqlce/thread/dc31ea59-5718-49b6-9f1f-7039da425296/ ...
|DataDirectory| (enclosed in pipe symbols) is a substitution string that indicates the path to the database. It eliminates the need to hard-code the full path which leads to several problems as the full path to the database could be serialized in different places. DataDirectory also makes it easy to share a project and also to deploy an application.
For example, instead of having the following connection string:
"Data Source= c:\program files\MyApp\Mydb.sdf"
Using DataDirectory, you can have the following connection string:
“Data Source = |DataDirectory|\Mydb.sdf”
To set the DataDirectory property, call the AppDomain.SetData method. If you do not set the DataDirectory property, the following default rules will be applied to access the database folder:
For applications that are put in a folder on the user's computer, the
database folder uses the application folder.
For applications that
are running under ClickOnce, the database folder uses the specific
data folder that is created.
Related
I have this problem:
I have a web application that was working ok on accessing shared folder in same server application, but outside of application folder.
The route of shared folder is stored in a table (SQL Server 2008 R2), this is query using sqlcommand and datareader, including credentials to access the shared folder.
Suddenly that part of application stop working, the problem I found is that the path of shared folder appears without the char '\', something like this \IP_SERVERsharedfolder altough in database it is stored as \\IP_SERVER\sharedfolder, if the query is executed on sql server managment, it displays the whole path.
Do you have experienced a similar problem.
I appreciate your help.
The problem here is not how to access the shared folder, that means, the problem is when the path is gotten from database where it is stored, I use a data reader object to get from database:
If dtrReader.HasRows Then
fldPath = dtrReader(3).ToString
username = dtrReader(4).ToString
password = dtrReader(5).ToString
End If
in variable fldPath , I got \IP_SERVERsharedfolder instead \\IP_SERVER\sharedfolder, char \ is removed.
I am trying to use SQLite for my ASP.Net Website.
The code is working fine in a Windows Application, but not in the WebAPP. I think I have to change some File Permissions to let the Website set up a Database. But how?
I always get a ERROR at the open() statement. Saying cannot open file.
Following the Code, which is working fine in the WindowsAPP:
Dim connDB as SQLiteConnection = New SQLiteConnection("PlayerDB.sqlite")
Dim conn = New SQLiteConnection("Data Source=PlayerDB.sqlite;Version=3")
conn.open() ---> ERROR
...
I hope someone can help me quick.
Thanks.
There are two reasons why you can get this error, so you must make sure that both these are taken care of before running your code.
You need to specify the absolute path to the database in your connection string and not a relative path that you are currently using in your code since SQLite database is just a file. For this you need to create a special folder for sqlite databases in your ASP.Net app. Let's say you have created a folder called sqlite under the root folder. Then your code should be as below. Server.MapPath is an utility method in ASP.Net framework to convert a root relative path to an absolute path that looks like c:\abc\wwq\myfile.db.
Dim connDB as SQLiteConnection = New SQLiteConnection(string.Format("Data
Source={0};Version=3;",Server.MapPath("~/sqlite/PlayerDB.sqlite")));
conn.open() ---> NO ERROR should happen now
You should also make sure that the ASP.Net application identity is allowed read/write access to thesqlitefolder created in above step under the root of your web app because data will be read as well as written to the database file under this folder.
Just right click on the sqlite folder and select Properties and then the Security tab as in screen shot below. In most localhost cases, the ASP.Net identity is IIS_IUSRS which is highlighted below. Make sure that this user has full control over the sqlite folder.
I make a software in C# .net express edition in in windows 7.But When I run this software in other machine in which install windows XP,it can not run due to instance failure.When I set the break point in other machine code,I see that it hold the database location path of my machine.Actually i want to access the db from my debug folder.
Like..
DB Location in may machine -- F:\MY Project\Tracking Soft\bin\debug\TRack.mdf
DB Location In Other Machine -- E:\Tracking Soft\bin\debug\TRack.mdf
I mean that in the both machine the connection string is same but the project location is different
When using SQL Server express, you can put your database file into the App_Data folder (e.g. in CATracking\App_Data). Then you should be able to access it using a connection string like this one:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|TRack.mdf;Database=...
Try this code
private string dbPath = Application.StartupPath + "\\bpTracking.mdf";
But your bpTracking.mdf isn't in the correct directory. Move it into \bin\Debug for this code to work.
I have a need to swap between different DB instances of the same design. Is there a way to repoint the DBML to this different DB. What would be ideal would be to specify in code a different web.config connection string. However one would not want to change the Datacontext names in the codebase. If one was doing this in ADO.NET then one could choose the connection string.
Thanks,
Ed
When you instantiate a DBML the appropriate connection string is retrieved from a configuration file. (app.config or web.config). In the case that no setting is found, the initial connection string (which exists as a property on the DBML, and can be viewed through the designer) is used.
You will only need to use the connection string constructor if you are using multiple connection strings through a single instance, and need to specify the database to which you should connect.
In your case you simply need to include the appropriate connection string in your executing settings file. The connection string must be named correctly - see the setting which is placed in the project containing the DBML to check the name.
You can use web.config transformations and easily swap needed configurations, here are the some links :
Common Web.Config transformations with Visual Studio 2010
Web.config Transformation Syntax for Web Application Project Deployment
You should able to do that. When you generate DBML usually it writes the connection string to a configuration file (web.config or app.config). We should just change the database location in the configuration file. However, you have to make sure all your database are in Sync.
I've created an entity model file (.edmx) based on an .mdf file in my asp.net application, placed in my App_Data Folder.
first of all my connection string, created by the entity framework wizard :
<connectionStrings>
<add name="Sales_DBEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=.\SQLEXPRESS;attachdbfilename="c:\users\ext\documents\visual studio 2010\Projects\WebProject_A\WebProject_A\App_Data\Sales_DB.mdf";integrated security=True;USER INSTANCE=TRUE;multipleactiveresultsets=True;App=EntityFramework'"
providerName="System.Data.EntityClient" />
</connectionStrings>
The above is placed in an app.config file created by the wizard.
I've copied the same connection string to the web.config file as well (I'm not sure if this is necessary).
I've come across two problems when attempting to later run the same application on a my machine (I've created the application on a different machine).
The first was regarding the user instance:
Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed
The first thing I don't really comprehend is what a user instance actually defines.
According to MSDN :
A user instance is a separate instance of the SQL Server Express Database Engine that is generated by a parent instance
Is my local database considered a user instance ?
I would really like some clarification on the matter because I came across posts suggesting
to mark it as False, and the reason was not clear to me .
another post on this matter
From there I came across another solution that explains that for this problem you need to
enable user instance in the SQL Server on your machine.
The Fix
I've done what was explained in that post
but now I've come across a new problem :
An attempt to attach an auto-named database for file (Full Path to my app_data\my.mdf file) A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
I've already attempted to delete all the files under
C:\Documents and Settings\(your user account name)\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS
and rebooting like suggested in other posts, but still I can't find a clear solution for this problem.
I know this is a long post but any help would be appreciated
thanks in advance.
Read this very good overview of what user instances really are.
In a nutshell:
SQL Server allows you to have multiple instances of itself on a single computer - the default instance can be accessed using just the machine name (or IP address) of the host machine - all other instances need to be specified by an instance name (like the SQLExpress on your machine)
for development purposes, you can also create a user instance - each user gets their own separate instance of SQL Server (works in the Express edition only) and a database is attached by its file name (path\Sales_DB.mdf) to that user instance. This instance is started up on demand, then runs and is shut down when no longer needed
While this works great for development, it's not really intended for later on - certainly not for production use.
In a production environment, you want to attach your database files to the actual SQL Server instance they should run on - typically using SQL Server Management Studio. When you do this, you no longer refer to that database via its file name, but instead you use the database name that was given to the database when being attached to the server. In this case, SQL Server will handle all details of operations - and that SQL Server instance is up and running all the time (doesn't need to be started "on demand" all the time), and you can use things like permissions and logins to control access to the database.
Your connection string would then look something like this:
<connectionStrings>
<add name="Sales_DBEntities"
connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='server=YourServerNameHere;database=Sales_DB;integrated security=True;App=EntityFramework'"
providerName="System.Data.EntityClient" />
</connectionStrings>
When you have attached a database to a production (or testing) SQL Server instance, you do not need to know what the files are that make up the database, and you don't need to specify those files - SQL Server will take care of that. You just refer to the database via its database name.
The solution is always simpler then anticipated:
double click the model.edmx file -> the designer will open.
right click on the designer -> Update model from database .
new connection -> Choose Database file (.mdf) under type .
Browse -> go to your App_Data folder, and choose the DB.
copy the connection string created in App.config to web.config.
bye.