Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC3 Razor ASP.NET4.0
hi
I am working on asp.net mvc3 based product.
and don't wanna use entity framework in my product for database access
I have to use basic database connectivity approach.
 
can anyone give me solution of this problem.how to use it .
 
With Warm Regards,
Gaurav ebay
Posted 26-Jun-12 1:48am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

In that case, use straight ADO.NET code. Here is some sample code that we sometimes use to read and write data in a SQL database:
 
public static System.Data.DataTable ReadData(string connectionString, string sqlQuery, Dictionary<string,> parameters = null)
{
    DataTable myTable = new DataTable();
    using (SqlConnection cnn = new SqlConnection(connectionString))
    {
        cnn.Open();
 
        using (SqlCommand myCommand = new SqlCommand())
        {
            SqlParameter param;
            myCommand.CommandText = sqlQuery;
 
            //Loops through each parameter in the dictionary
            //and adds it to the parameters list
            if (parameters != null)
            {
                foreach (var entry in parameters)
                {
                    param = new SqlParameter();
                    param.ParameterName = entry.Key;
                    param.Value = entry.Value;
                    myCommand.Parameters.Add(param);
                }
            }
 
            myCommand.Connection = cnn;
 
            using (SqlDataReader reader = myCommand.ExecuteReader())
            {
                myTable.Load(reader);
            }
        }
    }
    return myTable;
}
 
public static int WriteData(string connectionString, string sqlQuery, Dictionary<string,> parameters = null)
{
    using (SqlConnection cnn = new SqlConnection(connectionString))
    {
        cnn.Open();
 
        using (SqlCommand myCommand = new SqlCommand())
        {
            SqlParameter param;
            myCommand.CommandText = sqlQuery;
 
            //Loops through each parameter in the dictionary
            //and adds it to the parameters list
            if (parameters != null)
            {
                foreach (var entry in parameters)
                {
                    param = new SqlParameter();
                    param.ParameterName = entry.Key;
                    param.Value = entry.Value;
                    myCommand.Parameters.Add(param);
                }
            }
 
            myCommand.Connection = cnn;
 
            return myCommand.ExecuteNonQuery();
        }
    }
 
}
To use this, just pass in the connection string, the SQL query, and a Dictionary list of parameters. Adding a parameter to the Dictionary would look like this:
 
parameters.Add("@UserID", "14");
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 398
1 Sergey Alexandrovich Kryukov 320
2 Uma Shankar Patel 80
3 CB Sharma 79
4 PhilLenoir 70
0 Sergey Alexandrovich Kryukov 6,532
1 OriginalGriff 5,976
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,550


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 26 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100