Search This Blog

Tuesday, October 16, 2018

Connection between ADO.net to SQL Server.

Step1:

Welcome to designer blog, Here we are going to discuss about  how to connect ADO.net  to Sql Server.

Step2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace DataAccess
{
    public class clsSqlServer
    {

        public DataSet getCustomers()
        {
            // open a connection
            string Connectionstring = ConfigurationManager.ConnectionStrings["DbConn"].ToString();
            SqlConnection objConnection = new SqlConnection(Connectionstring);
            objConnection.Open();

            // Fire the command object
            // Command -- SQL - SQl server
            SqlCommand objCommand = new SqlCommand("Select * from Customer", objConnection);
            DataSet objDataset = new DataSet();
            SqlDataAdapter objAdapter = new SqlDataAdapter(objCommand);

            objAdapter.Fill(objDataset);


            // Close the connection
            objConnection.Close();
            return objDataset;
        }
        public DataSet getCustomers(string CustomerCode)
        {
            // open a connection
            string Connectionstring = ConfigurationManager.ConnectionStrings["DbConn"].ToString();
            SqlConnection objConnection = new SqlConnection(Connectionstring);
            objConnection.Open();

            // Fire the command object
            // Command -- SQL - SQl server
            SqlCommand objCommand = new SqlCommand("Select * from Customer where CustomerName='"
                                  + CustomerCode + "'",
                                  objConnection);
            DataSet objDataset = new DataSet();
            SqlDataAdapter objAdapter = new SqlDataAdapter(objCommand);

            objAdapter.Fill(objDataset);


            // Close the connection
            objConnection.Close();
            return objDataset;
        }

        public bool InsertCustomer(string strCustomerName,
                                    string Country,
                                    string Gender,
                                    string Hobbies,
                                    bool Status)
        {
            // Open connection
            string Connectionstring = ConfigurationManager.ConnectionStrings["DbConn"].ToString();
            SqlConnection objConnection = new SqlConnection(Connectionstring);
            objConnection.Open();
            try
            {
                
                // Command insert fire
                string strInsertCommand = "insert into Customer values('"
                                                    + strCustomerName + "','"
                                                    + Country + "','"
                                                    + Gender + "','"
                                                    + Hobbies + "',"
                                                    + Convert.ToInt16(Status) + ")";

                
                SqlCommand objCommand = new SqlCommand(strInsertCommand, objConnection);
                objCommand.ExecuteNonQuery();
                // close connection
                
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                objConnection.Close();
            }
           
        }

        public bool UpdateCustomer(string strCustomerName,
                                    string Country,
                                    string Gender,
                                    string Hobbies,
                                    bool Status)
        {
            // Open connection
            string Connectionstring = ConfigurationManager.ConnectionStrings["DbConn"].ToString();
            SqlConnection objConnection = new SqlConnection(Connectionstring);
            objConnection.Open();
            // Command insert fire
            string strUpdateCommand = "Update Customer set CustomerName='"
                                                + strCustomerName + "',Country='"
                                                + Country + "',Gender='"
                                                + Gender + "',Hobbies='"
                                                + Hobbies + "',Married="
                                                + Convert.ToInt16(Status) + " where CustomerName='" + strCustomerName + "'";


            SqlCommand objCommand = new SqlCommand(strUpdateCommand, objConnection);
            objCommand.ExecuteNonQuery();
            // close connection
            objConnection.Close();
            return true;
        }

        public bool DeleteCustomer(string strCustomerName)
        {
            string Connectionstring = ConfigurationManager.ConnectionStrings["DbConn"].ToString();
            SqlConnection objConnection = new SqlConnection(Connectionstring);
            objConnection.Open();

            // Fire the command object
            // Command -- SQL - SQl server
            SqlCommand objCommand = new SqlCommand("delete from Customer where CustomerName='"
                                    + strCustomerName + "'",
                                    objConnection);

            objCommand.ExecuteNonQuery();
            objConnection.Close();
            return true;
        }
    }
}

Step3:

Enjoy Folks

No comments:

Post a Comment

Validating to select in sequencial order using angular

    < input type = "checkbox" (change) = "handleSelectTaskItem($event, taskItem)" [checked] = " taskItem . i...