Click here to Skip to main content
Sign Up to vote bad
good
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:48


1 solution

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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 425
1 OriginalGriff 315
2 Slacker007 240
3 Aarti Meswania 210
4 Maciej Los 200
0 Sergey Alexandrovich Kryukov 8,893
1 OriginalGriff 7,134
2 CPallini 3,678
3 Rohan Leuva 3,036
4 Maciej Los 2,428


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 26 Jun 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid