What is best approach to get sql data from C#

It seems you may be looking at old books. If you're going to do it the "old fashioned way", then you should at least use using blocks. Summary:

using (var connection = new SqlConnection(connectionString))
{
    using (var command = new SqlCommand(commandString, connection))
    {
        using (var reader = command.ExecuteReader())
        {
             // Use the reader
        }
    }
}

Better still, look into Entity Framework.

Links: Data Developer Center


If it's easy you're looking for, you can't do any better than Linq-to-SQL:-

http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

If your SQL database already exists, you can be up-and-running in seconds.

Otherwise, I agree with John.


you should have a look into these tutorials,

[http://www.asp.net/learn/data-access/][1]

All the work you are planning is already been done.

have a look at this way of doing same what you are doinng

  string preparedCommand =
  @"SELECT TOP 1 [SomeColumn],[SomeColumn2], [SomeColumn3]    
  FROM [Database].[dbo].[Table]
  WHERE [SomeOtherColumn] = @varValue";
  [1]: http://www.asp.net/learn/data-access/

More better way of doing the same above is by Using LINQ TO SQL

var result = from someObject in SomeTable
             where SomeColumnHasValue == ValueToCompare
             select new { SomeColumn, SomeColumn1, SomeColumn2};
  • No Type Safety Issues
  • Visualise Database in C# while you work on it
  • at compile time less errors
  • less code
  • more productive

Following are some of the great resources for LINQ if you are interested

  • http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx
  • http://www.hookedonlinq.com/MainPage.ashx
  • https://stackoverflow.com/questions/47740/what-are-some-good-linq-resouces

Hope it helps

Tags:

C#

Sql

Sql Server