Tuesday, May 17, 2016

ADO .NET DATABSE CONNECTION CODING IN .NET:


  ADO .NET DATABASE CONNECTION CODING IN .NET:


GLOBAL VARIABLES


  static int maxid=0,max=0,wc=0,sum=0,mc=0,rate;
        static String addr="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Universe\\LGdt\\Acmnger.accdb;Persist Security Info=True";


EXECUTE READER - CONNECTED MODEL




        public static int Accmger()
        {
            int max = 0;
            OleDbConnection con = new OleDbConnection(addr);
            con.Open();
            OleDbCommand cmd = new OleDbCommand("select max(id) as maxid from acc",con);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                max = Convert.ToInt32(dr["maxid"]);
            }
            con.Close();
           // int maxid=Convert.ToInt32(max);
            return max;

        }

DISCONNECTED MODEL GRIDVIEW


 SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet ds = new DataSet();
        String selecteditem = DropDownList1.Text;
        int i = 0;
        string sql = null;
        string connetionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\dATA\Documents\Visual Studio 2010\WebSites\WebSite2\App_Data\EmpDet.mdf;Integrated Security=True;User Instance=True";
        sql = "select employee.emp_id,employee.emp_name,employee.designation,employee.salary,dept_list.dept_name from employee INNER JOIN dept_list on employee.dept_id=dept_list.dept_id where dept_list.dept_name='"+selecteditem+"';";
        SqlConnection connection = new SqlConnection(connetionString);
        connection.Open();
        SqlCommand command = new SqlCommand(sql, connection);
        adapter.SelectCommand = command;
        adapter.Fill(ds);
        adapter.Dispose();
        command.Dispose();
        connection.Close();
        GridView2.DataSource = ds.Tables[0];
        GridView2.DataBind();



EXECUTE NON QUERY- 


UPDATE QUERY
OleDbCommand cmd2 = new OleDbCommand("update acc set totalfinal="+ sum +" where id=1", con);
            cmd2.ExecuteNonQuery();
            con.Close();

INSERT QUERY
       
con.Open();
            OleDbCommand cmd = new OleDbCommand("insert into acc(id,purdate,reason,amount) values(" +maxid+ ",'"+purdt+"','" + reason + "'," + rate + ")",con);
            cmd.ExecuteNonQuery();
            con.Close();




Note: Data Adapter may change according to the Database providers
Some ADO .NET Database Provider:
OleDbCommand
SqlCommand






No comments:

Post a Comment