In my app I use ORM Designer which supports only 1:1 relation. I have Items, Products, Shipping tables: Item has ProductId and ShippingId attributes which are foreign keys.
I need to upload data from xml file into my SQL DB, so I gonna use SqlBulkCopy class and I need to create DataTable objects: itemsTable, productsTable, shippingTable to load data from xml into them.
So, do I need to create DataRelations: ItemsProducts and ItemsShipping? If so, itemsTable would contain productsTable and shippingTable?
Thanks!
Edits:
And is it possible to write to server the content from 3 dataTables simultaneously?
This isn't a direct answer to your question, but it's entirely possible to have a one-to-many relationship in a .dbml file. See the screenshot below, which is simply the properties sidebar you can get to by right-clicking on an association and choosing "Properties".
If you use SqlBulkCopy, you have to do it using one table/entity at a time. It will work if the entities already have the foreign key values. BTW I created a wrapper object for SqlBulkCopy that can do a bulk insert for a collection of objects.
It may help you, you can find it here:
http://www.codeproject.com/Articles/354094/Bulk-Insert-in-NET-applications-Part-1
Related
I have an automation project that I use for several similar sites.
The databases of the sites are also similar beside the USERS table.
In one database the table USERS has the column Fname, in the other one this column is not present. In the other database I have a column named RegesrationDate and in others this column is not present. etc. etc.
When I am running a query I get the error
specified cast is not valid.
because I can't cast the return value (which has more columns then I am holding in my dbml file).
There is a way in my project to make a generic dbml object that I will run:
SELECT * FROM USERS
and to get a normal object. Do you have other ideas how to make my project work?
first approach:
My suggestion is to remove all unique fields and get the generic table at first.
After that, use db.ExecuteQuery for the unique fields every special db.
second:
It's not generic db, therefore use two (or more) user table on your dbml file.
userDb1 table, userDb2 table....
in c# do:
if(dbname == 1){
var a =db.userdb1.select()....
}
Now i'm sure this is an amateur question and that i'm going about this the wrong way, that's why i'm asking here! So please go easy.
I'm trying to create a database in Access 2013 that will store orders for a POS system (It isn't a commercial product, it's a computing project in C#). It contains several tables.
My issue is that I can't have multiple menuitems for every orderitem. Is there a way to do this?
Thank you in advance!
Create a MenuOrderItem table that goes between OrderItems and MenuItems. It will have a 1-to-Many relationship to both OrderItems and MenuItems. That's how Many-to-Many relationships are built in relational dbs...with an intermediary table.
In your case, to do this, remove the relationship between OrderItems and MenuItems. Create table called MenuOrderItem (or something similar). It should have just two columns in it: a column named OrderItemId, and a column named MenuItemId. These columns will be used to make the two new relationships: the first between MenuOrderItem and OrderItems and the second between MenuOrderItem and MenuItems.
One way would be to put a table between orderItems and menuItems with 2 columns. orderItemID and menuItemID. Then create your relationships. This would allow you to have one orderItem with many menuItems.
I have a database where I have a table that will have columns added/removed by the customer (We can't change this behavior, it's legacy).
This table has a foreign key to a table that I've mapped through EF. I'm trying to figure out a way to sort/filter the parent table by one of the dynamic columns using EF.
Is there anyway to add to the query for a column that doesn't exist until runtime?
My current solution is to get all the records, then get the corresponding records in the other (dynamic)table as a dataset, and combine the two in a new class, and sort a list of those objects.
Unfortunately, I have no code to show for this as my already working solution isn't what I want, but I have no idea where to start with a better, optimal solution.
Without some example how or what you wan't we can't help much. If you wan't to work with queries runtime, then linq is a good solution:
http://www.c-sharpcorner.com/UploadFile/deveshomar/dynamic-linq-library-in-C-Sharp/
I have two tables, one containing patient information, the other, the notes for each patient.
(One patient, many notes for a patient).
Given this, in the Designer (which you access by right-clicking on the chosen DataSet), how do I create a one-to-many relationship? I have never performed this before.
Secondly, for the patient notes table, how would I add a note to a patient record using SQL syntax? Note, this is not updating an existing one, but adding a completely new one to the patientNotes table using the unique patient ID number as the reference (so only that specific patient has that note added to them, not them and everyone else).
Very technically speaking, you don't need to do anything to create a one-to-many relationship. You just have to have the two tables set up as you have them and use them as you intend on using them. I work in data warehousing and unfortunately a great many of our relationships like this are not formalized with any sort of key or constraint.
The correct way to do it is to implement a foreign key constraint on the patient ID column on the patientNotes table. A FK will only allow you to insert data into patientNotes IF the patient ID exists in the patient table. If you would try to insert a note into your table that has a patient ID that doesn't exist in the patient table, the insert would fail and the SQL engine would give you an error. Note that the column on the patients table that you are creating the FK to must be a primary key.
Inserting data will really go as any other insert would:
INSERT INTO dbo.patientNotes (patientId, NoteText)
VALUES(4265, 'During his 8/14/2014 visit, Mr. Cottinsworth complained of chest pains. Evidently he has been wearing a lady''s corset to hide his large gut. Advised the very portly Mr. Cottinsworth to discontinue corset use'
You could toss that in a SP, put it in your code and use parameters for the patientId and NoteText, however you wanted to do it.
As far as doing this all in Visual Studio graphically, I can't be of much help there. I typically use the TSQL editor and type out what I want to do to the DB. I'm sure there are tutorials abound on how to set up FKs on Visual Studio.
Further reading:
http://msdn.microsoft.com/en-us/library/ms189049.aspx
http://www.scarydba.com/2010/11/22/do-foreign-key-constraints-help-performance/
what are the advantages of defining a foreign key
Sir actually I'm getting data from three tables in database. I use a query in database that has inner joins that work fine for me and I populate dataset from that query, but issue is that I want to separate the fields that belong to different table in each record.
for example: I have a student, teacher and class tables in db. I fetch data using inner joins in between these tables. i populate dataset from that result. Now in front end I want to separate data that belong to separate table, i want student fields separate, teacher fields separate. what should I need to do? what operation require me to perform on dataset to achieve such scenario.
If you use stored procedures you can return 3 differents select statements instead of using joins. You can then access the tables like so:
ds.Tables[0].Columns["studentid"]
ds.Tables[1].Columns["classid"]
ds.Tables[2].Columns["teacherid"]
You have to create a new data set which contains the three distinct tables with their correct relations.
If you already have the structure within your database you can within Visual Studio simply click within the menu bar on Data - Add new data source and select Database - Dataset in the first wizard dialogs. Afterwards define the connection string and select all the needed tables, views, etc.
When you have finished the wizard you'll get a new dataset containing all the desired tables and their relations to each other. Within the designer you can then open the Data - Show data sources panel and drag/drop the desired tables onto your form/control and also create quite easily master/detail views.
Loop through your data set and since you know your columns that will be the result of your query, you can do something like this.
ds.Tables[0].Columns["student_colummn1"]
ds.Tables[0].Columns["student_colummn2"]
ds.Tables[0].Columns["student_colummn3"]
ds.Tables[0].Columns["teacher_column1"]
ds.Tables[0].Columns["teacher_column2"]
ds.Tables[0].Columns["teacher_column3"]
ds.Tables[0].Columns["class_column1"]
ds.Tables[0].Columns["class_column2"]
ds.Tables[0].Columns["class_column3"]
If possible look into LINQ, makes your life easier. Hope this helps.
Don't use a dataset. Define separate classes for student,teacher and class tables. Use your DAL to populate these classes & access them separately in your application.