标签:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; //引用命名空间 namespace 增删改查 { class Program { static void Main(string[] args) {
//增:insert into //1.连接数据库 SqlConnection con = new SqlConnection("server=.; database=ADO; user=sa; pwd=ray;"); //2.编写SQL语句 string sql = "insert into Student values(‘钱八‘,1,‘1990-01-05‘,‘90‘,‘22050219900105061X‘,‘18853315510‘)"; //3.创建一个执行SQL语句的对象 using (SqlCommand cmd = new SqlCommand(sql, con)) { con.Open(); //4.打开连接 int z = cmd.ExecuteNonQuery(); //5.执行语句 Console.WriteLine("成功插入了{0}行数据\n" + sql, z); con.Close(); //6.关闭连接 Console.ReadKey(); } } } }
//删:delete SqlConnection con = new SqlConnection("server=.; database=ADO; user=sa; pwd=ray;"); string sql = "delete from Student where Sname=‘钱八‘"; using (SqlCommand cmd = new SqlCommand(sql, con)) { con.Open(); int s = cmd1.ExecuteNonQuery(); Console.WriteLine("成功删除了{0}行数据\n" + sql, s); con.Close(); Console.ReadKey(); }
//(3)改:update...set SqlConnection con = new SqlConnection("server=.; database=ADO; user=sa; pwd=ray;"); string sql = "update Student set Sname=‘张小三‘,Sbirthday=‘2000-01-01‘,Sscore=100 where Scode=10001"; using (SqlCommand cmd=new SqlCommand(sql,con)) { con.Open(); int g = cmd.ExecuteNonQuery(); Console.WriteLine("成功更改了{0}行数据\n"+sql,g); con.Close(); Console.ReadKey(); }
//(4)查:select
SqlConnection con = new SqlConnection("server=.; database=ADO; user=sa; pwd=ray;"); string sql = "select count(*) from Student"; using (SqlCommand cmd=new SqlCommand(sql,con)) { con.Open(); object c=cmd.ExecuteScalar(); Console.WriteLine("Student表中共有{0}条数据",c); con.Close(); Console.ReadKey(); }
标签:
原文地址:http://www.cnblogs.com/xiao55/p/5604817.html