c#事务、using释放资源、实体类的高级应用
来源:互联网 发布:淘宝带图评价 淘气值 编辑:程序博客网 时间:2024/05/18 03:03
namespace Day04_001事务{ class Program { static void Main(string[] args) { string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True"; string sql="insert into grade (gradename) values ('S2226')"; using (SqlConnection conn=new SqlConnection(str)) { SqlCommand cmd=new SqlCommand(sql,conn); conn.Open(); SqlTransaction tran = conn.BeginTransaction(); cmd.Transaction = tran; try { int count = cmd.ExecuteNonQuery(); if (count > 0) { Console.WriteLine("add ok"); } tran.Commit(); } catch (Exception) { tran.Rollback(); } } Console.ReadKey(); } }}
namespace using释放资源{ class Program { static void Main(string[] args) { string sql="select * from student"; using (SqlConnection conn = new SqlConnection(SqlHelper.constr)) { using (SqlCommand cmd = new SqlCommand(sql, conn)) { conn.Open(); Console.WriteLine("高傲的分割线"); Console.WriteLine("数据库"); } } Console.ReadKey(); } }}
namespace Day05_001实体类的高级应用{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True"; SqlConnection con=new SqlConnection(str); string sql="select * from grade"; SqlDataAdapter da=new SqlDataAdapter(sql,con); DataSet ds=new DataSet(); da.Fill(ds, "gradeinfo"); comboBox1.DataSource = ds.Tables[0]; comboBox1.ValueMember = "gradeid"; comboBox1.DisplayMember = "gradename"; } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True"; string gradename = comboBox1.Text; int gradeid = GetIdByName(gradename); SqlConnection con=new SqlConnection(str); string sql = "select * from subject where gradeid=@gradeid"; SqlParameter para=new SqlParameter("@gradeid",gradeid); SqlCommand cmd=new SqlCommand(sql,con); cmd.Parameters.Add(para); SqlDataAdapter da=new SqlDataAdapter(); da.SelectCommand = cmd; DataSet ds=new DataSet(); da.Fill(ds, "subjectinfo"); comboBox2.DataSource = ds.Tables["subjectinfo"]; comboBox2.ValueMember = "subjectid"; comboBox2.DisplayMember = "subjectname"; if (ds.Tables["subjectinfo"].Rows.Count == 0) { ds.Tables["subjectinfo"].Clear(); } } //根据id获取年级名称 private int GetIdByName(string gradename) { string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True"; SqlConnection con = new SqlConnection(str); string sql = "select gradeid from grade where gradename='" + gradename + "'"; SqlCommand cmd=new SqlCommand(sql,con); con.Open(); int gradeid = Convert.ToInt32(cmd.ExecuteScalar()); con.Close(); return gradeid; } //根据id获取科目名称 public int GetidByName(string subjectname) { string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True"; SqlConnection con = new SqlConnection(str); string sql = "select subjectid from subject where subjectname='" + subjectname + "'"; SqlCommand cmd = new SqlCommand(sql, con); con.Open(); int gradeid = Convert.ToInt32(cmd.ExecuteScalar()); con.Close(); return gradeid; } public DataTable StudentInfo(int gradeid, int subjectid ) { string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True"; string sql = @"select student.StudentNo, LoginPwd, StudentName, Gender, student.gradeid, Phone, Address, Birthday, Email from student, result,subject where subject.subjectid=result.subjectid and student.gradeid=@gradeid and result.subjectid=@subjectid "; using (SqlConnection con = new SqlConnection(str)) { SqlParameter[] paras = { new SqlParameter("@gradeid",gradeid), new SqlParameter("@subjectid",subjectid) }; SqlCommand cmd = new SqlCommand(sql, con); cmd.Parameters.AddRange(paras); con.Open(); SqlDataAdapter Adapter = new SqlDataAdapter(); Adapter.SelectCommand = cmd; DataSet da = new DataSet(); Adapter.Fill(da, "pp"); return da.Tables["pp"]; } } private void button1_Click(object sender, EventArgs e) { string gradename = comboBox1.Text; int gradeid = GetIdByName(gradename); string subjectname = comboBox2.Text; int subjectid = GetidByName(gradename); dataGridView1.DataSource = StudentInfo(gradeid, subjectid); } }}
0 0
- c#事务、using释放资源、实体类的高级应用
- C#里using的作用------释放资源
- 实体类的高级应用
- C# 中using 用来释放资源的用法
- C#里using的妙用(释放资源)
- C# Bitmap类释放资源的方法
- 资源的释放using与Dispose()
- C#的资源释放问题
- C# image资源的释放
- C#语言中的Using语句块确保资源被释放
- C#中using关键字在资源释放中的使用
- C#高级编程七十四天----释放非托管资源
- Hibernate事务的高级应用
- 提高C#编程水平的50个要点 之十五“使用 using 和 try-finally 来处理资源的释放”
- 使用using关键字同时释放多个资源的问题
- .NET 语言的 using statement 与资源释放
- 使用using关键字同时释放多个资源的问题
- c#是如何释放资源的
- Vijos 1144 小胖守皇宫 【树形DP】
- Freemarker 使用
- jdbc自动生成实体类
- poj2528Mayor's posters【离散化+线段树】
- Ionic使用cordova-hot-code-push实现热更新
- c#事务、using释放资源、实体类的高级应用
- URL传参中文乱码问题
- 某商店规定,某商品一次购买5件以上(包含5件)打九折,一次购买10件以上(包含10件)打八折,请根据单价和数量球总价
- 北航计算机机试17
- iScroll.js 用法参考【基于webkit的下拉刷新】
- 上传视频到阿里云服务器
- Android--傻瓜式分包插件
- 802.1x认证过程 TTLS
- 1.4.2.PHP5.6 女神级教程-女神的成长环境(Mac下 PHP开发环境 配置及安装 php5.6.x nginx mysql)