C#之各類控件中輸入、輸出數據


本文重點講的是:ComboBox、DateTimePicker、TextBox、RadioButton、DataGridView這五種控件的輸入和輸出。

一、控件數據的輸入:

(1)ComboBox控件中的下拉列表中,可以顯示多項數據,使用ComboBox控件中的Items集合的Add方法向控件中添加數據。如下圖:

 

(2)DateTimePicker控件,一般用於讓用戶可以從日期列表中選擇單個值。運行時,單擊控件邊上的下拉箭頭,會顯示兩個部分:一個下拉列表,一個用於選擇日期。如下圖:

 

(3)TextBox控件,即文本框,用於用戶輸入和顯示文本。輸入數據如下圖:

 

(4)RadioButton控件為用戶提供由兩個或多個互斥選項組成的選項集,需要注意的是,當用戶選擇某單選按鈕時,同一組中的其他單選按鈕不能同時選定。如下圖所示

 

(5)DataGridView控件可以顯示和編輯來自不同類型的數據源的表格數據,如下圖所示:

 

 

二、思維導圖:

 

三、控件數據的輸出:

 

 

上圖界面代碼如下:

SqlConnection sqlConnection = new SqlConnection();                                              //聲明並實例化SQL連接;
sqlConnection.ConnectionString =
            "Server=(local);Database=Message;Integrated Security=sspi";                             //在字符串變量中,描述連接字符串所需的服務器地址、數據庫名稱、集成安全性(即是否使用Windows驗證);
            SqlCommand sqlCommand = new SqlCommand();                                                  //聲明並實例化SQL命令;
            sqlCommand.Connection = sqlConnection;                                                     //將SQL命令的連接屬性指向SQL連接 
            sqlCommand.CommandText = "SELECT * FROM DMESSAGE WHERE D_ID=@D_ID;";                              //指定SQL命令的命令文本;
            sqlCommand.Parameters.AddWithValue("@D_ID", this.txt_id.Text.Trim());
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //聲明並實例化SQL數據適配器,同時借助構造函數,將其SelectCommand屬性設為先前創建的SQL命令;
            sqlDataAdapter.SelectCommand = sqlCommand;                                                      //將SQL數據適配器的查詢命令屬性指向SQL命令;
            DataTable dengjiTable = new DataTable();                                                         //聲明並實例化數據表,用於保存所有班級,以用作下拉框數據源;
            sqlConnection.Open();                                                                           //打開SQL連接;                                                                  
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();                                      //調用SQL命令的方法ExecuteReader來執行命令,並獲取數據閱讀器;
            if (sqlDataReader.Read())                                                                       //若數據閱讀器成功讀取到下一條記錄(首次查詢則表示第一條記錄);
            {
                this.txt_id.Text = sqlDataReader["D_ID"].ToString();                                          //在數據閱讀器的索引器中指定列名,從而訪問當前記錄的指定列的值,並賦予相應控件;
                this.txt_name.Text = sqlDataReader["DOCTOR"].ToString();
                //this.cmb_Class.SelectedValue = (int)sqlDataReader["ClassNo"];
                this.cmb_dengji.SelectedValue = sqlDataReader["DENGJI"];
                this.txt_tel.Text = sqlDataReader["TEL"].ToString();       
                this.txt_address.Text = sqlDataReader["ADDRESS"].ToString();
                this.cmb_keshi.SelectedValue = sqlDataReader["KESHI"];
                this.txt_que.Text = sqlDataReader["QUESTION"].ToString();
                this.txt_ans.Text = sqlDataReader["ANSWER"].ToString();
            }
            sqlDataReader.Close();  //關閉數據閱讀器(同時關閉連接);
        }
SqlConnection sqlConnection = new SqlConnection();                                              //聲明並實例化SQL連接;
            sqlConnection.ConnectionString =
                "Server=(local);Database=Message;Integrated Security=sspi";                             //在字符串變量中,描述連接字符串所需的服務器地址、數據庫名稱、集成安全性(即是否使用Windows驗證);
            SqlCommand sqlCommand = new SqlCommand();                                                       //聲明並實例化SQL命令;
            sqlCommand.Connection = sqlConnection;                                                          //將SQL命令的連接屬性指向SQL連接;
            sqlCommand.CommandText = "SELECT * FROM PMESSAGE WHERE P_NAME=@P_NAME;";                              //指定SQL命令的命令文本;
            sqlCommand.Parameters.AddWithValue("@P_NAME", this.txt_name.Text.Trim());
            sqlConnection.Open();                                                                           //打開SQL連接;
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();                                       //調用SQL命令的方法ExecuteReader來執行命令,並獲取數據閱讀器;
            if (sqlDataReader.Read())                                                                       //若數據閱讀器成功讀取到下一條記錄(首次查詢則表示第一條記錄);
            {
                this.txt_id.Text = sqlDataReader["P_ID"].ToString();                                          //在數據閱讀器的索引器中指定列名,從而訪問當前記錄的指定列的值,並賦予相應控件;
                this.txt_name.Text = sqlDataReader["P_NAME"].ToString();
                this.rdb_male.Checked = (bool)sqlDataReader["SEX"];
                this.dtp_birthdate.Value = (DateTime)sqlDataReader["DATE"];
                this.txt_minzu.Text = sqlDataReader["MINZU"].ToString();
                this.txt_marry.Text = sqlDataReader["HUNYIN"].ToString();
                this.txt_job.Text = sqlDataReader["JOB"].ToString();
                this.txt_address.Text = sqlDataReader["ADDRESS"].ToString();
                this.txt_guomin.Text = sqlDataReader["GUOMIN"].ToString();
            }
            sqlDataReader.Close();  
 
         
SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                "Server=(local);Database=Message;Integrated Security=sspi";
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText =
                "select * from PMESSAGE";
            sqlConnection.Open();
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = sqlCommand;
            DataSet d = new DataSet();
            da.Fill(d, "PMESSAGE");
            dataGridView1.DataSource = d;
            dataGridView1.DataMember = "PMESSAGE";
            sqlConnection.Close();

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM