I want to use SqlBulkCopy C# to insert rows to Sql DataBase
but I don't understand if I define a table in the Db with X columns and I want to insert 2 DataTables- one of them contains only y/x columns and the second contains only z/x columns.
when I use SqlBulkCopy, does it auto check the column name and insert value only if it is the appropriate column and null if not, or it always inserts values in the first columns and the last columns will be empty?
I tried to search it but I haven't find it?
can anyone help?
Look for :SqlBulkCopyColumnMapping
When SqlBulkCopyColumnMapping is used, only columns for which mappings are created will be copied.
If you do not create a mapping for a column, it will be ignored by the copy process.
Take a look at the SqlBulkCopy.ColumnMappings property. It allows you to map source and destination columns when the column count or positions do not match.
Related
With ODP.Net package we fill a simple query result into Dataset by Oracle.ManagedDataAccess.Client.OracleDataAdapter.I want to have a bit more description for resulting columns. Would have been nice if I could define a caption for columns in Oracle and get them in the resulting dataset.
I found a way to add a comment on column in Oracle :
COMMENT ON COLUMN my_table.my_columns IS 'MY_CUSTOM_CAPTION'
but I don't know how we can get it.
In the other side i found two options (Caption & Extended properties) in the resulting dataset which i guess is what i am looking for, but it seems that I am wrong:( :
Anyone knows a way to put some description or alternative caption to columns in Oracle DB and read them in application by Odp.Net?
I would recommend to create a view from your table like this:
CREATE OR REPLACE VIEW V_my_table AS
SELECT my_column AS "My Caption even with spaces"
FROM my_table;
You can also make DML operations like DELETE, UPDATE or INSERT into such views.
I think there are two possible ways you can choose.
Read the Column comments from Oracle as you described in your question
Create a own Table where you store extra information
In both ways you need to join an extra Table to your query. If you want to read the Column comments take a look at Dba_col_comments. This table stores all columncomments you define in your Oracledatabase. So you can read them with sql. But be careful that you don't fill Column comments with extra information which describe the column. You DBA and others will intend that a column comment describe for what the column is or what it stores. So don't write any other information to it.
The second approach is that you build an own Table, where you store extra information for your columns. You just need three columns for table, column and information. This table you can join.
I am reading an excel file and saving contents to database.
I have two columns , first column name is blank and second column name is A.
Both columns has rows under it .
When I am executing "select * from ["+excel[i]+"]") using oledbconnection,
empty column is automatically replaced with F1. I need both the columns and data as it is available from excel sheet.
How to avoid it?
I don't think there is a way to avoid this. This is a normal behaviour of OleDB when connected to Excel, if it finds empty columns, it will either take the first row as column name or generate some default names as of F1,F2,F3...etc
I am using EF 6 in my project, when i want to delete a row from a table it trow exception because that row is referenced in another table. I want to know which table preventing me from deleting with c# code and EF.
We have a record in table A and that row is referenced in table C not in table B. Is it possible with EF to know that table C prevent me from deleting that row?
I am also using Sql-Server 2012.
If you are trying to dynamically sort this at runtime to show the user or determine before delete attempt and you are unsure of the possible conflict you could use the sys tables and some dynamic sql to sort this out.
Use sys.objects to find your table name and get an object_id.
Use sys.foreign_keys to find tables that reference your table.
Use sys.foreign_key_columns to get the exact column numbers referenced.
Use sys.columns to get appropriate column names.
Build dynamic SQL to search the table and column names and find the offending rows using the values from the source row that cannot be deleted.
List the tables returned looking up in sys.objects. Optionally list the number of rows.
Optionally use dynamic SQL to build other statements (e.g. delete offending records - this may need to be recursive - take care with this - you could lose a lot of data!)
I have a problem with the Query Access. My code is as follows:
string query = "ALTER TABLE Student ADD COLUMN Surname MEMO AFTER 'Name'";
Why always inserts the column at the end of the table? Is there any method to insert a new column in a specific position?
First of all, I don't see any reason to add your column to a specific position. You can always use the column order as you want for a select statement for example..
Why always inserts the column at the end of the table?
Because it is designed like that?
There is a method to insert a new column in a specific position?
As far as I know, there is no way to do it without rebuilding your table.
From ALTER TABLE syntax for changing column order
Today when you use ALTER TABLE ADD to add a column, a new column is
always placed as the last column. This is far from often desireable.
Often developers and database designers want to keep some logic in a
column order, so that related column are close to each other. A
standard rule we keep in the system I work with is to always have
auditing columns at the end. Furthermore many graphical design tools
encourage this kind of design, both bottom-end tools like the Table
Designer in SSMS as well as high-end data-modelling tools such as
Power Designer.
Today, if you want to maintain column order you have no choice but to
go the long way: create a new version of the table and copy over. It
takes time, and if not implemented correctly, things can go very
wrong.
I am using fluentmigrator to add a new column to a table. I then want to update each row in the table with a unique value for that column.
Currently when I use:
Update.Table("Foo").InSchema("dbo").Set(new { Bar = Bar.Generate() }).AllRows();
It gives the same value for all the rows.
How do I ensure it calls that method for each row?
I'm not sure what Bar.Generate does but I am guessing it creates a GUID or unique id.
If so then you could use:
Execute.Sql("update dbo.Foo set Bar = NEWID()");
Or if you want sequential guids then you could use NEWSEQUENTIALID().
If you are adding a new column for this unique identier, then all you would need to do is specify the new column .AsGuid()
EDIT: FluentMigrator is a small fluent dsl and is not meant to cover a complicated case like this. There is no way (as far as I know) to do this with one sql UPDATE and therefore no easy way to do it with FluentMigrator. You'll have to get the row count for the table with ADO.NET or an ORM (Dapper/NHibernate) and then loop through each row and update the Bar column with the custom unique identifier. So if you have one million rows then you will have to make one million sql updates. If you can rewrite your Bar.Generate() method as an Sql function that is based on the NEWID() function like this or this then you could do it as one UPDATE statement and call it with FluentMigrator's Execute.Sql method.
You haven't mentioned which database you are working with. But some like Postgres have non-standard features that could help you.