標籤:style blog io color os ar 使用 for sp
執行個體
更新記錄
在本例子中,建立一個供使用者輸入學生學號和姓名的文字框和幾個對應不同操作類型的更新資訊按鈕,當使用者輸入資訊以後單擊相應的按鈕則執行相應的操作。在此執行個體中還將接觸到伺服器資訊驗證的相關知識。
(1)建立名為UpdateTest的Windows Application應用程式,在預設的Forml.cs中添加2個Label控制項,2個TextBox控制項,3個Button控制項,按表4.7所示設定這7個控制項的屬性。
表4.7控制項屬性
控制項類型 ID屬性 Text屬性 |
標籤 lblUserID 學號: 標籤 lblUserName 姓名: 文字框 txtUserlD 文字框 txtUserName 按鈕 btnExecute1 拼接字串 按鈕 btnExecute2 使用參數 按鈕 btnExecute3 使用預存程序 |
(2)調整控制項的位置(按照個人喜好了)
(3)雙擊“拼接字串”按鈕,註冊按鈕btnExecute的按鈕單擊事件btnExecute1_Click,
然後再切換到Form1.cs頁面的“設計”視圖,依次雙擊“使用參數”和“使用預存程序”按鈕來註冊對應的按鈕單擊事件btnExecute2_Click和btnExecute3_Click。
(4)在Form1.cs檔案中首先引入命名空間System.Data.SqlClient,然後添加一個名 CheckInfo的方法,傳回值為bool類型,代碼如下:
bool CheckInfo(){ //判斷學號是否輸入 if (this.txtUserID.Text.Trim() == "") { Alert("學號不完整"); return false; } else if (this.txtUserName.Text.Trim() == "") //判斷姓名是否輸入 { Alert("姓名不完整"); return false; } //資訊檢查通過 return true;}
//其中,Alert是自訂的另外一個方法,用來彈出一個對話方塊,定義如下: void Alert(string message) { MessageBox.Show(null, message, "資訊提示", MessageBoxButtons.OK, MessageBoxIcon.Information); }
//在btnExecute1_Click中編寫如下代碼: private void btnExecute1_Click(object sender, EventArgs e) { //資訊檢查 if(this.CheckInfo()) { //取值 string userId=this.txtUserID.Text.Trim(); string userName=this.txtUserName.Text.Trim(); //建立連線物件 SqlConnection conn=new SqlConnection(); conn.ConnectionString="Data Source=(local);Initial Catalog=Student;Integrated Security=SSPI"; //拼接命令字串 string updateQuery="update StudentInfo set sName=‘"+userName+"‘"+"where ID=‘"+userId+"’"; //建立命令對象 SqlCommand cmd=new SqlCommand(updateQuery,conn); conn.Open(); //儲存執行結果 int RecordsAffected=cmd.ExecuteNonQuery(); conn.Close(); //提示結果 Alert("更新資料數為"+RecordsAffected); } }
//在btnExecute2_Click中編寫如下代碼: private void btnExecute2_Click(object sender, EventArgs e) { //資訊檢查 if(this.CheckInfo()) { //取值 string userId=this.txtUserID.Text.Trim(); string userName=this.txtUserName.Text.Trim(); //建立連線物件 SqlConnection conn=new SqlConnection(); conn.ConnectionString="Data Source=(local);Initial Catalog=Student;Integrated Security=SSPI"; //拼接命令字串 string updateQuery="update StudentInfo set [email protected] where [email protected]"; //建立命令對象 SqlCommand cmd=new SqlCommand(updateQuery,conn); //添加參數 cmd.Parameters.Add(new SqlParameter("@userName", userName)); cmd.Parameters.Add(new SqlParameter("@userId", userId)); conn.Open(); //儲存執行結果 int RecordsAffected = cmd.ExecuteNonQuery(); conn.Close(); /* try { conn.Open(); //儲存執行結果 int RecordsAffected = cmd.ExecuteNonQuery(); } catch (Exception err) { MessageBox.Show(err.Message, "修改記錄失敗"); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } }*/ //提示結果 Alert("更新資料數為"+RecordsAffected); } }
//在btnExecute3_Click中編寫如下代碼: private void btnExecute3_Click(object sender, EventArgs e) { //資訊檢查 if (this.CheckInfo()) { //取值 string userId = this.txtUserID.Text.Trim(); string userName = this.txtUserName.Text.Trim(); //建立連線物件 SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=(local);Initial Catalog=Student;Integrated Security=SSPI"; //建立命令對象 SqlCommand cmd = new SqlCommand("UpdateStudentInfo", conn); //指定命令類型為預存程序 cmd.CommandType = CommandType.StoredProcedure; //添加參數 cmd.Parameters.Add(new SqlParameter("@userName", userName)); cmd.Parameters.Add(new SqlParameter("@userId", userId)); conn.Open(); //儲存執行結果 int RecordsAffected = cmd.ExecuteNonQuery(); conn.Close(); //提示結果 Alert("更新資料數為" + RecordsAffected); } }
(9)在學號和姓名中分別輸入資訊以後,單擊任意按鈕即可測試更新結果。
例如:
分別輸入學號"2007102001"和姓名“某某”後單擊任意按鈕.
代碼講解
在引入了System.Data.SqlClient命名空間以後,使用了SQL Server .NET資料提供者對資料進行更新。
更新資料前使用了CheckInfo方法對資料進行檢查,查看使用者是否在姓名和學號兩個文字框中輸入了有效資訊,如果兩者的輸入資訊都有效,則該方法返回true,否則返回false,
方法實現如下:
//判斷學號是否輸入 if (this.txtUserID.Text.Trim() == "") { Alert("學號不完整"); return false; } else if (this.txtUserName.Text.Trim() == "") //判斷姓名是否輸入 { Alert("姓名不完整"); return false; } //資訊檢查通過 return true;
當使用者輸入的資訊不正確時,Checklnfo將調用Alert方法顯示提示資訊對話方塊,
Alert方法實際上是固定MessageBox.Show方法的一些參數,利用其來彈出對話方塊,Alert方法實現非常簡單,僅僅需要下面一句代碼:
MessageBox.Show(null,message,"資訊提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
其中,message是Alert方法接受的參數。
在3個按鈕單擊事件中,程式碼分別實現對應的資料更新操作,這些操作前面都進行了詳細的講解,這裡不再贅述。
C#與資料庫訪問技術總結(九)之執行個體