I have completed my project in c#. I have used SQL Server Compact database (.sdf file). Now, when I compile and run, it works fine. But, how will I be able to run the application in other computers without configuring the connection to database?
In your App.config file add the following after </configSections>:
<connectionStrings>
<add name="NameOfYourConnectionString" connectionString="Data Source=.; AttachDbFilename=|DataDirectory|\student.sdf;Initial Catalog=student; Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Related
I'm developing a Windows Forms application that I need to storage some data as a database in user's system, so that I would be able to access my database through Entity Framework. I mean I need to copy a database (tables should be created already) from the resources directory to specific directory. Then use it with Entity Framework. To be honest I have no idea. That's my problem!
I make sample fixed problem for all project when create winforms & localdb
You can create 2 ConnectionString in App.config file
App.config
<add name="DevConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" /> <!-- *1* Use when publish project-->
<!--<add name="AppConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='E:\My Projects\MyDatabase.mdf';Integrated Security=True"
providerName="System.Data.SqlClient" />-->
<!--*2* Use for development-->
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
I'm currently completing an ASP.NET web app assignment which requires a n-tier design. On the marking guide for database design and implementation 1 mark goes towards:
"A DB login created to use for the Web Application connection String"
I've contacted my lecturer but no response about what I want to ask.
I have to hand in the SQL script as a script file. I'm wondering how do I create a DB login in the script? Or what does this mean when you read it.
Or am I over thinking this as I've already created a connection string and I've placed it in the web.config file which looks like this
<connectionStrings>
<add name="ConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DB; Id=myUsername;password=myPassword;Trusted_Connection=False; Integrated Security=True; MultipleActiveResultSets=True" />
</connectionStrings>
<connectionStrings>
<add name="DBConnection" connectionString="server=192.168.1.21\DBServer;database=DataBase1;user id=sa;pwd=wins123;Pooling=True;Max Pool Size=1000;Connection Timeout=2000;"/>
<add name="DBConnection1" connectionString="server=192.168.1.21\DBServer;database=DataBase2;user id=sa;pwd=wins123;Pooling=True;Max Pool Size=1000;Connection Timeout=2000;"/>
</connectionStrings>
server=192.168.1.21\DBServer Instead of this you can put Your SQL Server Name
I created a working Data-Based Database. I can also use this db with the DataGridView.
But how can I create a connection to the database with C#? I searched already before but nothing worked for me.
In my App.config is also
<connectionStrings>
<add name="make_it_server_verwaltung.Properties.Settings.ServerlistConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Serverlist.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
So how can I create a connection to my db and how can I add/delete/change/read my database over C#?
Thank you ^^
Working DataGridView
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!