I have 3 tables simplified to the below:
main_table
id | attribute1_id | attribute2_id | price
attribute1_table
id | attribute_name
attribute2_table
id | attribute_name
I've created a view in Sql Server that joins the tables together to give me the following output:
main_table
id | attribute1_id | attribute2_id | attribute1_name | attribute2_name | price
The problem I have is I want to be able to show the data in a DataGridView and allow the price to be editable. But I've created a "view" which I take it this is not the correct thing to use (i.e it's called a "view" which doesn't sound editable?)
I know I could create my own script to go through and update only the "main_table" but I think there must be a way to use DataGridView / Linked datasets with joined tables?
-
Best thing I can advise is to create a stored procedure that takes all of the parameters and then within that procedure do the individual update statements to the table. Should work fairly well.
Related
i have search page where in there are 2 filters. one is the city and other is the service.
city values are in usertable, where city is column , and service values are in an other service table
when i search on applying both the filters the output must display the answer merging both the result into one grid view /
please suggest logic how can i do ?
You want to join 2 tables together using an SQL query.
You can do what you ask in the same manner as below.
First let's imagine the structure of 2 tables, let's choose Human and Address as our tables.
Here's the Human table:
ID (PK AutoIncrement) | FirstName (nvarchar(50)) | LastName nvarchar(50)) | AddressID (int) |
1 | Bob | Bobson | 8 |
2 | Dob | Dobbers | 9 |
Here's the Address table:
AddressID (PK AutoIncrement) | HouseNum (int) | PostCode nvarchar(6)
8 | 2 | AL23FP
9 | 37 | AL23UR
You see that we can join these tables based on their AddressID as they share the values are mutual.
So you can query these 2 tables by doing something like this:
SELECT h.FirstName + ' ' + h.LastName AS Name, a.PostCode FROM HUMAN h
INNER JOIN Adress a on h.AddressID = a.AddressID
That's the basis for applying the query to your filters just with different table column names.
INNER JOIN will keep only the matching correspondance between the 2 tables if there's mutual info in both.
You can see other Joins here
If you are using stored procs in your SQL db, one way is to write a stored proc that takdse city and service as a parameter and returns the data using a SQL Join. In your c# code or class, you can call the SQL stored proc and map it to a DataTable and parse the results and JSON serialize the data to send it back to the client. If you are using Enitity Framework, you can do the same thing with Linq queries and map it to your c# model objects. Then you can JSON serialize that data and send it back to the client. On the client side code, you can parse the list and display it in your grid in whatever client framework you are using.
It would help to know more details about your server side and client side frameworks.
In our database we have this parent - child - grandchild relation that is many-to-many relationship ( twice ). This happens through two junction / cross-reference tables. Parent/Child/Grandschild tables have varchar functional keys that are unique. Below is a simplified version showing only the first step in the hierarachy:
Parent Junction Child
+----+-------+ +------+------+ +----+-------+
| PK | F_KEY | | PK_1 | PK_2 | | PK | F_KEY |
+----+-------+ +------+------+ +----+-------+
| 1 | AAA | | 1 | 1 | | 1 | BBB |
+----+-------+ +------+------+ +----+-------+
The number of records in both parent / child / grandchild are several millions.
Situation
We need to deal with the situation where we're given a collection of parent-child-grandchild and some of them may already be present in the database. We need to insert the ones that are not yet present, ignore rest ( based on functional key ).
So the current implementation:
switches off autodetectChanges and disables all constraints on the datacontext.
checks for parents already present ( using F_KEY ) - inserts non existing ones
checks for children already present ( F_KEY ) - inserts non existing ones and I think manually updates EF
idem for grandchildren
Not surprisingly - something went wrong and now we have missing links in our junction table and we're having to fix this through scripts.
This implementation doesn't sit well with me. Argument of the dev was performance. Original implementation did not perform:
Given list of parents - ignore existing ones
Look at remaining children - replace existing ones with DbEntries
Idem for grandchildren
SaveChanges()
Didn't perform. My colleague said - 'think about it: you have to enter parents, then retrieve the id's. Save children, retrieve id's, use these for first junction table etc.'
Question
How can I make this perform? I mean - it works, but not very maintainable and really rubs me the wrong way.
An idea I had - if we make the junction table contain the unique functional keys like so:
Parent Junction Child
+----+-------+ +------+------+ +----+-------+
| PK | F_KEY | | PK_1 | PK_2 | | PK | F_KEY |
+----+-------+ +------+------+ +----+-------+
| 1 | AAA | | AAA | BBB | | 1 | BBB |
+----+-------+ +------+------+ +----+-------+
Then we don't have to retrieve the ids of the inserted items to store them in the junction table. Does that make sense? Will EF be able to benefit from that?
If that doesn't work - and we're not using EF in the way it's at its best - we might as well consider using stored procedures or direct queries to the database. You save the overhead of EF altogether and at least then you're in full control of what we're doing and not have EF make the queries for us behind the scenes.
What are the thoughts on that? Any other suggestions are very welcome as well of course.
For this kind of task I would make a stored procedure that accepts few table-valued parameters https://msdn.microsoft.com/en-us/library/bb510489.aspx https://msdn.microsoft.com/en-us/library/bb675163(v=vs.110).aspx with the list of new Parents, Children, Junctions, GrandChildren, Junctions and perform all merging on the server inside one transaction without transmitting anything back to the client.
A bunch of MERGE T-SQL statements processing rows in bulk worked quite well for me in similar cases.
Merge Parents, then Children, then GrandChildren tables. Then Junction between Parents and Children. Then Junction between Children and GrandChildren.
As long as the size of collection that you need to merge is reasonable (say, around 10K rows) it would work very well with a single call to the stored procedure. If you have to merge significantly more rows, consider splitting them in smaller batches and calling your stored procedure several times.
I was wondering if there was a way to qualify the table mappings when using sqlbulkcopy in c#?
Currently, I have a table that contains Stock Codes and then columns associated with range of weekly bucks.
example:
Stock Code | 11-2013 | 12-2013| 13-2013 | 14-2013 etc etc.
I have a query that returns quantities for the given stock code and the week number in which they occurred.
example:
part a | 20 | 11-2013
part b | 10 | 14-2013
Ideally, there would be a way to set the columnmappings.add method and specify that I would like to map the date column of the table to the resulting date in the return row of the query. I would show what I have; however, I have no idea if this is even possible. Any suggestions or alternative ideas would be great.
Thanks
Not directly possible. Your source data has to match to your destination data. The SqlBulkCopy class isn't going to do that for you.
Create a sql query from your source data that matches the table schema of your destination table. Then you can use the SqlBulkCopy class.
I am wondering if it is possible to have a table in C# which allows one of the columns to appear as a row so each record is effectively two rows.
I am attempting to create a search engine for documents. I would like document properties such as document title, date created to be put into columns and also have an extract of the document in a column as well however I feel it would be more appropriate if the extract was on a new line similar to how google displays results with a page extract. I will be grateful for any advice on how this could be achieved. I am currently considering creating a jQuery component and loading it in this way unless there are any easier methods? Below is a depiction of how I imagine the table to look:
-----------------------------------------------
|Col 1 | col 2 | col3 |
-----------------------------------------------
|Data | data | data |
|Contents of col4 |
-----------------------------------------------
|Data | data | data |
|Contents of col4 |
-----------------------------------------------
Store the "extract" in it's own column in the database (each row is one "document"). Then, in your view, you can have it be displayed as it's own row in the HTML table. No need for jQuery.
I'm working on a local city project and have some questions on efficiently creating relationships between "parks" and "activities" in Microsoft SQL 2000. We are using ASP.NET C# to
I have my two tables "Parks" and "Activities." I have also created a lookup table with the proper relationships set on the primary keys of both "Parks" and "Activities." My lookup table is called "ParksActitivies."
We have about 30 activities that we can associate with each park. An intern is going to be managing the website, and the activities will be evaluated every 6 months.
So far I have created an admin tool that allows you to add/edit/delete each park. Adding a park is simple. The data is new, so I simply allow them to edit the park details, and associate "Activities" dynamically pulled from the database. This was done in a repeater control.
Editing works, but I don't feel that its as efficient as it could be. Saving the main park details is no problem, as I simply call Save() on the park instance that I created. However, to remove the stale records in the lookup table I simply DELETE FROM ParksActitivies WHERE ParkID = #ParkID" and then INSERT a record for each of the checked activities.
For my ID column on the lookup table, I have an incrementing integer value, which after quite a bit of testing has got into the thousands. While this does work, I feel that there has to be a better way to update the lookup table.
Can anyone offer some insight on how I may improve this? I am currently using stored procedures, but I'm not the best at very complex statements.
[ParkID | ParkName | Latitude | Longitude ]
1 | Freemont | -116.34 | 35.32
2 | Jackson | -116.78 | 34.2
[ActivityID | ActivityName | Description ]
1 | Picnic | Blah
2 | Dancing | Blah
3 | Water Polo | Blah
[ID | ParkID | ActivityID ]
1 | 1 | 2
2 | 2 | 1
3 | 2 | 2
4 | 2 | 3
I would prefer to learn how to do it a more universal way as opposed to using Linq-To-SQL or ADO.NET.
would prefer to learn how to do it a more universal way as opposed to using LINQ2SQL or ADO.NET.
You're obviously using ADO.NET Core :). And that's fine I think you should stick to using Stored procedures and DbCommands and such...
If you were using MSSQL 2008 you'd be able to do this using TableValued parameters and the MERGE statement. since you're using MSSQL 200 (why?) what you'd need to do is the following:
1. Send a comma delimited list of the Activity ids (the new ones) along with the ParkId to your stored proc. The ActivityIds parameter would be a varchar(50) for example.
In your stored proc you can split the ids
The strategy would be something like
1. For the Ids passed in, delete records that don't match
The SQL for that would be
DELETE FROM ParkActivities
WHERE ActivityId NOT IN (Some List of Ids)
WHERE ParkId = #ParkId
Since your list is a string you can do it like this
EXEC('DELETE FROM ParkActivities WHERE ActivityId NOT IN (' + #ActivityIds + ') AND ParkId = ' + #ParkId)
Now you can insert those activities that are not already in the table. The simplest way to do this would be to insert the ParkActivity ids into a temp table. To do that you'll need to split the comma delimited list into individual ids and insert them into a temp table. Once you have the data in the temp table you can insert doing a join.
The is a built-in user defined function in MSSQL 2000 that can do the split and return a Table Variable with each value on a seperate row.
http://msdn.microsoft.com/en-us/library/Aa496058
What is wrong with LinqToSQL and ADO.NET? I mean, could you specify your doubts about using those technologies
update
if LinqToSQL is not supported for 2000, you can easily upgrade to free 2008 express. It would be definitely enough for purposes you described.