1. 程式人生 > >C#編寫簡單的資料庫增刪改查(二)

C#編寫簡單的資料庫增刪改查(二)

一、今天我們繼續資料庫的增刪改查的編寫,下面我們進行刪除操作,我們在Formdelete介面上拖入幾個控制元件,如下圖:
這裡寫圖片描述
(資料庫顯示框參照之前的資料庫增加的方法)
將控制元件改名後,雙擊刪除按鈕進入程式碼編寫介面,首先連線資料庫,在判斷學號與姓名文字框中是否有資料,詳細程式碼見下方。

SqlConnection sqlconnect = new SqlConnection("Data Source=RJ250825;Initial Catalog=pubs;Integrated Security=True");
            sqlconnect.Open
(); string str=""; if(tbxuehao.Text.Trim().Length==0&&tbname.Text.Trim().Length==0) { MessageBox.Show("請輸入要刪除的學號或姓名!!!"); }else if(tbxuehao.Text.Trim().Length>0&&tbname.Text.Trim().Length==0) { str="delete from student where 學號="
+tbxuehao.Text+""; }else if (tbxuehao.Text.Trim().Length==0&&tbname.Text.Trim().Length>0) { str="delete from student where 姓名='"+tbname.Text+"'"; }else if(tbxuehao.Text.Trim().Length>0&&tbname.Text.Trim().Length>0) { str = "delete from student where 學號="
+ tbxuehao.Text+"and 姓名='" + tbname.Text + "'"; } SqlCommand sqlcommand = new SqlCommand(str, sqlconnect); int s = sqlcommand.ExecuteNonQuery(); if (s == 1) { MessageBox.Show("刪除成功"); tbxuehao.Clear(); tbname.Clear(); this.studentTableAdapter.Fill(this.pubsDataSet.student); } this.studentTableAdapter.Fill(this.pubsDataSet.student); sqlconnect.Close();

二、接下來我們進行修改操作,這裡我的思路是先進行查詢,再將查詢到的資料顯示在TextBox上,方便對TextBox裡的能容進行修改。首先我們在FormUpdate介面上拖入幾個控制元件,如下圖:
這裡寫圖片描述
這裡我們設定學號不可更改,點選學號後的TextBox文字框,在屬性欄找到Enabled,設定為False(不可修改),這樣使用者智慧修改除學號以外的內容。
雙擊查詢按鈕,進行查詢程式碼的編寫,如果查詢到就顯示在右側的文字框中,如果找不到,彈出MessageBox提示使用者(重新輸入!!):

 SqlConnection sqlconnect = new SqlConnection("Data Source=RJ250825;Initial Catalog=pubs;Integrated Security=True");
            sqlconnect.Open();
            string str = "select * from student where 學號="+tbxuehao.Text;
            SqlCommand sqlcommand = new SqlCommand(str, sqlconnect);
                SqlDataReader dateReader = sqlcommand.ExecuteReader();
                if (dateReader != null)
                {
                    MessageBox.Show("查詢成功!!");
                    dateReader.Read();

                    tbxuehao1.Text = dateReader[0].ToString();  // 把查到結果顯示在文字框
                    tbname1.Text = dateReader[1].ToString();
                    tbage1.Text = dateReader[2].ToString();
                    tbphone1.Text = dateReader[3].ToString();


                }
                else
                {
                    MessageBox.Show("請重新輸入!!");
                }

如果查詢到相關資料後,對其進行修改,點選更改按鈕進入資料修改階段:

 SqlConnection sqlconnect = new SqlConnection("Data Source=RJ250825;Initial Catalog=pubs;Integrated Security=True");
            sqlconnect.Open();
            string str = "update student set 姓名='"+tbname1.Text+"',年齡="+tbage1.Text+",電話="+tbphone1.Text+"where 學號="+tbxuehao.Text;
            SqlCommand sqlcommand = new SqlCommand(str, sqlconnect);
            sqlcommand.ExecuteNonQuery();

三、資料的查詢,開啟查詢介面拖入控制元件如下圖:
這裡寫圖片描述
資料的查詢可以對單一條件進行查詢,也可以對組合條件進行查詢:

 SqlConnection sconn = new SqlConnection("Data Source=RJ250825;Initial Catalog=pubs;Integrated Security=True");
            sconn.Open();
            string str = "select * from student ";
            if (tbxuehao.Text.Trim().Length > 0)
            {
                str += "where 學號=" +Convert.ToInt32(tbxuehao.Text);

            }    
            if (tbname.Text.Trim().Length > 0)
            {
                if (str.Contains("where"))
                {
                    str += "and 姓名='" +tbname.Text+"'";
                }
                else
                {
                    str += "where 姓名='" + tbname.Text+"'";
                }
            }
            if (tbage.Text.Trim().Length > 0)
            {
                if (str.Contains("where"))
                {
                    str += " and 年齡=" + Convert.ToInt32(tbage.Text);
                }
                else
                {
                    str += "where 年齡=" + Convert.ToInt32(tbage.Text);
                }
            }
            if (tbphone.Text.Trim().Length > 0)
            {
                if (str.Contains("where"))
                {
                    str += " and 電話=" + Convert.ToInt32(tbphone.Text);
                }
                else
                {
                    str += "where 電話=" +Convert.ToInt32( tbphone.Text);
                }
            }

            SqlCommand comm = new SqlCommand(str, sconn);
            comm.ExecuteNonQuery();
            SqlDataAdapter sd = new SqlDataAdapter(comm);
            DataSet ds = new DataSet();
            sd.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            sconn.Close();

採用的是按個判斷文字框是否有資料,如果第一個文字框有資料,SQL語句一定包含where關鍵字,如果下邊還有資料,只需要新增and…
(例如:select * from student where 學號=… and 姓名=… and 年齡 =….)。