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-->
Related
I use Entity Framework 6 code-first in a Windows application to connect to a SQL Server database. How could I edit my code to enable me or any app user to change the database name without getting any errors?
You can set the connection string separately in the configuration file and then use ConfigurationManager class to read strings.
But to change only the database name without other errors, you must ensure that other settings are the same. Otherwise, you must modify the corresponding code or settings according to the actual situation.
For example: add in the configuration file (config)
<connectionStrings>
<add name="MyDbContext" connectionString="Data Source=myServer;Initial Catalog=myDatabase;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Then call it like this:
string connectionString = ConfigurationManager.ConnectionStrings["MyDbContext"].ConnectionString;
For detailed examples, please refer to documentation.
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'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.
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!
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>