Tuesday, 29 January 2013

SQL Server Bulk Data Copy in C#



Microsoft SQL Server includes a popular command-prompt utility named bcp for moving data from one table to another, whether on a single server or between servers. The SqlBulkCopy class lets you write managed code solutions that provide similar functionality. There are other ways to load data into a SQL Server table (INSERT statements, for example), but SqlBulkCopy offers a significant performance advantage over them.
The SqlBulkCopy class can be used to write data only to SQL Server tables. However, the data source is not limited to SQL Server; any data source can be used, as long as the data can be loaded to a DataTable instance or read with a IDataReader instance.
SqlBulkCopy will fail when bulk loading a DataTable column of type SqlDateTime into a SQL Server column whose type is one of the date/time types added in SQL Server 2008.
Ex.
The following console application demonstrates how to load data using the SqlBulkCopy class. In this example, a SqlDataReader is used to copy data from the Production.Product table in the SQL Server AdventureWorks database to a similar table in the same database.
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString = GetConnectionString();
        // Open a sourceConnection to the AdventureWorks database. 
        using (SqlConnection sourceConnection =
                   new SqlConnection(connectionString))
        {
            sourceConnection.Open();

            // Perform an initial count on the destination table.
            SqlCommand commandRowCount = new SqlCommand(
                "SELECT COUNT(*) FROM " +
                "dbo.BulkCopyDemoMatchingColumns;",
                sourceConnection);
            long countStart = System.Convert.ToInt32(
                commandRowCount.ExecuteScalar());
            Console.WriteLine("Starting row count = {0}", countStart);

            // Get data from the source table as a SqlDataReader.
            SqlCommand commandSourceData = new SqlCommand(
                "SELECT ProductID, Name, " +
                "ProductNumber " +
                "FROM Production.Product;", sourceConnection);
            SqlDataReader reader =
                commandSourceData.ExecuteReader();

            // Open the destination connection. In the real world you would  
            // not use SqlBulkCopy to move data from one table to the other  
            // in the same database. This is for demonstration purposes only. 
            using (SqlConnection destinationConnection =
                       new SqlConnection(connectionString))
            {
                destinationConnection.Open();

                // Set up the bulk copy object.  
                // Note that the column positions in the source 
                // data reader match the column positions in  
                // the destination table so there is no need to 
                // map columns. 
                using (SqlBulkCopy bulkCopy =
                           new SqlBulkCopy(destinationConnection))
                {
                    bulkCopy.DestinationTableName =
                        "dbo.BulkCopyDemoMatchingColumns";

                    try
                    {
                        // Write from the source to the destination.
                        bulkCopy.WriteToServer(reader);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        // Close the SqlDataReader. The SqlBulkCopy 
                        // object is automatically closed at the end 
                        // of the using block.
                        reader.Close();
                    }
                }

                // Perform a final count on the destination  
                // table to see how many rows were added. 
                long countEnd = System.Convert.ToInt32(
                    commandRowCount.ExecuteScalar());
                Console.WriteLine("Ending row count = {0}", countEnd);
                Console.WriteLine("{0} rows were added.", countEnd - countStart);
                Console.WriteLine("Press Enter to finish.");
                Console.ReadLine();
            }
        }
    }

    private static string GetConnectionString()
        // To avoid storing the sourceConnection string in your code,  
        // you can retrieve it from a configuration file.
    {
        return "Data Source=(local); " +
            " Integrated Security=true;" +
            "Initial Catalog=AdventureWorks;";
    }
}

Monday, 28 January 2013

Error log into a text file in C#



using System.IO;

/// <summary>
/// Writes text into a Log file under specified path
/// </summary>
/// <param name="text">Error String</param>
/// <param name="directryPath">Relative directry Path, use Server.MapPath() method to get a path</param>
public static void WriteLog(string text, string directryPath)
{
string FileName = string.Empty;
       string FilePath = string.Empty;
       try
       {              
              if (!Directory.Exists(directryPath))
              {
                    Directory.CreateDirectory(directryPath);
              }
              
              //Make a valid file Name
              FileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                
              //Make a FilePath
              FilePath = Path.Combine(directryPath, FileName) + ".txt";
              
              //If file exists then append the text , otherwise create a file and insert the text
              if (File.Exists(FilePath))
              {
                    WriteLogInLogFile(FilePath, text);
              }
              else
              {
                    File.WriteAllText(FilePath, " ");
                    WriteLogInLogFile(FilePath, text);
              }
}
       catch (Exception)
       {               
}          
}


/// <summary>
/// Used to insert the errorLog in text file
/// </summary>
/// <param name="FilePath">File path with directory</param>
/// <param name="ErrorString">Error String</param>
private static void WriteLogInLogFile(string FilePath, string ErrorString)
{
StreamWriter writer;
       try
       {
              writer = new StreamWriter(FilePath, true);
              ErrorString = DateTime.Now.ToString() + " >> " + ErrorString;
              ErrorString += System.Environment.NewLine;
              writer.WriteLine(ErrorString);
              writer.Flush();
              writer.Dispose();
       }
       catch (Exception)
       {
       }
}

Output:
Create a file named “2013125.txt”, and its context will like below:

1/25/2013 3:05:25 PM >> System.Threading.ThreadAbortException: Thread was being aborted.
   at System.Threading.Thread.AbortInternal()
   at System.Threading.Thread.Abort(Object stateInfo)
   at System.Web.HttpResponse.End()
   at System.Web.HttpResponse.Redirect(String url, Boolean endResponse)
   at System.Web.HttpResponse.Redirect(String url)
   at LoginPakistanPolio.btnLogin_Click(Object sender, EventArgs e) in c:\-- Projects --\TestProject\ TestProject \Home.aspx.cs:line 199