Databases in winforms c# - c#

I can't understand how to manage database with Entity Framework.
I'll try to describe my problem step-by-step
create winforms application
add database (.mdf) to solution
when appears window "choosing database model", select dataset
then finish, as database is empty
go to server explorer, choose created database, create some tables
add to solution EDM ADO.NET
The problem is that new data doesn't appear in my database in server explorer. This means that while my Winforms app is working, I can modify my database (change existing values, add new, ..). but if I close my app, there is no update in my database in server explorer. I DON'T forget to use function SaveChanges()
P.S. while my app is working, values are updating. Think, that means that data updates in dataset, but not in database. if i am right, give a clue how to update database from dataset.
pps. visual studio 2010 ultimate. sorry for english
dbentity db=new dbentity();
db.items.addobject(new item() {value=something});
db.savechanges();

Don't add the database to your solution. You can use the Server Explorer to add a connection to your database via your existing SQL Server, or you can select your database in the wizard that appears when adding a new EDMX file to your solution.
Did you try either of these? Where did you get stuck?

Related

My entity data model wizard is not showing new tables

I've created a database from scripts using Microsoft SQL Server 2019 and generated some classes in C# using Entity Framework. Now I've had to modify and add a number of new items to that database, including stored procedures and new tables.
However, on trying to regenerate the classes in Visual Studio 2019, the Entity Data Model Wizard is still showing the old database (the way it looked a week ago), including some tables I deleted. None of the new stuff is listed. I've tried deleting and recreating the database, restarting both programs, and restarting the PC to no effect.
The steps I'm taking to generate the framework in Visual Studio are:
Add a new item to the project.
Select ADO.NET Entity Data Model.
Select Code First from database.
Select Next (the connection string is already filled in).
The next screen is the "Choose Objects and Settings" window, which is where I'm still seeing the old tables (and not the new ones).
Is there some special step I need to take after changing a database to get those changes to show up in Entity Framework?

How to type some values for database table in Visual Studio IDE

I downloaded source code of an ASP.NET MVC book and it looks this, I can see the table but I need to enter some data in it so it can show them to me
Where should I go to enter data?
While you should actually do this in SQL Server Management Studio, Visual Studio does offer you some support to deal with data. To do so, Open Server Explorer (Ctrl+Alt+S) in VS. Create a data connection, if it doesnt exist already to the DB in question by right clicking on Data Connection and click on Add Connection. Since you downloaded the source code for exercises, there is a good chance that they already created one for you. Once you have a connection to your desired database, you will be able to see database object under that including tables. To add data to a table, right click on that table and click on Show Table Data. You can then go to individual cells and enter data. Hitting enter should submit the data change to SQL Server.
Hope this helps..

SQL table getting deleted by itself in local db, using LINQ to SQL [duplicate]

I'm making a WinForms app in C#. When I go to create my database object, I'm presented with two options:
Local Database
Service-based Database
All I want is a simple local database to use for my project.
However, if I select "Local Database (Compact Edition)" then after I create my tables and drag them over to a LINQ-To-SQL Class creator it says:
The selected object(s) use an unsupported data provider.
So, I don't know why creating a local database would be considered an unsupported provider when dragging to tables to the Object Relational designer.
So, I try the second type, "Service-based Database". I lay out my tables how I want for my basic application and make my tables. The LINQ-To-SQL Designer works fine when I drag these tables and I proceed to write my code in my app to insert and upate the database entries.
However, with this second "Service-based Database" my inserts and updates work as long as the application is running. However, once I close it it reverts back to what it was before. If I manually add in data via the Server Explorer it persists but any inserts I do don't save.
Why can't I create and use a Local Database with LINQ?
If that isn't an option, then why isn't the "Service-Based Database" (whatever that is), persisting beyond the application closing?
Thanks for your time!
Edit: Apparently the LINQ-To-SQL Object Designer doesn't work on Compact Edition Local Databases. It can still be done, but you have to use SqlMetal apparently to generate the dmbl file.
When you compile, the old database gets erased and a new one is copied to the bin folder. Therefore try link (using linq) to the database in the bin folder. Try change your connectionString.

Connecting to a Service-Based Datasource in C#

This is a very noobish question:
I don't understand SQL. I'm trying to make a generic connection to an empty SQL database in C#. I don't want to make tables in advance, I just want to be able to open the connection.
Every "connectionstring" I try results in "InvalidOperationException" so I need some help understanding the proper syntax to connect to a SQL data file in Visual Studio.
What I have done is gone to "Add -> New Item -> Service-based Database"
This gives me a "Database1.mdf" in the solution explorer.
Now I want to open a connection through C# to this file at which point I can use SQL strings to manipulate the file.
I believe the next step is to use: SQLConnection SQLConn = new SQLConnection(" ... ");
But I don't know what to put for the "server" or the "database" in the connectionstring.
Can someone tell me the most basic way to connect to a "Service-based Datasource" in C#?
Will this "service-based database" be included in the final compile? E.g. if I compile a finished executable, will each installation come with its own local SQL database?
The service based database will not be embedded in the dll. See the last section of the below (in bold) to see how to obtain the connection string.
From an MSDN article (http://msdn.microsoft.com/en-us/library/vstudio/ms233763%28v=vs.110%29.aspx):
To create a project and a database file:
Create a Windows Forms project that's named SampleDatabaseWalkthrough.
See Creating Solutions and Projects.
On the menu bar, choose Project, Add New Item.
The Add New Item dialog box appears so that you can add items that are appropriate in a windows Form project.
In the list of templates, scroll down until Service-based Database appears, and then choose it.
Name the database SampleDatabase, and then choose the Add button.
The Data Source Configuration Wizard opens.
On the Choose a Database Model page, choose the Dataset icon, and then choose the Next button.
On the Choose Your Database Objects page, no database objects are available because the database is new.
Choose the Finish button to create the database and dataset and add them to the project.
The properties window for the database shows the connection string and the location of the primary .mdf file for the database. To display this window, choose the Server Explorer tab, expand the Data Connections node, open the shortcut menu for the database, and then choose Properties.

LINQ Confusion - Database not persisting after the application closes?

I'm making a WinForms app in C#. When I go to create my database object, I'm presented with two options:
Local Database
Service-based Database
All I want is a simple local database to use for my project.
However, if I select "Local Database (Compact Edition)" then after I create my tables and drag them over to a LINQ-To-SQL Class creator it says:
The selected object(s) use an unsupported data provider.
So, I don't know why creating a local database would be considered an unsupported provider when dragging to tables to the Object Relational designer.
So, I try the second type, "Service-based Database". I lay out my tables how I want for my basic application and make my tables. The LINQ-To-SQL Designer works fine when I drag these tables and I proceed to write my code in my app to insert and upate the database entries.
However, with this second "Service-based Database" my inserts and updates work as long as the application is running. However, once I close it it reverts back to what it was before. If I manually add in data via the Server Explorer it persists but any inserts I do don't save.
Why can't I create and use a Local Database with LINQ?
If that isn't an option, then why isn't the "Service-Based Database" (whatever that is), persisting beyond the application closing?
Thanks for your time!
Edit: Apparently the LINQ-To-SQL Object Designer doesn't work on Compact Edition Local Databases. It can still be done, but you have to use SqlMetal apparently to generate the dmbl file.
When you compile, the old database gets erased and a new one is copied to the bin folder. Therefore try link (using linq) to the database in the bin folder. Try change your connectionString.

Categories

Resources