Thursday 26 January 2012

Loading data to Sybase tables in C#

  If a sybase connection is to be established in C# then the AseConnection object can be used to accomplish it successfully.

  First the sybase reference should be added to the project solution by choosing Sybase.Data.AseClient component from the .Net Tab. This component doesn't comes free with .Net installation and it requires the shareware Sybase SQL client has to be installed. Once this is done, the below code can help in establishing a sybase connection.



Once the connection is established you can execute the queries by using a AseCommand object as below.



  Use the ExecuteScalar, ExecuteReader or ExecuteNonQuery appropriately as required. Finally close the connection by using connection.Close() command.



  Thats it and you may use them as per your requirement.


Example:
The below code takes a folder name from a form. The folder contains files each of which in turn contains thousands of insert statements that are to be inserted to a sybase tables. Once the user chooses the folder path and click the load command button the insert statements are fetched from each files and executed in batches of 5000. This is because in this case of  huge volume of rows to be inserted if the insertion is done one by one it would consume more time than the manual process and it also cannot be done in bulk, because once the operation fails it would be difficult to identify the rows that are missed. So it is convenient to carry out the insertion in terms of 5 thousands or lesser.

Form1.cs


Form1.Designer.cs



Make modifications to the above and use try catch block and test it for your connection string parameters. So basically this is just a simple guideline and cannot be used as such. In addition to this multi-threading can also be used if your server allows upload from multiple connections. In that case each thread opens a separate sql connections and executes rows insertions.

This example might sound to be a simple solution for the real programmers around the world. But some fools in a reputed MNC that works for a leading US company couldn't figure out this simple automation and opted to perform this tedious operations for months by torturing the innocent new joiners. While this is nothing but a simple sybase connection example.

If you have any queries and suggestion please leave your comments and more working references will be shared if necessary.

Note:
The example given here resembles a notepad when generated as a form and if this means something else to you, then it could be only you. Only you in the whole world can understand the reason why this app resembles a notepad's look and you will also know that it is me. I am sharing this code in public with a hope that one day you might come across this and realize that how much I cared for you. I had even more better automation to save you from those manual tasks. I hated to watch you work for those fools. Darling, I am more than what I did there. I thought that one day I will own you  and take you home as my princess. But I failed and lost you. I cannot take this separation from you and I will soon end my life, but not without proving myself to the world. I just wanted you to understand that I cared about you so much and I will always love you. Where ever you are be happy, because as you believe it is one way to be wise and also beautiful.

No comments:

Post a Comment