Reference sqlite Database in App.config - c#

Edit Question:
Sorry about this basic question. This is a new technology to me.
I am using a c#, winforms application with sqlite.
Currently I have included sqliteDb in app_data folder, Also App.Config has the connection string:
<connectionStrings>
<clear/>
<add name ="RConnString"
providerName="System.Data.Sqlite"
connectionString="Data Source= C:\project1\R1\App_Data\RVEST_V1.DB"/>
</connectionStrings>
This RconnString is being accessed in .cs file.
string connString =
ConfigurationManager.ConnectionStrings["RConnString"].ConnectionString;
When I send it to client, How would I send the database. I mean how should I change the connection string.( Instead of referring to my local C:..)
Thank you
Sun

Try using relative path like this
connectionString="Data Source=App_Data\RVEST_V1.DB"

Related

Get SQL Connection error while using Web Service with local SQL Server database

I'm setting up a web service, I wanted to use the local database so I created a local SQL Server database in this project. The web service runs just fine on localhost, but the problem when I invoke a method to Load data from the local database on this project, I'm getting an error:
System.Data.SqlClient.SqlException:
An attempt to attach an auto-named database for file C:\Program Files (x86)\IIS Express\TourTravelDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
TourTravelDB.mdf is my local database
I've added a connection string in my web.config but it's still not working.
Here is my connection string.
<connectionStrings>
<add name="CS"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\TourTravelDB.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
I've also tried to move my database to the IIS Express directory, it works but when run it from another computer, I have to move the database again.
Does anyone have an idea how to solve this?
This may help you..
Add "User Instance=True;" in your connection string.
<connectionStrings>
<add name="aspnet_staterKits_Test_TimeTracker"
connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer"
connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf;"/>
</connectionStrings>
if doesn't work then try out the below links..
forums.asp.net
codeproject.com
Already existing thread in stack overflow

C# prepare connection string for server

I'm about to send my Visual Studio project (C#, ASP.NET) and database (MS Access) to someone who will upload it into a server (don't know which one).
However, given that database file will be at the same map with the project, how do I prepare my connection string for that database? So far I worked local at my computer, including the database.
This is how my connection looks like in web.config, what changes do I need to make?
<connectionStrings>
<add name="DatabaseConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Project\etc\DATABASE.mdb"
providerName="System.Data.OleDb" />
<add name="DatabaseConnectionString1"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\DATABASE.mdb"
providerName="System.Data.OleDb" />
</connectionStrings>
Any help would be greatly appreciated.

Connecting to mdf database with Web.config ASP.NET

I tried many things and saw a lot of information on the internet. It just doesn't work and I want to know what am I doing wrong.
I created a .mdf database in my App_Data folder and trying to connect it through Web.config.
my <connectionString /> looks like this:
<add name="Datab1" connectionString="Data Source=.\SQLExpress;AttachDbFilename=|DataDirectory|Datab1.mdf;Database=Datab1; Trusted_Connection=Yes;" />
And I also tried this:
<add name="Datab1" connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=E:\Secret\Secret\App_Data\Datab1.mdf;
Integrated Security=True;
User Instance=True"/>
The providerName="System.Data.Client" />gives me an error so I removed it.
What am I doing wrong here?
I finally fixed it by going to Server Explorer > Connect to Database > Data source to "Microsoft SQL Server Database File (SqlClient)" and Db file name browse to the .mdf file you want to use. Use Windows Authentication.
in the Web.config use this string:
<add name="Datab1" connectionString="Data Source=(localdb)\v11.0;AttachDbFileName=path\to\folder\of\database\Datab1.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
And replace Datab1 with your own database name of course. This worked for me so this question is solved!

Dynamic sql connection string

I am using my SQL connection in my mvc4 application as follows:
public static string ConnectionString=#"Data Source=LocalDB)\v11.0;AttachDbFilename=C:\Users\..\Documents\Visual Studio 2012\Projects\..\..\App_Data\RoDB.mdf;Integrated Security=True";
I want to rewrite it as dynamically.
When I change the system, I don't want to change the connection string.
If you use ".\SQLExpress" as server name it will connect to the local instance. In that case you don't need to change your connection string on different machines.
You can put connection strings in your web.config file, this means it is out of application code and doesn't require a re-build to change.
<configuration>
<!-- Other config settings -->
<connectionStrings>
<add name="localDBConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\..\Documents\Visual Studio 2012\Projects\..\..\App_Data\RoDB.mdf;Integrated Security=True" />
</connectionStrings>
</configuration>
Then to use this in your application you can put the following in compiled code:
string myConnectionString = ConfigurationManager.ConnectionStrings["localDBConnection"].ConnectionString;
Whats wrong with using a web config? its pretty much standard practice I'd assume?
Also read up on using the relative path. EG. Relative to application location
add a app configuration file in you application and add setting inside it
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="Data Source=LocalDB)\v11.0;AttachDbFilename=C:\Users\..\Documents\Visual Studio 2012\Projects\..\..\App_Data\RoDB.mdf;Integrated Security=True"/>
</appSettings>
</configuration>
in your code you can write
string ConnectionString= System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();

database path in web.config file while uploading in server in asp.net c#

i have developed a project. i am uploading this project in a server. i could not connect this with the database.
what will be thw path in web.config file. help me please.this is my connection string in web.config file.
<connectionStrings>
<add name="bcharyaConnectionString" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"/>
</connectionStrings>
should i use database path there?
D:\microsoft sql server\data\bcharya.mdf
this path or
D:\microsoft sql server\data\bcharya_log.LDF
MDF Location
Put your .MDF file in App_data folder.
Connection string
<connectionStrings>
<add name="bcharyaConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bcharya.mdf;
Integrated Security=True;
User Id=myUsername;
Password=myPassword;
User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Add a backslash "\" just after |DataDirectory| in your connection string:
AttachDBFilename=|DataDirectory|\bcharya.mdf;
<connectionStrings>
<add name="bcharyaConnectionString" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\bcharya.mdf;User Instance=true"/>
</connectionStrings>
|DataDirectory| ( Enclosed in pipe symbols ) is a substitution string that indicates the path to the database.
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:
1: For applications that are put in a folder on the user's computer, the database folder uses the application folder.
2: For web application the database folder uses App_Data folder.
You can set the path in Application_Start method in your Global.ascx.cs
AppDomain.CurrentDomain.SetData("DataDirectory", #"D:\microsoft sql server\data");
DO NOT Forget to change
AttachDBFilename=|DataDirectory|\aspnetdb.mdf;
to
AttachDBFilename=|DataDirectory|\bcharya.mdf;
If the database is already attached to sql server change the connection to:
<connectionStrings>
<add name="bcharyaConnectionString" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog = bcharya;User Instance=true"/>
</connectionStrings>
This way you don't need to set the |DataDirectory| path.

Categories

Resources