Пример #1
0
        //method to update in database from our application
        public bool Update(ContactClass c)
        {
            //create a default return type and set its default value to false
            bool          isSuccess = false;
            SqlConnection conn      = new SqlConnection(myconnstrng);

            try
            {
                //sql to update in our database
                string sql = "UPDATE tbl_contact SET FirstName=@FirstName, LastName=@LastName, ContactNo=@ContactNo, Address=@Address, Gender=@Gender WHERE ContactID=@ContactID";

                //create sql commande
                SqlCommand cmd = new SqlCommand(sql, conn);
                //create parameters to add value
                cmd.Parameters.AddWithValue("@FirstName", c.FirstName);
                cmd.Parameters.AddWithValue("@LastName", c.LastName);
                cmd.Parameters.AddWithValue("@ContactNo", c.ConstactNo);
                cmd.Parameters.AddWithValue("@Address", c.Address);
                cmd.Parameters.AddWithValue("@Gender", c.Gender);
                cmd.Parameters.AddWithValue("ContactID", c.ContactID);

                //oen database connection
                conn.Open();

                int rows = cmd.ExecuteNonQuery();
                //if the query runs succesfuly then the value will be grearer than zero else its value will be 0

                if (rows > 0)
                {
                    isSuccess = true;
                }
                else
                {
                    isSuccess = false;
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                conn.Close();
            }
            return(isSuccess);
        }
Пример #2
0
        //inserting data into database
        public bool Insert(ContactClass c)
        {
            //creating a default type and setting its value to false
            bool isSuccess = false;

            //step 1 connect database
            SqlConnection conn = new SqlConnection(myconnstrng);

            try
            {
                //step 2 create a slq query to insert data
                string sql = "INSERT INTO tbl_contact (FirstName, LastName, ContactNo, Address, Gender) VALUES (@FirstName, @LastName, @ContactNo, @Address, @Gender)";
                //creating sql command using sql and conn
                SqlCommand cmd = new SqlCommand(sql, conn);
                //creating parameters to add data
                cmd.Parameters.AddWithValue("@FirstName", c.FirstName);
                cmd.Parameters.AddWithValue("@LastName", c.LastName);
                cmd.Parameters.AddWithValue("@ContactNo", c.ConstactNo);
                cmd.Parameters.AddWithValue("@Address", c.Address);
                cmd.Parameters.AddWithValue("@Gender", c.Gender);

                //conection open here
                conn.Open();
                int rows = cmd.ExecuteNonQuery();
                //if the query runs succesfuly then the value of rows will be generate than zero else its value will be 0
                if (rows > 0)
                {
                    isSuccess = true;
                }
                else
                {
                    isSuccess = false;
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                conn.Close();
            }
            return(isSuccess);
        }