Monday, 23 September 2013

Simple method in C#.NET to Select Count from SQL Server using SqlCommand.ExecuteScalar() method and with SqlParameters.

Here i implemented a simple method to Select Count value from SQL Server. I am implementing this method by using SqlCommand.ExecuteScalar() method and SqlParameter's. This method is better is secure way also.

Namespaces:
using System.Data.SqlClient;
using System.Collections.Generic;

Method: 
public static int PerformSQLSelectCountOperation(string sqlQuery, List<SqlParameter> sqlParameters, string sqlConnectionString)
    {
        SqlConnection connection = new SqlConnection(sqlConnectionString);
        SqlCommand selectCommand = new SqlCommand(sqlQuery, connection);
        if (sqlParameters.Count > 0)
            selectCommand.Parameters.AddRange(sqlParameters.ToArray());
        connection.Open();
        int num = Convert.ToInt32(selectCommand.ExecuteScalar());
        connection.Close();
        return num;
    }
How can we use this method?
string sqlConnectionString = "Data Source=ADMIN-PC;Initial Catalog=dvtech;User ID=sa;Password=sql@121";
string sqlQuery = "Select count(*) from employee where Location=@Location";
List<SqlParameter> sqlParams = new List<SqlParameter>();
sqlParams.Add(new SqlParameter("@Location", "Hyderabad"));
int result = PerformSQLSelectCountOperation(sqlQuery, sqlParams, sqlConnectionString);

Simple method in C#.NET to Select Single cell value from SQL Server using SqlCommand.ExecuteScalar() method and SqlParameters.

Here i implemented a simple method to Select single cell value from SQL Server. I am implementing this method by using SqlCommand.ExecuteScalar() method and SqlParameter's. This method is better is secure way also.

Namespaces:
using System.Data.SqlClient;
using System.Collections.Generic;

Method: 
public static string PerformSQLSelectSingleCellOperation(string sqlQuery, List<SqlParameter> sqlParameters, string sqlConnectionString)
    {
        SqlConnection connection = new SqlConnection(sqlConnectionString);
        SqlCommand selectCommand = new SqlCommand(sqlQuery, connection);
        if (sqlParameters.Count > 0)
            selectCommand.Parameters.AddRange(sqlParameters.ToArray());
        connection.Open();
        string str = Convert.ToString(selectCommand.ExecuteScalar());
        connection.Close();
        if (!string.IsNullOrEmpty(str))
            return str;
        else
            return string.Empty;
    }

How can we use this method?
string sqlConnectionString = "Data Source=ADMIN-PC;Initial Catalog=dvtech;User ID=sa;Password=sql@121";
string sqlQuery = "Select empname from employee where Location=@Location";
List<SqlParameter> sqlParams = new List<SqlParameter>();
sqlParams.Add(new SqlParameter("@Location", "Hyderabad"));
string result = PerformSQLSelectSingleCellOperation(sqlQuery, sqlParams, sqlConnectionString);

Simple method in C#.NET to Select data from SQL Server using SqlDataAdapter.Fill() method and SqlParameters.

Here i implemented a simple method to perform the Select operation in SQL Server. I am implementing this method by using SqlDataAdapter.Fill() method and SqlParameter's. This method is better is secure way also.

Namespaces:
using System.Data.SqlClient;
using System.Collections.Generic;

Method: 
public static DataTable PerformSQLSelectOperation(string sqlQuery, List<SqlParameter> sqlParameters)
    {
        DataTable dataTable = new DataTable();
        SqlConnection connection = new SqlConnection(sqlConnectionString);
        SqlCommand selectCommand = new SqlCommand(sqlQuery, connection);
        if (sqlParameters.Count > 0)
            selectCommand.Parameters.AddRange(sqlParameters.ToArray());
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommand);
        sqlDataAdapter.SelectCommand = selectCommand;
        sqlDataAdapter.Fill(dataTable);
        return dataTable;
    }

How can we use this method?

string sqlConnectionString = "Data Source=ADMIN-PC;Initial Catalog=dvtech;User ID=sa;Password=sql@121";
string sqlQuery = "Select * from employee where Location=@Location";
List<SqlParameter> sqlParams = new List<SqlParameter>();
sqlParams.Add(new SqlParameter("@Location", "Hyderabad"));
DataTable noOfEffectedRows = PerformSQLSelectOperation(sqlQuery, sqlParams, sqlConnectionString);

Simple method in C#.NET to Insert Update or Delete data in SQL Server using SqlCommand.ExecuteNonQuery() method and SqlParameters.

Here i implemented a simple method to perform the Insert Update of Delete actions in SQL Server. I am implementing this method by using SqlCommand.ExecuteNonQuery() method and SqlParameter's. This method is better is secure way also.

Namespaces:

using System.Data.SqlClient;
using System.Collections.Generic;

Method: 

public static int PerformSQLInsertOrUpdateOrDeleteOperation(string sqlQuery, List<SqlParameter> sqlParameters, string sqlConnectionString)
    {
        SqlConnection connection = new SqlConnection(sqlConnectionString);
        SqlCommand selectCommand = new SqlCommand(sqlQuery, connection);
        if (sqlParameters.Count > 0)
            selectCommand.Parameters.AddRange(sqlParameters.ToArray());
        using (connection)
        {
            connection.Open();
            int num = selectCommand.ExecuteNonQuery();
            connection.Close();
            return num;
        }
    }

How can we use this method?

string sqlConnectionString = "Data Source=ADMIN-PC;Initial Catalog=dvtech;User ID=sa;Password=sql@121";
string sqlQuery = "Select * from employee where Location=@Location";
List<SqlParameter> sqlParams = new List<SqlParameter>();
sqlParams.Add(new SqlParameter("@Location", "Hyderabad"));
int noOfEffectedRows = PerformSQLInsertOrUpdateOrDeleteOperation(sqlQuery, sqlParams, sqlConnectionString);