Qualifying bulk copy table mappings MSSQL - c#

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.

Related

Oracle : How to immediately get the first results from slow queries?

I'm writing a C# application that should run an Oracle-Select query and perform some calculations for each line.
The select query is very big and takes a long time.
In the current application design, I should wait until the query finishes retrieving all the data from the database in order to start the required computations on each row.
I was wondering if there is a way to get the first query results as the database engine find them.
Means that : Instead of waiting for the database engine to find all the rows that correspond to my query and return them, get the result since the first row found by the database engine.
At the end the computation required for each line will start as long as the first line found in the database and hence the total run time will be less.
The idea here is not about how to speed up an Oracle query or adding any index. It's more about getting overlapping computations to optimize more the computations.
Sorry if it's a dump question and thank you in advance.
I'm using Oracle 11g and the Query may just be as simple as (but returns hundreds of thousands of rows)
Select * from Table Where Condition1;
I run the explain plan for my query :
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 251 | 122K| 656K (1)| 00:07:40 |
|* 1 | TABLE ACCESS FULL| TABLE1 | 251 | 122K| 656K (1)| 00:07:40 |
-----------------------------------------------------------------------------
Oracle has an all rows strategy and a first rows strategy.
Usually, Oracle will, when possible, do a first rows strategy when possible. The simplest example of that would be something like:
select * from emp;
Here, there is no join, there's no sorting, etc, so, Oracle will begin to return rows immediately, as it reads through the EMP table.
On the other hand, this is a simple example of an all rows strategy:
select * from emp order by surname;
Here, we're asking for sort on SURNAME, so, we cannot begin to immediately return results. The table must be read in its entirety, and then sorted, before we can return the first row.
There are other factors as well. If you're joining tables, a NESTED LOOPS join will execute with a first rows strategy, whereas a HASH JOIN will (necessarily) employ an all rows strategy.
Ultimately, which is better, which you will want, is going to be dependent on your application. If you're doing stuff that the user directly interacts with, you'll probably want first rows, to not keep the user waiting. For batch jobs, all rows is (probably) better.
Finally, the optimizer can be influenced with the ALL_ROWS and FIRST_ROWS_n hints.

How do you update a Dataset made up of joined tables

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.

C# MySQLBulkLoader overwriting timestamp

I am working with some massive datasets and I am using the MySQLBulkLoader to import the data into my database. However I am having issues with the MySQLBulkLoader overwriting my timestamp column which is auto-updated. To contextualize assume I have the following structure:
Timestamp | Name | Age | Height
--------------------------------
2011-01-01 | Jeff | 30 | 183
2012-02-03 | Bob | 55 | 165
2016-04-05 | Sue | 33 | 155.
The data file I am loading in with the MySQLBulkLoader is a CSV which only contains the last three columns as shown below:
Name,Age,Height
Jeff,30,183
Bob,55,165
Sue,33,155
The issues I am having is that the first column in the CSV writes into the first column of the database. So I need to be able to:
Ignore the first column or bulk insert, or
Bulk insert into a subset of columns
Thanks, your assistance is appreciated.
Just to provide the answer to the question. The My SQL function:
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
[REPLACE | IGNORE]
INTO TABLE tbl_name
[CHARACTER SET charset_name]
[{FIELDS | COLUMNS}
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char']
]
[LINES
[STARTING BY 'string']
[TERMINATED BY 'string']
]
[IGNORE number LINES]
[(col_name_or_user_var,...)]
[SET col_name = expr,...]
Allows for the loading of the data into specific columns by specifying the column names. I struggled to get this operation working by manually creating the query but was able to modify the MySQLBulkLoader function. By default that function does not allow for the setting of the columns as the columns list is read only. But I modified the function to allow for the setting of column names. Once the modification was made the system write the data into the correct columns and the timestamp was maintained.

Should I create a view or another table?

Firstly, as a disclaimer, I'm learning a little about SQL/LINQ indirectly through C#. I'm very green in both really. I've quickly grown tired of using sample databases full of data and doing queries set in the most simple of situations. The information is presented this way because the focus is on C# and visual programming and not the hairies of SQL or even LINQ for that matter.
Even so, I decided to convert a simple application I wrote using text (CSV) files for storage. The application keeps track of three possible payables where 0 - 3 records will exist for a given date. I was able to create the database and separate tables for each contract and build an application that inserts the existing records into the database using LINQ to SQL classes.
Now I have another application that is used to add entries via a contract calculator or directly through a BindingSourceNavigator and DataGridView. Each table has in common four columns - Date, GrossPay, TaxAmount, and NetPay, Date being the primary key. I'd like to view the records by date where I have TotalGross, TotalTax, TotalNet, and a column for the GrossPay each contract table for that date.
What would correct approach to this - a view, LINQ query, separate table, or other? It seems a table would be the "easiest", at least in terms of my ability, but seems like an unnecessary copying of records. I tried to "link" the tables but no other column is guaranteed to be unique or primary.
Any suggestions would be great.
Clarification:
Three tables have the format:
| Date | GrossPay | TaxAmount | NetPay | ...each have others not in common... |
** Each table has specific data used to calculate the common columns based on contract type
I would like to view all records "grouped" by date such that each are represented like:
| Date | TotalGross | TotalTax | TotalNet | Table1Gross | Table2Gross | Table3Gross |
** "Total" columns are sums of the respective columns of all records sharing the date.
** One or two of the "Table(n)Gross" may be zero
I think you are asking if you can select records from three different tables by date for the columns they have in common?
If so, you need to do a union.
In your case it may look something like this in SQL. note that I have made a dummy column to denote the source of the record (which you may want)
SELECT Date, GrossPay, TaxAmount, NetPay, 'Table1' as Source FROM Table1
UNION
SELECT Date, GrossPay, TaxAmount, NetPay, 'Table2' as Source FROM Table2
UNION
SELECT Date, GrossPay, TaxAmount, NetPay, 'Table3' as Source FROM Table3
WHERE Date = '2013-05-05'
I wouldn't bother with a view and definitely don't replicate your data with a seperate table.

What is the best way to add/update/delete a lookup table in Microsoft SQL with ASP.NET C#?

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.

Categories

Resources