In my C# windows app using sqlite database, i observed each time i run the application and insert new records into the database, the records will be saved as long as the application is still running, but when i stopped the application, the records will be wiped out(deleted) from the database while using this connection string:
<add name="PITADatabaseConnectionString" connectionString="Data Source=|DataDirectory|\App_Data\PITApplication.SQLite3Db; Version=3;" providerName="System.Data.Sqlite" />
When i change the connection string to:
<add name="PITADatabaseConnectionString" connectionString="Data Source=C:\SqLite\Gui\SQLiteStudio\PITApplication.SQLite3Db; Version=3;"
providerName="System.Data.Sqlite" />
and save new records, it will be saved permanently and not deleted. I don't know what the issue is, i added the database to a folder in my application through add new items context menu, after adding the database to my application i set Build Action property to Content, and Copy to output directory property to Copy always.
i will appreciate your kind assistance.
I was able to resolve this issue by going to application properties window, under settings i added a connection string that points to the connection string in the app.config file. I also set the copy to output directory property of the database to copy if newer, and the issue was resolved, thanks to everyone that contributed.
Related
I've been trying to publish my application so I can install it on other computers but I always end up with an "SqlException" when trying it on the VS editor and when running it on the other computer
to make things clear I made the database using SqlServer and I attach it to the VS editor until now everything is going well and the application is running on the editor without any exceptions
but to make the application working on another computer I needed to copy the (mdf file from the SqlServer directory and paste it in the |datadirectory| of the project ) and of course I changed the connectionString dataSource to point to the mdf file and here where the exceptions begin to rise
The message of the exception says:
An attempt to attach an auto-named database for file
C:\Users\xXx\Desktop\Projects\Master Maint\Master
Maint\bin\Debug\GMAO.mdf failed. A database with the same name exists,
or specified file cannot be opened, or it is located on UNC share.
when I add the Initial Catalog=test in the connection string the exception message becomes:
"Cannot attach the file 'C:\Users\xXx\Desktop\Projects\Master
Maint\Master Maint\bin\Debug\GMAO.mdf' as database 'test'."
PS 1: It's not a permission issue cause I gave full access to the file from the advanced installer
PS 2: if there is the other way to run the database application on the other computer, I'm listening.
<connectionStrings>
<add name="connString2" connectionString="Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|\GMAO.mdf ;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Thank you all , I'm not gone delete the question cause may others need it , but finally I found the answer
1 - the first bug was because the database file is not Categorized and that was solved by adding the Initial Catalog=test
2 - second the database file is named same us the the real database used in the project and that was solved by renaming the file
3 - third the log file that responsible of creating the database was not attached and that solved by adding the log file to the directory
I have a database for user details in my SQL Server and I have already written a C# login application. I need to connect this application to my database to get user data. Before that, I think I should connect my database to Visual Studio using a connection string.
Can anyone please tell me a way of creating a connection string to connect my database to the application?
If you don't have an app.config file just add it by right click your project > add > new item > search box type application then select Application Configuration File then click Add button below. then open the App.config file and paste the code below.
It will be has a name of App.config in your project.
<configuration>
<connectionStrings>
<clear />
<add name="conn" providerName="MySql.Data.MySqlClient" connectionString="data source=localhost;initial catalog=YourDBName;user=YourUsername;password=YourPassword; default command timeout=120;;" />
</connectionStrings>
</configuration>
See this reference to run the code.
Good Luck!
You can use EntityFramework database first convention. The easiest way is to add ADO.NET Entity Data Model from visual studio Project -> Add New Item -> Data and then wizard helps you to connect to your database and creates required objects for tables in code
Note: This is different from Cant find my EF code first database
He didn't specify the initialCatalog while I did as you will see:
I have 3 CodeFirst DBs that I work with.
1 I create and use in my visual studio project when I hit F5
1 I host in Azure with test users. If I want to update it, I simply remove it and recreate it by publishing my VS project
1 that is somewhere in my localDB, hosting it in my IIS in my machine, which is my issue. I can't find it! I don't know where CodeFirst places it. But I can access the website which somewhat brings records from it! Please help me find it.
Here is my entire setup: in my publishing profile for the Local:
lets call the blacked out "ProjAPIContext"
and in my IIS
and the www root folder is simple. The AppData folder is always empty:
and I have this in my web.config
<connectionStrings>
<add name="ProjAPIContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=ProjAPIContext; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|ProjAPIContext.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
the context matches what is in code:
internal sealed class Configuration : DbMigrationsConfiguration<ProjAPI.Models.ProjAPIContext>
That's my setup. I am looking for the DB so I can navigated through table rows and run sql queries against it, etc...
I have looked my entire harddrive with that ProjAPIContext. I see other DB created with the same times frame like tempdb and masterdb but they can't be those.
I have attach the debugger to connect to my server code in my visual studio, but nothing hit. I've run the profiler in my sql, it doesn't print the connection string. It prints useless junk.
UPDATE #1:
I also looked here:
Update#2:
of course I've looked there:
Where else should I look!?
I successfully followed the contoso university tutorial, getting it to work with SQL Express in VS 2010 - creating the DB in the App_Data folder. The tutorial follows the EF Code First model. Wanting to see the data in the tables, I opened the database in Server Explorer and was able to view the table data.
Now when I run it again, I can no longer access the database - the error given is:
One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from a backup.
Cannot open database "SchoolDB" requested by the login. The login failed.
Login failed for user 'DEV\bwilliams'.
Log file 'C:\ContosoUniversity\ContosoUniversity\ContosoUniversity\App_Data\SchoolDB.ldf' does not match the primary file. It may be from a different database or the log may have been rebuilt previously.
DEV\bwilliams would be windows authentication.
Here is my connection string:
<add name="SchoolContext"
connectionString="data source=.\SQLEXPRESS;Initial Catalog=SchoolDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|SchoolDB.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
I tried to open the DB again in server explorer and it tells me that the login failed. So I figured I could drop the database and have EF recreate it - but after doing so, I still receive the same error. The whole point is to create a project and deploy to an actual server, but I can't seem to get past these connection issues.
Update: Changing the name of the database in the connection string creates the new database and all is well. However, opening it up in Server Explorer in VS 2010 causes the same problem. Very frustrating.
I have problem and can't find solution.
On start I have that App.config file:
...
<connectionStrings>
<add name="IdealsadEntities" connectionString="" providerName="System.Data.EntityClient" />
</connectionStrings>
...
In my application (WinForm) I read params for connection to database from xml file and then override App.config and got compiled file AppName.exe.config.
Problem is with creating instance of my entities. I used this code after creating file:
IdealsadEntities _db = new IdealsadEntities();
But in first run application _db.Database.Connection is null. In second run it reading from AppName.exe.config file and works fine.
Questions:
So my question is why first runing read empty string even if I
rewrite this file?
Is DbContext execute earlier?
How to fix it, to get new connection string from rewrited file?
Your app.config is only read upon application startup. Changes you write to it during the runtime of your application will not be noticed until you restart the program.
You can however construct your entities using a connectionstring, like this:
IdealsadEntities _db = new IdealsadEntities("metadata=res://...");
Where you replace the appropriate info in the connectionstring by the data you read from the other file.