保存对类实例的更改以反映更改

时间:2016-11-12 20:15:22

标签: c#

朋友我正在使用class和ado.net 我正在使用数据表连接oledb等

当我使用表单加载来获取此类的实例时,在文本框和组合等表单控件上加载数据 我使用这个实例做一些操作,如添加记录删除记录编辑记录

我还有导航按钮move2first move2last等等

我的问题是什么: 我的问题什么时候在这个类实例上插入删除更新这个更改不会反映在它自己的实例上 当使用按钮移动移动时,我看到相同的记录

如何更新类的实例以反映更改

这是课程

    class dataConnection
    {
        public int affectedrecord;
        //Microsoft.Jet.OLEDB.4.0
        public OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Properties.Settings.Default.dPath + ";Jet OLEDB:Database Password=azouz(2016)");
       OleDbDataAdapter da;
       DataTable dt =new DataTable() ;
       OleDbCommand com;
        int intRow = 0;
        DataRow r;

        public DataTable  loadingdata(string sql)
        {
          dt.Clear();  
          da = new OleDbDataAdapter(sql, cn);
          da.Fill(dt);
          return dt;
        }


public void saverecord(string sql,string[]para,string[]val) 
      {
          cn.Open();
          com = new OleDbCommand();
          com.CommandText = sql;
          com.Connection = cn;
          com.CommandType = CommandType.Text;
          for (int i = 0; i< para.Count(); i++)
          {
              com.Parameters.AddWithValue(para[i], val[i]);
          }

          affectedrecord = com.ExecuteNonQuery();
          if (affectedrecord > 0) 
          {
              affectedrecord = 1;
          }
          cn.Close();
      }

      public void EditRecord(string sql, string [] para,string []val)
      {
          cn.Open();
          com = new OleDbCommand();
          com.CommandText = sql;
          com.Connection = cn;
          com.CommandType = CommandType.Text;
          int x = para.Count();
          for (int i = 0; i < para.Count(); i++)
          {
              com.Parameters.AddWithValue(para[i], val[i]);
          }
          affectedrecord = com.ExecuteNonQuery();
          if (affectedrecord > 0)
          {
              affectedrecord = 1;
          }
          cn.Close();

      }

      public void DeleteRecord(string sql,string [] para,string []val)
      {
          cn.Open();
          com = new OleDbCommand();
          com.CommandText = sql;
          com.Connection = cn;
          com.CommandType = CommandType.Text;
          for (int i =0;i<1;i++)
          {
              com.Parameters.AddWithValue(para[i], val[i]);

          }
          affectedrecord = com.ExecuteNonQuery();

          if (affectedrecord > 0)
          {
              affectedrecord = 1;
          }
          cn.Close();

      }

表单上的操作

 public partial class frmRegStore : Window
    {
        public frmRegStore()
        {
            InitializeComponent();
        }
        #region Variables
        //     'هذا النموذج يعتمد علي
        //' class
        //' c worktable 
        //'لملئ النموذج وورقة البيانات بالسجلات


        //'تعريف كائن من الكلاس
        dataConnection TotalWork = new dataConnection();   
        DataTable dt = new DataTable();
        DataRow r;
        #endregion
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string[] para = { "@storeName" };
            string[] val = { this.txtStoreName.Text };
            dataConnection Stores = new dataConnection();
            Stores.saverecord("insert into Stores (StoreName)values(?);", para, val);
            if (Stores.affectedrecord > 0)
            {
                MessageBox.Show("تم اضافة السجل بنجاح");

            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dt = TotalWork.loadingdata("Select * from Stores");
            r = TotalWork.Move2First();
            showData();
        }
        public void showData()
        {

            if ((r != null))
            {
                this.txtStoreID.Text = r[0].ToString();
                this.txtStoreName.Text = r[1].ToString();
            }

        }

        private void btnLast_Click(object sender, RoutedEventArgs e)
        {
            r = TotalWork.Move2Last();
            showData();
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            r = TotalWork.Move2Next();
            showData();
        }

        private void btnPrevious_Click(object sender, RoutedEventArgs e)
        {
            r = TotalWork.Move2Previous();
            showData();
        }

        private void btnFirst_Click(object sender, RoutedEventArgs e)
        {
            r = TotalWork.Move2First();
            showData();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {

            string[] para = { "@storeID" };
            string[] val = { this.txtStoreID.Text };
            dataConnection Stores = new dataConnection();
            Stores.saverecord("Delete from Stores where StoreId =?", para, val);
            if (Stores.affectedrecord > 0)
            {
                MessageBox.Show("تم حذف السجل بنجاح");

            }

        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            string[] para = {"@storeName", "@storeID" };
            string[] val = { this.txtStoreName.Text,this.txtStoreID.Text };
            dataConnection Stores = new dataConnection();
            Stores.saverecord("update Stores set StoreName = @storeName where StoreId =@storeID", para, val);
            if (Stores.affectedrecord > 0)
            {
                MessageBox.Show("تم تعديل السجل بنجاح");

            }

        }
    }
}

0 个答案:

没有答案