C#中的经典代码.doc

上传人:自*** 文档编号:124939386 上传时间:2020-03-14 格式:DOC 页数:6 大小:79.81KB
返回 下载 相关 举报
C#中的经典代码.doc_第1页
第1页 / 共6页
C#中的经典代码.doc_第2页
第2页 / 共6页
C#中的经典代码.doc_第3页
第3页 / 共6页
C#中的经典代码.doc_第4页
第4页 / 共6页
C#中的经典代码.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《C#中的经典代码.doc》由会员分享,可在线阅读,更多相关《C#中的经典代码.doc(6页珍藏版)》请在金锄头文库上搜索。

1、C#中的经典代码普通控件的增删改查:例如:文本框,下拉框等,所有的操作,首先导入命名空间:using System.Data.SqlClient;增加代码:/连接字符串 SqlConnection conn = new SqlConnection(Data Source=.;Initial Catalog=数据库名称;Integrated Security=True); try conn.Open(); string sql = string.Format(insert into 表名 values( 0,1,2), this.textBox1.Text, int.Parse(this.tex

2、tBox2.Text), this.textBox3.Text); SqlCommand comm = new SqlCommand(sql, conn); int number = comm.ExecuteNonQuery(); if (number 0) MessageBox.Show(插入成功); else MessageBox.Show(插入失败); catch (Exception ex) MessageBox.Show(数据库异常); finally /连接始终要关闭 conn.Close(); 注意:在插入的SQL语句中,占位符需要注意:字符型的要加单引号,数字型的不加。在给占位

3、符赋值时需要注意:数字型的要强转。删除代码:/连接字符串 SqlConnection conn = new SqlConnection(Data Source=.;Initial Catalog=数据库名称;Integrated Security=True); try conn.Open(); string sql = string.Format(delete from 表名 where 列名=0 , int.Parse(this.textBox2.Text); SqlCommand comm = new SqlCommand(sql, conn); int number = comm.Exe

4、cuteNonQuery(); if (number 0) MessageBox.Show(删除成功); else MessageBox.Show(删除失败); catch (Exception ex) MessageBox.Show(数据库异常); finally /连接始终要关闭 conn.Close(); 修改的代码: /连接字符串 SqlConnection conn = new SqlConnection(Data Source=.;Initial Catalog=数据库名;Integrated Security=True); try conn.Open(); string sql

5、= string.Format(update 表名 set 数据库列名=0,数据库列名=1,this.textBox1.Text, int.Parse(this.textBox2.Text); SqlCommand comm = new SqlCommand(sql, conn); int number = comm.ExecuteNonQuery(); if (number 0) MessageBox.Show(更新成功); else MessageBox.Show(更新失败); catch (Exception ex) MessageBox.Show(数据库异常); finally /连接

6、始终要关闭 conn.Close(); 查询出某列绑定到下拉列表控件中string name=; SqlConnection conn = new SqlConnection(Data Source=.;Initial Catalog=数据库名;Integrated Security=True); try conn.Open(); string sql=select 某列 from 表名; SqlCommand comm = new SqlCommand(sql, conn); SqlDataReader dr=comm.ExecuteReader(); while (dr.Read() na

7、me=dr数据库某列名.ToString(); /加载到页面的下拉列表控件中 boBox1.Items.Add(name); dr.Close(); catch (Exception ex) MessageBox.Show(数据库异常); finally conn.Close(); 如果绑定的是datagridview控件:查询代码:SqlConnection conn = new SqlConnection(Data Source=.;Initial Catalog=AddressBookDB;Integrated Security=True); string sql=select * fr

8、om 表名; SqlDataAdapter da=new SqlDataAdapter(sql,conn); DataSet ds=new DataSet(); da.Fill(ds,表名); this.dataGridView1.DataSource=ds.Tables0;更新代码: SqlConnection conn=new SqlConnection (Data Source=.;Initial Catalog=AddressBookDB;Integrated Security=True); SqlDataAdapter da=new SqlDataAdapter(); SqlComm

9、andBuilder comm=new SqlCommandBuilder(da); da.Update(ds,表名);清空代码:重置按钮也叫清空按钮: this.textBox1.Text = ; boBox1.Text = ;窗体的关闭:this.Close();应用程序的关闭: Application.Exit();MDI子窗体显示:IsMdiContainer:true Form2 objfom = new Form2();/实例化子窗体 objfom.MdiParent = this;/设置父窗体为this objfom.Show();/普通显示 objfom.ShowDialog();/模式窗体显示窗体最大化:WindowState:Maximized窗体首次加载:StartPosition:四个属性 /为空判断 private bool NewMethod() if (this.textBox1.Text.Trim() = ) MessageBox.Show(姓名不能为空); this.te

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > 计算机应用/办公自动化

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号