I have a C# application with a SQL Server database.
One functionality of the application is to read a big Excel file and go row by row trying to match an ID column with an ID in the database. Once a row matches, then some of the values are stored in a table.
Later the user is able to change the value on that table but I would like have a way to audit the original values. So I was thinking on creating an XML column in that table that will store the original values of the whole Excel row.
I don't need to implement audit trail to the table, just want to keep the Excel reference row in my SQL Server table for future audit.
Any advice?
External storage of the old value is too hard to maintain, in the long run.
All you need to do is add an "OriginalValue_[fieldname]" column in the SQL table for each value that the user can change. When you populate the table, put the same value in [fieldname] and [OriginalValue_fieldname]. When the user makes changes, you only change [fieldname] but the old value is right in the table.
If this won't work (i.e. you're not allowed to make schema changes), make another table with the ID fields and the original values. Same approach -- no external data.
Related
Im trying to figure out how I can do bulk operations on a table in SQL datbase where I want to be able to handle failed rows.
Let's say I do a bulk insert of 10 rows into a table and row number 5 has some faulty data and can't be inserted. I then want the 9 other rows to be inserted but in some way be able to get the 1 row that coulden't be inserted in return so I can store this row in some place for later error handling.
It would be great if I could use some nuget package for C# programming.
Typically the most optimal solution is to validate bad data as early in the process as possible. Consider the following strategy.
Create a datatable in your c# code where the datacolumns precisely match the datatype, length, nullable, etc. of the target tables where the needs to be persisted. If any data does not conform it will throw an exception when attempting to insert into the datatable.
Populate the datatable from your source data, one data row at a time. If a row fails to insert into the datatable, your exception handling could catch that row and log that data as needed.
Create a custom table-valued type in SQL Server to match the datatable. Make sure everything matches including all of the meta data of each datacolumn.
Create a stored procedure on SQL Server that contains the table-values input parameter. This parameter will be the new custom type created in step 3. The table valued parameter can then be used to quickly insert the data into the database tables.
Pass your datatable as a parameter value to the stored procedure. The data type should be SqlDbType.Structured.
This design adds speed and efficiency with early input validation, while allowing the process to pass data in bulk, allowing for fast inserts.
I guess you can do a trigger instead of insert, check if the data is valid. If it is not you can save it in another table from where you will retrieve the rows with errors, if there are not errors in the row you can insert it in your original table.
I'm making a KTV System, and in part of that system I need to have a database where I can add Karaoke Songs, Store, Delete and Access them. I'm using c#, VS2012. Local database for my database, created a table with 5 columns (Code, Title, Artist, Genre, Path).
Set my code to be unique key and primary key, and also auto-incremented w/ default values(1,1)
Path Column is for the path of the video file to be played whenever a songs is chosen.
I didn't program the database functions, just drag and drop the built-in functions from the datasource like this:
Everything works fine, all the basic functions are working as it should be. When adding it produces negative and weird auto-incremented primary key(which is Code Column) but I know it's fine because everytime I re-run it is already correct. Now
And one thing, after I added my Data Connection and Data Source I also add a New data connection, connecting into the database that is located in the bin folder of the project. Because if I wont do that, everytime I refresh the database it will erase the data inside of it. Or everytime I close the project, it deletes the data, and it gets data from the Empty database from the Project folder not from the bin.
PROBLEM:
For example:
I tried to add 6 rows and then deleted them all, I was thinking that whenever I will add new rows the Code Column will start from 1 2 and 3 again but not, 7, 8 and 9. It seems like it is skipping because there are already 6 existing columns but checked the data table in the database, it isn't. The deleted 6 columns are also gone, same as with the data in the form.
-
and something like this also
I want the Code Column to be always sorted, if ever I delete or Add new rows into the local database.
It all depends how you delete your data.
DELETE * FROM ...
will not affect your auto-increment primary key generator. However,
TRUNCATE TABLE ...
will reset the counter to its initial seed.
May I suggest that you don't attempt to control your auto-generated ID (code), but introduce a new column in your query using the ROW_NUMBER() function.
If you want to reset your primary key or code column truncate the table
Truncate Table tableName
Truncate like others have said will work or with DCBB CHECKIDENT:
DBCC CHECKIDENT (TABLENAMEHERE, RESEED, 0)
I have created table in MS SQL 2008 with one identity column(Start Value - 1 and Increment is also 1) and 4 other columns. I am accessing this DB from C# ASP.NET. Used to push data only for the non identity column. Identity column will auto increment itself.
As of now i am manually querying the column value with the remaining for columns. But I am facing problem if all the other four column values are equal i am not getting the exact value which i am looking for
Now my query is, Is there any why in C# where I can get the value of the newly created identity column whenever new record is created.
Thanks.
You can use
SCOPE_IDENTITY()
Which will returns the primary key value of the recently inserted row
The answer to your question actually lies in SQL Server. You can run:
SELECT ##identity
after your insert to get the last inserted row identity.
http://technet.microsoft.com/en-us/library/aa933167(v=sql.80).aspx
EDIT BASED ON COMMENTS:
Consider using SCOPE_IDENTITY() as referenced here:
http://technet.microsoft.com/en-us/library/aa259185(v=sql.80).aspx
In SQL terms you can output the records back if you wish it. But how you might apply this to C# is up to you. Example:
INSERT INTO TABLE_A (SOMETHING, SOMETHINGELSE, RANDOMVAL3)
OUTPUT inserted.A_ID, inserted.SOMETHING, inserted.SOMETHINGELSE, inserted.RANDOMVAL3
SELECT 'ASD','DOSD', 123
But unless you're using merge, you can't use OUTPUT to print out any values from joining tables from an INSERT. But that's another matter entirely, I think.
Also, it's hardly good practice to bounce this data between the application and the DB all the time, so I'd look to alternatives if possible.
I'm trying to store a DataTable into a single column in a SQL Server table. The idea behind this is a user runs a SQL command and the returned output is stored into a datatable, then I want that datatable to be stored into a SQL Server logging table. Later on I want to be able to retrieve that entire datatable back for displaying on a logging aspx page.
Currently I'm just storing it as a big string but that doesn't give me column headers and the formatting is kinda funky as well as being inefficient.
TIA
I would probably convert the datatable to XML and store it into an XML field type if I was going to do what you are trying to do.
Hello you can try with WriteXml
this link give you sample interessant : http://msdn.microsoft.com/fr-fr/library/system.data.datatable.writexml.aspx
Another Idea is to create two tables in your database. It is slightly complex.
One Table contains two columns, Let name the table PTable.
Columns:
ID and ColumnID
ID is the primary key and ColumnID contains the name of your column in datatable
After creating this table create another table. It will consists of three fields. Let name it STable. This table stores the columns of you datatable.
Columns:
stblID, PtblID and PtColumnID
StbID is the primary key in this table, PtblID is the Primary key of PTable and PtColumnID is the ColumnID of PTable. This table stores the rows of table. Now store the data in this table and receive the data when you need it.
and the simplest idea is to create a table in your datbabase having an xml column and store your datatable as an xml.
Say that I have this code:
rentalEaseDataSet.tblCheckbookEntry.Rows.Add(newRow);
I want to get the identity of a column from that row I just added. How can I do this? Say the row is called ID.
For reference, I'm using C# and sql server express.
Well, that row hasn't actually been saved to the database yet, so it doesn't have a database id (so sql server express isn't an issue). You need to use the adapter to save the changes, and then the id of newRow should reflect the database.
Assuming you are dealing with an auto-incremented identity value on the source of the data, you can't do this. You have to commit the changes to the data source through a data adapter, and then the column that represents the auto incremented id will be updated with the value.