VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之WinForm之窗体应用程序(2)

181 string sql = string.Format("select sid,sname,ssex,saddress,semail from students where ssex={0}", sex); 182 //创建数据适配器对象 183 sda = new SqlDataAdapter(sql, DBHelper.connection); 184 int result = sda.Fill(ds); 185 if (result > 0) 186 { 187 this.dgvStudentInfo.DataSource = ds.Tables[0]; 188 } 189 else 190 { 191 MessageBox.Show("无查询结果"); 192 } 193 } 194 195 private void btnSearchByName1_Click(object sender, EventArgs e) 196 { 197 //清空数据集中表信息 198 ds.Tables.Clear(); 199 200 //根据学员姓名查询学员信息(模糊查询) 201 string name = this.txtName2.Text.Trim(); 202 string sql = string.Format("select sid,sname,ssex,saddress,semail from students where sname like '%{0}%'", name); 203 //创建数据适配器对象 204 sda = new SqlDataAdapter(sql, DBHelper.connection); 205 int result = sda.Fill(ds); 206 if (result > 0) 207 { 208 this.dgvStudentInfo.DataSource = ds.Tables[0]; 209 } 210 else 211 { 212 MessageBox.Show("无查询结果"); 213 } 214 215 } 216 217 private void btnSearchBySex3_Click(object sender, EventArgs e) 218 { 219 //清空ListView中的项 220 this.lstStudentInfo.Items.Clear(); 221 222 //根据性别查询学员信息 223 string sex; 224 if (this.radMan3.Checked) 225 { 226 sex = this.radMan3.Tag.ToString(); 227 } 228 else 229 { 230 sex = this.radWoman3.Tag.ToString(); 231 } 232 MessageBox.Show("性别的值为:" + sex); 233 234 string sql = string.Format("select sid,sname,ssex,saddress,semail from students where ssex={0}", sex); 235 try 236 { 237 SqlCommand command = new SqlCommand(sql, DBHelper.connection); 238 DBHelper.connection.Open(); 239 SqlDataReader sdr = command.ExecuteReader(); 240 while (sdr.Read()) 241 { 242 //1. 243 ListViewItem lvi = new ListViewItem(sdr["sid"].ToString()); 244 //2. 245 if (sdr["ssex"].ToString().ToLower() == "true") 246 { 247 sex = ""; 248 } 249 else 250 { 251 sex = ""; 252 } 253 lvi.SubItems.AddRange(new string[] { sdr["sname"].ToString(), sex, sdr["saddress"].ToString(), sdr["semail"].ToString() }); 254 //3. 255 this.lstStudentInfo.Items.Add(lvi); 256 } 257 //关闭sdr 258 sdr.Close(); 259 } 260 catch (Exception ex) 261 { 262 263 MessageBox.Show(ex.Message); 264 } 265 finally 266 { 267 DBHelper.connection.Close(); 268 } 269 270 271 272 } 273 274 275 private void btnSearchBySex4_Click(object sender, EventArgs e) 276 { 277 //清空ListView中的项 278 this.lstStudentInfo.Items.Clear(); 279 280 //根据性别查询学员信息 281 string sex; 282 if (this.cboSex2.Text != "") 283 { 284 if (this.cboSex2.Text == "") 285 { 286 sex = "1"; 287 } 288 else 289 { 290 sex = "0"; 291 } 292 MessageBox.Show("性别的值为:" + sex); 293 } 294 else 295 { 296 MessageBox.Show("请选择性别"); 297 return; 298 } 299 300 string sql = string.Format("select sid,sname,ssex,saddress,semail from students where ssex={0}", sex); 301 try 302 { 303 SqlCommand command = new SqlCommand(sql, DBHelper.connection); 304 DBHelper.connection.Open(); 305 SqlDataReader sdr = command.ExecuteReader(); 306 while (sdr.Read()) 307 { 308 //1. 309 ListViewItem lvi = new ListViewItem(sdr["sid"].ToString()); 310 //2. 311 if (sdr["ssex"].ToString().ToLower() == "true") 312 { 313 sex = ""; 314 } 315 else 316 { 317 sex = ""; 318 } 319 lvi.SubItems.AddRange(new string[] { sdr["sname"].ToString(), sex, sdr["saddress"].ToString(), sdr["semail"].ToString() }); 320 //3. 321 this.lstStudentInfo.Items.Add(lvi); 322 } 323 //关闭sdr 324 sdr.Close(); 325 } 326 catch (Exception ex) 327 { 328 329 MessageBox.Show(ex.Message); 330 } 331 finally 332 { 333 DBHelper.connection.Close(); 334 } 335 } 336 337 private void btnSearchByName2_Click(object sender, EventArgs e) 338 { 339 //清空ListView中的项 340 this.lstStudentInfo.Items.Clear(); 341 342 //根据学员姓名查询学员信息(模糊查询) 343 string name = this.txtName3.Text.Trim(); 344 string sql = string.Format("select sid,sname,ssex,saddress,semail from students where sname like '%{0}%'", name); 345 try 346 { 347 SqlCommand command = new SqlCommand(sql, DBHelper.connection); 348 DBHelper.connection.Open(); 349 SqlDataReader sdr = command.ExecuteReader(); 350 while (sdr.Read()) 351 { 352 //1. 353 ListViewItem lvi = new ListViewItem(sdr["sid"].ToString()); 354 //2. 355 string sex; 356 if (sdr["ssex"].ToString().ToLower() == "true") 357 { 358 sex = ""; 359 } 360 else 361 { 362 sex = ""; 363 } 364 lvi.SubItems.AddRange(new string[] { sdr["sname"].ToString(), sex, sdr["saddress"].ToString(), sdr["semail"].ToString() }); 365 //3.将主键值写到lvi的Tag属性中 366 lvi.Tag = sdr["sid"].ToString(); 367 368 //4. 369 this.lstStudentInfo.Items.Add(lvi); 370 } 371 //关闭sdr 372 sdr.Close(); 373 } 374 catch (Exception ex) 375 { 376 377 MessageBox.Show(ex.Message); 378 } 379 finally 380 { 381 DBHelper.connection.Close(); 382 } 383 } 384 385 private void dgvStudentInfo_CellClick(object sender, DataGridViewCellEventArgs e) 386 { 387 //单元格点击事件 388 if (this.dgvStudentInfo.SelectedRows.Count > 0) 389 { 390 string id = this.dgvStudentInfo.SelectedRows[0].Cells["sid"].Value.ToString(); 391 MessageBox.Show(id); 392 } 393 } 394 395 private void lstStudentInfo_MouseClick(object sender, MouseEventArgs e) 396 { 397 //ListView控件点击事件 398 if (this.lstStudentInfo.SelectedItems.Count > 0) 399 { 400 string id = this.lstStudentInfo.SelectedItems[0].Tag.ToString(); 401 MessageBox.Show(id); 402 } 403 } 404 } 405 }
复制代码
复制代码
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 using System.Data.SqlClient;//
  9 
 10 namespace DataBaseOperation
 11 {
 12     public partial class frmUpdate : Form
 13     {
 14         public frmUpdate()
 15         {
 16             InitializeComponent();
 17         }
 18 
 19         private void btnSearchStudentInfo_Click(object sender, EventArgs e)
 20         {
 21             //根据学号查询学员信息
 22             string id = this.txtNum.Text.Trim();
 23             if (id != "")
 24             {
 25                 string sql = string.Format("select sname,ssex,saddress,semail from students where sid={0}", id);
 26                 try
 27                 {
 28                     SqlCommand command = new SqlCommand(sql, DBHelper.connection);
 29                     DBHelper.connection.Open();
 30                     SqlDataReader sdr = command.ExecuteReader();
 31                     if (sdr.Read())
 32                     {
 33                         this.txtName.Text = sdr["sname"].ToString();
 34                         // MessageBox.Show("性别字段的值:"+sdr["ssex"].ToString());
 35                         if (sdr["ssex"].ToString().ToLower() == "true")
 36                         {
 37                             this.radMan.Checked = true;
 38                         }
 39                         else
 40                         {
 41                             this.radWoman.Checked = true;
 42                         }
 43                         this.txtAddress.Text = sdr["saddress"].ToString();
 44                         this.txtEmail.Text = sdr["semail"].ToString();
 45 
 46                         //激活或屏蔽窗体中部分控件
 47                         this.txtNum.Enabled = false;
 48                         this.txtName.Enabled = true;
 49                         this.txtAddress.Enabled = true;
 50                         this.txtEmail.Enabled = true;
 51                         this.radMan.Enabled = true;
 52                         this.radWoman.Enabled = true;
 53                     }
 54                     else
 55                     {
 56                         MessageBox.Show("查无此人!");
 57                         this.txtNum.Text = "";
 58                         this.txtNum.Focus();
 59                         this.txtName.Text = "";
 60                         this.radMan.Checked = true;
 61                         this.txtAddress.Text = "";
 62                         this.txtEmail.Text = "";
 63                     }
 64 
 65                     sdr.Close();
 66 
 67 
 68                 }
 69                 catch (Exception ex)
 70                 {
 71 
 72                     MessageBox.Show(ex.Message);
 73                 }
 74                 finally
 75                 {
 76                     DBHelper.connection.Close();
 77                 }
 78             }
 79             else
 80             {
 81                 MessageBox.Show("请输入学号!");
 82             }
 83         }
 84 
 85         private void btnUpdate_Click(object sender, EventArgs e)
 86         {
 87             //1.
 88             string id = this.txtNum.Text.Trim();
 89             string name = this.txtName.Text.Trim();
 90             string sex;
 91             if (this.radMan.Checked)
 92             {
 93                 sex = "1";
 94             }
 95             else
 96             {
 97                 sex = "0";
 98             }
 99             string address = this.txtAddress.Text.Trim();
100             string email = this.txtEmail.Text.Trim();
101 
102             //2.
103             string sql = string.Format("update students set sname='{0}',ssex={1},saddress='{2}',semail='{3}' where sid={4}", name, sex, address, email, id);
104 
105             //3.
106             try
107             {
108                 SqlCommand command = new SqlCommand(sql, DBHelper.connection);
109                 DBHelper.connection.Open();
110                 int result = command.ExecuteNonQuery();
111                 if (result > 0)
112                 {
113                     MessageBox.Show("更新完毕!");
114                     //激活或屏蔽窗体中部分控件
115                     this.txtNum.Enabled = true;
116                     this.txtName.Enabled = false;
117                     this.txtAddress.Enabled = false;
118                     this.txtEmail.Enabled = false;
119                     this.radMan.Enabled = false;
120                     this.radWoman.Enabled = false;
121 
122                 }
123                 else
124                 {
125                     MessageBox.Show("更新操作失败!");
126                 }
127             }
128             catch (Exception ex)
129             {
130 
131                 MessageBox.Show(ex.Message);
132             }
133             finally
134             {
135                 DBHelper.connection.Close();
136             }
137         }
138     }
139 }
复制代码

 


相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号