using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using System.Data.SqlClient;

namespace ClassLibrary3
{
    public class Operation
    {
        private SqlConnection con = null;
        private SqlCommand cmd = null;
        private SqlDataAdapter sda = null;
        private DataTable dt = null;
        public Operation(string connection)
        {
            con = new SqlConnection();
            con.ConnectionString = connection;
        }
        private void OpenCon()
        {
            if (con.State == ConnectionState.Closed)
                con.Open();
        }
        private void CloseCon()
        {
            if (con.State == ConnectionState.Open)
                con.Close();
        }
        public void DeleteMultiRows(string tableName,string primaryKey,string primaryKeyValue)
        {
            cmd = new SqlCommand();
            cmd.CommandText = "delete from "+ tableName+" where "+ primaryKey+" in ("+ primaryKeyValue+")";
            cmd.Connection = con;
            OpenCon();
            cmd.ExecuteNonQuery();
            CloseCon();
        }
        public void DeleteMultiRowsSP(string primaryKey, string primaryKeyValue,string spName)
        {
            cmd = new SqlCommand();
            cmd.Parameters.AddWithValue("@" + primaryKey, primaryKeyValue);
            cmd.CommandText = spName;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = con;
            OpenCon();
            cmd.ExecuteNonQuery();
            CloseCon();
        }


        public DataTable Select(string tableName)
        {
            dt = new DataTable();
            sda = new SqlDataAdapter("select * from "+ tableName,con);
            sda.Fill(dt);
            return dt;
        }
    }
}
