DataSet - easy way to create new table? - c#

I use Visual Studio 2010 with C# to write a database application. Until now, I used the guided dialog to add a DataSet to my current project. In the project explorer tree on the right side, a node "mydatabase.xsd" is created.
On the left side, I have the server explorer. From there, I can drag existing tables onto the DataSet-designer to create the references (to my understanding, VS auto-generates code in the background). The references (or TableAdapters etc. respectively) can be used in the source code, in the form of mydatabase.mytable
This is very convenient as I don't have to deal with the details of the connection settings, and it keeps SQL code out of my eyesight.
If, however, I want to create a new table in the existing database, VS does not seem to offer any assistance. I looked at the MSDN-specification. They tell you how to create the table object and the column definitions and how to link it to the DataSet, but not how to actually save it into the database. In all the examples I found on Google, you'd have to explicitly, manually, define a ConnectionString, SQL commands or similar. With all the VS GUI advancements, this seems rather convoluted to me. Is there an easy way to accomplish it?

Open Server Explorer, click Add Data Connection and supply connection information to your database, click OK. Right Click on Tables folder and click Add Table. Add Fields, etc to your new datatable.

Related

How to update a C# project with a data source when the SQL database has been modified

I have created a database with several tables. Using Visual Studio 2012, I created a DataSource object that connects to those tables and created a DataGrid and some forms using the data from the SQL database.
In the process of development I then made changes to the SQL database. In one case I designated a column as a key in a table I had neglected to choose a key for. Now I want my application and the DataSource object to recognize these SQL changes.
How do I do that?
OK Bill, I believe I understand the problem now. Yes there is a way through the GUI is Visual Studio to pull the changes from your database into the Dataset you have created. Here is how you do it. Open the Data Sources Tab -> Select your DataSet You can see that mine is called People.
Now, I have updated my database by adding another column called "Phone" that I want to add to my dataset. I right click on the dataset and select Configure Data Source With Wizard.
I will expand the data objects selection and select my new column value.
Then click finish and my Dataset now has the new column in it.
I hope this helps you.

Entity Framework Error 3004 while trying to add a field into a table

this question is about adding a new field called "Position" of type integer into an existing table via the Entity Framework designer. What I expect to happen (correct me if I am wrong) is add the field, select to update the database via the model and get the new field created in my database. Instead of this, I get an "Error 3004 Problem in mapping fragments at line xxx". My environment is using Visual Studio 2015 with EF5, and the database is hosted on Oracle MySQL. The project I am working on is a website which was given to me to maintain and upgrade further.
Here are my steps:
1. Discover the table in the model browser, click Show in Diagram.
2. Inside the model I right-click on the table -> Add New -> New Scalar Property and type the new field "Position" inside the table.
3. Click save icon to save the model
4. Then I get an Error 3004:
5. I realise that in the mapping details my new field isn't showing and I can't type in there to add it manually.
6. I can see the new field has been added to the syncitem class in fact I want this to be an integer so I go ahead and change it by hand.
Tried to click save again and re-compile but I still get the same error. I need to say at this point that I have spent an entire day searching around on the web about various error codes I got other than just 3004 but here is where I stopped and turned to SO for guidance. I know there are other threads here about Error 3004 and I did read quite a few them. One of the pieces of advice I found was to change the DDL generation template to one that supports MySQL since Visual Studio will default to SQL (this is done by clicking on the white space inside the designer and then clicking the Properties tab):
I tried that as well but nothing changed, the error persists... I also tried deleting the entire table in the designer and updating from the database, the table re-appeared but with a torrent of "Error 11007" lines, plus the table's associations are not wired correctly. Another thing I did was to open the .edmx file in Notepad++ and see if I can add my new field in there, but it just felt wrong doing it like that so I quit.
Am I approaching this in the wrong way? All I want to do is add a new field! I have come to a stop, my experience with EF is not great, I felt lots of frustration with this database's particular setup, quite frankly I dare say that I think it is somewhat broken. It's like dealing with a brand new religion whenever I have to touch it!
In the long run I want to upgrade to EF6 and get rid of the designer but that's another story.
Any advice on my problem will be greatly appreciated.
Thanks.
Eventually I had to tamper with the XML file of the model (the .edmx file) to manually add in my new field and set it to integer, as there was nothing I could do to satisfy Visual Studio. The web project I am working on looks rather complicated with loads of sub-projects in it like "DataAccess", "EntityModel", "ManagerLayer", my web folders and a host of other directories. I think my EF needs a complete re-do, my guess is that it started with a very old EF version and is shuffling its feet right now in terms of maintainance.

How to change the table name in visual studio 2013 in design mode?

I created a SQL database table in Visual Studio 2013. I want to rename it but the name property is disabled. How can I change the table name?
In Server Explorer right click on Views and click New Query.
use this code to rename table:
EXEC sp_rename 'Table', 'NewName'
then click on Execute button.
after 5-30 seconds in server explorer click on refresh button.
You can change table name in SQL Server Object Explorer in Visual Studio. Choose the table in tree and rename by right-click on the table -> rename
The correct answer as follows:
1.First select your table that you want to change.
2. Then change the name in the script pane.
3.Finally in the upper-left corner of the Table Designer, choose the Update button.
Please as shown below:
Ive been trying to too, and the simple option to rename the [dbo][Table] did not seem to work.
But it actually does! Please note that refreshing doesn't work right away.
Steps:
rename [dbo][Table] to [dbo][yourTable]
press update button
Refresh a few times, it may take a minute.
Hope this helps :) It seems a like really wierd bug.
You can change table name in T-SQL part as showin in image as follows..
CREATE TABLE{dbo].[t1]{
{
...
...
}
to
CREATE TABLE{dbo].[t2]{
{
...
...
}
ans then press "Update" button present above...
As explained here, found through "visual studio server explorer rename table", you can't.
There is a workaround: add the tables to a database diagram, rename it there and save it.
(using VS2013)
I came looking on here because I had written the main table for my project this afternoon, and as usual set it's name in T-SQL. But had not realized that the name had not stuck until I went to use it in the Linq-to-SQL I tried over and over to rename it, and I had refreshed many times.
The default name 'Table', has an obvious clash problem in normal code.
Then tonight came on here to see if you guys had found a way and opened the project to try some out.
Turns out that although the original table called 'Table' was still there, I now had many copies of it, one for each attempt at renaming it.
So I think from that we can assume the table renaming problem is a bug rather than a feature, and that MS use a copy first, then should do the deete of the file that is being renamed. But also that the refresh does not work on these copies until the project is closed and then reopened. Yes I know, a bit cheesey, but at least there is a way around.
Personally, with this and the database overwrites at run time, I am starting to lean towards letting the database designer create the scripts for my program to use rather than creating the tables and other database parts. That puts everything back into your hands, which must be a good thing.
I haven't quite got to grips with doing the Linq-to-SQL manually yet, and it is such a bonus to a programmer that I have to tackle that first.
This will help if you accidently put a dang period in your table name
EXEC sp_rename '[dbo].[Vb.Net]','VB'
Just rename the
[dbo][Table] to [dbo][TableName]
press the update button.

Using a database or table structure within program

Basically all I want is to be able to create like a self contained database in my Windows Forms application, I do not want to connect to a server or anything like that with SQL Server, I just want like a small database in the application that can handle a few transactions.
Like for instance if I create a Windows Forms application then it would have its own small database in it and when deploying it.. it will save rows there and stuff. I remember hearing about a plug in, it started with a CT or something don't recall, but it would be a plugin for Visual Studio.
Any help would be much appreciated.
Regards
You can use SQL Server Compact or SQLite (ADO.NET provider is available here). Both are embedded database engines that run in-process. You don't need to install anything, just include the appropriate DLLs with your application.
Visual Studio has no problem providing you with just such a database. Just right click your project, add new item, and select SQL Server Database. The database created will be part of your project and can be deployed as needed.
If you have a limited amount of data you can just right-click the project and Add a DataSet. Then you can define multiple tables in the .xsd and store multiple rows. in the .xml. Then you can write some simple code in the .xsd code-behind to auto load and save defaults and even pull back rows of data. I typically use this approach for storing a single record in each table. It will also work with multiple records in each table. There's a point where if you start to get a lot of data you'll probably wish you had used something like SQL Compact Edition or something similar that you bundle with your app.
you can use sqlLite,this is the website
http://www.sqlite.org/
or you can use access too

C# where does the dbml file come from?

I am currently learning C# and LINQ. I have lots of questions about them. Basically, I need a step by step tutorial.
I suppose the dbml file is the configuration file of the database. If I double click the dbml file VS will open it in a design diagram. Can I create/delete/modify tables here? I can use add new item to add the LINQ to SQL Classes to get a dbml file?
What's next? Generate tables in database? Generate SQL script? Generate cs files? When? How?
The DBML file is not related to the database server at all. It's a completely client side thing. It's essentially a set of information about your tables in the database and how you're going to map them to .NET objects.
Of course, you can drag a table from a database and have Visual Studio infer some information for you automatically, but changing the file will not affect the database. You can create a DBML file from scratch without any database too.
Internally, the DBML file is simply an XML file that's fed into a custom tool by Visual Studio and generates .cs files representing the LINQ object model for your database from it.
The DBML file is mapping that defines your classes based on your database schema. Yes, it defines your (default) connection string, but it doesn't "configure" your database at all.
Linq to Sql uses a database-first approach where you have the database and model your classes after the DB schema. By dragging & dropping the table onto there, you'll be automating the creation of the classes so you don't have to type them out. You can change property names etc from there and the mapping between the property and its correct database column name will remain intact.
this link learn how to create dbml File in your Project :
http://dotnetlearners.com/linq/linq-to-sql-creating-dbml-file
Implement LINQ to SQL first we have to create the DBML file, the DBML file contains the C# source code which allow us to write the LINQ Queries to SQL. Here is the step by step process to generate dbml file.
Right click on the project (or project folder) and select the option Add New Item.
add-new-item
Select LINQ to SQL Classes Template and Give Name as MyDB.dbml.
linq-to-sql-classess
Click on Yes when the below confirmation displayed.
dialog
MyDB.dbml file will be added under App_Code folder.
dbml
Expand Server Explorer and Right click on the Data Connections and select the option Add Connection.
add-connection
Add Connection Pop up will be opened, give the SQL Server details and select the then click on OK button.
server-detail
Database will be added under Data Connections as shown below.
server-explorer
Drag the table in the left pane and if primary key & foreign key relations are there then it will automatically show as shown below.
drag-tables
Drag the stored procedures to the right pane.
drag-store-procedures
Related C# code will be automatically generated and we can see by opening the file MyDB.designer.cs.
dbml-designer-cs
Yes, the DBML file is created when you add a Linq to SQL class. In the designer (what you see when you double click the DBML file) you can drag tables (from the server explorer) onto it. You can then reference these tables in your code. There are more than a few getting started tutorials out there:
Check this SO question for details:
https://stackoverflow.com/questions/481244/can-anyone-recommend-a-good-tutorial-for-learning-linq2sql
Walkthrough: Simple Object Model and Query (C#)
Consider SqlMetal
PK :-)

Categories