C# 實現資料的增刪改查

來源:互聯網
上載者:User

最近跟了幾個項目,期間學到了不少東西,所以把學到的知識點寫在這裡,供以後參考:
1:增,刪,改,查

要對資料進行增刪改查,前提是擷取資料集,如何擷取資料集呢?簡單的就是通過查詢語句查詢出來,並將查詢出來的結果傳遞給某個資料容器,通過操作資料容器來操作資料。

        private string m_SQL;//sql語句

        private string m_SqlConnString; //資料庫連接字串
        private SqlConnection m_SqlConnection;        //資料庫連接對象
        private DataSet m_DataSet;//全域變數資料集
        private SqlDataAdapter m_SqlDataAdapter;        //資料配接器
        private SqlCommandBuilder m_SqlCommandBuilder;        //自動產生更新DataSet的語句
        private BindingSource m_BindingSource;//綁定資料集

        m_SqlConnString = DBManagerSQL.SetConnectionString(Common.C_CONNECTION_KEY);//擷取資料庫連接字串
        m_SqlConnection = new SqlConnection(m_SqlConnString);//串連資料庫
        m_SQL = "select [欄位名1],[欄位名2],[欄位名3]......... from [表名]  where [過濾條件]";

        m_SqlDataAdapter = new SqlDataAdapter(m_SQL, m_SqlConnection);//建立資料配接器
        m_SqlCommandBuilder = new SqlCommandBuilder(m_SqlDataAdapter); //用於自動產生sqlCommand   
        m_DataSet = new DataSet();

         //填充資料集
         m_SqlDataAdapter.Fill(m_DataSet, "表的別名(自己定義)");

         //初始化myBindingSource,控制項與資料集中間的一個橋樑。
         m_BindingSource = new BindingSource();
         m_BindingSource.DataSource = m_DataSet;
         m_BindingSource.DataMember = "表的別名";
         //調用資料繫結函數
         this.BindingCtrlToDB();
         //將資料集綁定到資料檢視上
         gridControlMain.DataSource = m_BindingSource;

         //翻譯空間上面顯示的英文欄位名為中文
         DBManagerSQL.SetGridComment(gridViewMain, tableName);

        public override void BindingCtrlToDB()
        {
            //欄位綁定
            控制項名.DataBindings.Clear();
            控制項名.DataBindings.Add("EditValue", m_BindingSource, "資料庫裡的欄位名");

        }

         增:

         this.m_BindingSource.AddNew();

     當為某一些控制項賦初始值時,要在m_BindingSource.AddingNew();的方法裡面寫,否則容線空值的現象,因為增加的時候m_BindingSource.AddNew();會把控制項清空。

          刪:

        public virtual void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           
if (MessageBox.Show("您確定要刪除這條記錄嗎?", "提示", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question) == DialogResult.OK)//刪除時提示使用者是否刪除
            {
                this.m_BindingSource.RemoveAt(m_BindingSource.Position);//刪除選中行
                this.m_SqlDataAdapter.Update(this.m_DataSet.Tables[0]);//刪除後更新資料集

    //這裡要把刪除按鈕的操作控制好,如果不想控制,可以寫一句話來判斷m_BindingSource裡面是否還有資料    //,如果有可以刪除,如果沒有則提示並返回

            //  if(m_BindingSource.RwCount>0)

     //  {

    // his.m_BindingSource.RemoveAt(m_BindingSource.Position);//刪除選中行
            //  this.m_SqlDataAdapter.Update(this.m_DataSet.Tables[0]);//刪除後更新資料集

      //   }

    //else

    //{

         //MessageBox.Show("沒有資料可以刪除");

    //}
            }
        }

    改:

               修改沒有什麼提別的,這裡不作介紹了。

               查:

               主要就是sql語句的寫法,這裡不作詳細介紹。

               儲存:資料操作完畢後必然要儲存。即把資料的修改提交到資料庫。

      public virtual void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
             {
               if (this.InitEditControlInputRules() == true)
               {
                this.m_BindingSource.EndEdit();//結束m_BindingSource的編輯狀態

       //如果是直接在gridview裡面編輯資料則需要兩句話

       //this.gvList.CloseEditor();
                //this.gvList.UpdateCurrentRow();

       //如果沒有這兩句話會出現最後條雖然寫入了資料但是儲存時出現空值,因為謝了,

                //那是因為他並沒有結束編輯狀態並寫入到資料集中
                this.m_SqlDataAdapter.Update(this.m_DataSet.Tables[0]);
            }
        }

取消m_BindingSource的編輯可以調用

this.bindingSourceMain.CancelEdit();

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.