I created a database following exactly this tutorial: https://www.youtube.com/watch?v=OdDkFPO_nto
But when I restart the computer, the tables I created are deleted everytime. I created them in Visual Studio (right click, create new table, type in the columns, click Update, Update OK) and tried to use them (check if the data exists via C#, printing the items of the columns (rows) to the console) and it worked. I closed Visual Studio, opend it again and everything works fine, I can execute the code multiple times without any issues. But when I restart the PC, all tables are gone. I also tried creating the tables in Microsoft SQL Server Management Studio 17, with the same outcome - everything cleared after restart.
How to correctly save the data/the tables?
here is the App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="provider" value="System.Data.SqlClient" />
<add key="connectionString" value="Data Source=ANGELUS-PC;Initial Catalog=tempdb;Integrated Security=True"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
You are creating the tables in the SQL Server temporal (internal) database. It is a system database not intended to users. Create a different database and use it.
Related
I've created a basic windows form application using the Service-based Database option so that when I deploy it on another pc it will not require to install sql server there.
I've added a LINQ-to-SQL class in the project and here is my full code
And here is the app.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<connectionStrings>
<add name="_16Sep18_databaseAppWithSetup_.Properties.Settings.WrestlersConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Wrestlers.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
The problem is when every time I run my program the previously stored data is no longer in the database but when I input data and perform the insert,delete,update etc operations it works and the data is shown in the datagridview also but once I close the app all those data are gone.
Why is this happening and how do I fix it?
This problem occurs because the mdf file is saving in DEBUG folder also when you try to run the program...
Just go to app.config file,,
it seems like you have added the directory like
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Data\Database1.mdf;Integrated Security=True;User Instance=True
change the |DataDirectory| to full data directory address like
"AttachDbFileName=c:\Project\Data\Database1.mdf"
it will work
I resolved this problem by changing "copy always" => "copy if newer":
It worked for me. Hope this helps you.
The MDF file is copied to the debug folder on each run, and that is the file your code manipulates, not the one in your source folder.
The problem is that the table adapter keeps referencing a connection string that I have not set up for it. When I go to each data table in the DataSet Designer, the connect says "MyConnectionString(settings)". When I search for the incorrect connection string, VS can't find it.
The project that is reused over multiple solutions. I have three configurations: Debug, Staging and Release. Each configuration has it's own connection string. My app.config looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings configSource="connect.config"/>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
</configuration>
Each configuration file looks something like this:
<connectionStrings>
<clear/>
<add name="Properties.Settings.MyConnectionString" connectionString="Data Source=CorrectDataSourceforthisConfig\SQL;Initial Catalog=MyDB;Trusted_Connection=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
In my dataset, I have this XML:
<Connections>
<Connection AppSettingsObjectName="Settings" AppSettingsPropertyName="MyConnectionString" ConnectionStringObject="" IsAppSettingsProperty="true" Modifier="Assembly" Name="MyConnectionString (Settings)" ParameterPrefix="#" PropertyReference="ApplicationSettings.MyMenu.Properties.Settings.GlobalReference.Default.MyConnectionString" Provider="System.Data.SqlClient" />
</Connections>
In my settings.designer.cs, I have this:
[global::System.Configuration.DefaultSettingValueAttribute("Data Source=CorrectDataSourceForDebug\SQL;Initial Catalog=MyDB;Integrated Security=True")]
public string RMSConnectionString {
get {
return ((string)(this["MyConnectionString"]));
}
}
Where is this rogue connection string coming from? Any help, ideas, advice and opinions would be greatly appreciated.
The connection string is stored in your app.config file as well as in your sometimes in your dataset and sometimes in your code. In my case, I was able to fix this problem by going into Explorer and deleting all the files that I had accidentally created (i.e. Form1) and by searching my solution and making sure tha there were no reference to the incorrect connection string. Then I deleted all instances of the .DLL that I had used when I included this project in different solutions and re-referenced and rebuilt all the projects.
There is also machine.config which is the master configuration file on your system. This may be where your hidden connection string is stored.
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/connection-strings-and-configuration-files
The machine.config file also contains a connectionStrings section, which contains connection strings used by Visual Studio. When retrieving connection strings by provider name from the app.config file in a Windows application, the connection strings in machine.config get loaded first, and then the entries from app.config. Adding clear immediately after the connectionStrings element removes all inherited references from the data structure in memory, so that only the connection strings defined in the local app.config file are considered.
I’m a student. And this is my first production level project. I’m developing a WPF application using Entity Framework, which will be running only on a tab. I have problem in choosing a database. Since this application is going to run only on one device and cloud database cannot be used, what would be the best option?
If I use mssql the connection string in my development environment and production environment differs. Am I wrong? If I’m right what would be the solution for having a connection string that works identically in both environments.
Thanks in advance.
Usually you always end up with a different connection string for development (ex: without password) and for production (ex: long password).
C# handles this with App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="yourname" connectionString="..." providerName="MySql.Data.MySqlClient" />
</connectionStrings>
</configuration>
And then you'll add a transformation file that changes certain values of your App.config depending on the environment where you'll deploy it.
Typically there is an App.Release.config file that updates the connectionString when built in Release mode (versus Debug mode)
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="yourname"
connectionString="productionConnectionStringHere"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
As to which database you're going to use: It doesn't really matter. Have a look at Sqlite. Or Mongo if you don't have tabular data.
I had developed C# application. and used external tool for themes.
Before creating setup for this application it works fine. and no problem in database connection.
But after creating setup for this application and installing it, connection to database not created.
I have used app.config file for getting connection string. Am I missing something when creating setup or the problem due to external theme?
My app.config code
<?xml version="1.0" encoding="utf-8" ?>
<configuration> <connectionStrings>
<add name="connectionStrings" connectionString="Data Source=SUMEET-PC\SQLSERVERNEW; Initial Catalog=ClothShop;User Id=sa;Password=123;" />
</connectionStrings>
</configuration>
I'm using SQLite so it's only a file, not a server, but here's the plan:
Install the application + the physical .sqlite file on a machine and on another machine over the network, let the other user connect to the .sqlite file.
So I'd have to share that folder and give it permissions to allow everyone to read and write from it.
Here's my App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="ColegioMentorEntities" connectionString="metadata=res://*/Repositories.ColegioMentor.csdl|res://*/Repositories.ColegioMentor.ssdl|res://*/Repositories.ColegioMentor.msl;provider=System.Data.SQLite;provider connection string="data source=C:\Users\Sergio\Desktop\ColegioMentor.sqlite"" providerName="System.Data.EntityClient" /></connectionStrings>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" />
</startup>
</configuration>
It seems I CAN change the connection string! Great! But how do I reference a networked location in a string?
As suggested here, you may want to consider using something like MySQL if you need to support more than a single user, due to potential corruption issues that can arise when you access SQLite concurrently.
Though SQLite does support concurrent access, there are several caveats that you can read about on their FAQ (see items 5 and 6).
From that page:
sharing an SQLite database between two
or more Windows machines might cause
unexpected problems
The first thing that comes to mind is a standard UNC path, like:
\\ServerName\ShareName\Database.sqlite
I hope this helps!
To answer your question about the ShareName, it would be the shared name of the shared folder, as shown in the properties dialog > Shared Tab > share name, not the windows explorer folder name (assuming you're using windows).