C# 连接数据库(添加,修改,删除)
2016-01-22 16:03
387 查看
一、添加年级信息到表中
二、修改学生的信息
三、删除学生信息
四、总结
操作添加,删除,修改数据,其实就是用SQLCommand命令对象中的ExecuteNonQuery()来执行,返回是一个整数型的数据。
/// <summary> /// 添加年级表 /// INSERT INTO grade VALUES('IT11') /// </summary> private static void addGrade(string gname) { SqlConnection con = null; try { //1.连接字符串 string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True"; //2.连接对象 con = new SqlConnection(str); //3.打开连接 con.Open(); //4.sql语句 //INSERT INTO grade VALUES('IT11') StringBuilder bulider = new StringBuilder(); bulider.AppendLine("INSERT"); bulider.AppendLine(" INTO "); bulider.AppendLine(" grade "); bulider.AppendLine(" VALUES "); bulider.AppendFormat("('{0}') ", gname); // Console.WriteLine(bulider.ToString()); //5.命名对象 SqlCommand cmd = new SqlCommand(bulider.ToString(), con); //6.执行sql语句 返回Int int count = cmd.ExecuteNonQuery(); // Console.WriteLine(count); if (count > 0) { Console.WriteLine("OK!"); } else { Console.WriteLine("no!"); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { con.Close(); } }
二、修改学生的信息
/// <summary> /// 修改学生表 /// UPDATE stu SET BornDate='1992-1-1',Phone='80011011' WHERE StudentName='白燕' /// </summary> private static void updateStudent(string date,string tel,string name) { SqlConnection con = null; try { //1.连接字符串 string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True"; //2.连接对象 con = new SqlConnection(str); //3.打开连接 con.Open(); //4.sql语句 //UPDATE stu SET BornDate='1992-1-1',Phone='80011011' WHERE StudentName='白燕' StringBuilder bulider = new StringBuilder(); bulider.AppendLine("UPDATE"); bulider.AppendLine(" stu "); bulider.AppendLine(" SET "); bulider.AppendFormat(" BornDate='{0}',Phone='{1}' ",date,tel); bulider.AppendLine(" WHERE "); bulider.AppendFormat("StudentName='{0}' ", name); // Console.WriteLine(bulider.ToString()); //5.命名对象 SqlCommand cmd = new SqlCommand(bulider.ToString(), con); //6.执行sql语句 返回Int int count = cmd.ExecuteNonQuery(); // Console.WriteLine(count); if (count > 0) { Console.WriteLine("OK!"); } else { Console.WriteLine("no!"); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { con.Close(); } }
三、删除学生信息
/// <summary> /// 删除学生表 /// delete stu WHERE StudentName='白燕' /// </summary> private static void delStudent(string name) { SqlConnection con = null; try { //1.连接字符串 string str = "Data Source=.;Initial Catalog=MySchool;Integrated Security=True"; //2.连接对象 con = new SqlConnection(str); //3.打开连接 con.Open(); //4.sql语句 // delete stu WHERE StudentName='白燕' StringBuilder bulider = new StringBuilder(); bulider.AppendLine("delete"); bulider.AppendLine(" stu "); bulider.AppendLine(" WHERE "); bulider.AppendFormat("StudentName='{0}' ", name); // Console.WriteLine(bulider.ToString()); //5.命名对象 SqlCommand cmd = new SqlCommand(bulider.ToString(), con); //6.执行sql语句 返回Int int count = cmd.ExecuteNonQuery(); // Console.WriteLine(count); if (count > 0) { Console.WriteLine("OK!"); } else { Console.WriteLine("no!"); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { con.Close(); } }
四、总结
操作添加,删除,修改数据,其实就是用SQLCommand命令对象中的ExecuteNonQuery()来执行,返回是一个整数型的数据。
相关文章推荐
- 利用redis + lua解决抢红包高并发的问题
- 统计数据库对象
- oracle AWR报告DB time和DB cpu
- 【转】不用程序mysql也可以查询今天、昨天、7天、近30天、本月、上一月的数据
- SQL Performance Analyzer
- mysqladmin命令修改Mysql密码
- MySQL 表分区梳理
- Oracle数据文件物理删除后的恢复
- xampp修改mysql数据库密码
- Sql Server 2016中增加了对JSON的内置支持
- mysql not in 或in 优化
- 【转】MYSQL 存储过程定时操作数据库
- Sql Server 2016中增加了对JSON的内置支持
- 微软SqlServerHelper
- mysql中myisamchk命令使用总结
- MySql—解压缩版本的安装与配制
- vc获取Access/ SqlServer 获取主键
- sql基础05
- MySql的存储引擎
- sql语法:inner join on, left join on, right join on详细使用方法