修改:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace 修改 { class Program { static void Main(string[] args) { Console.Write("请输入要修改的成员姓名:"); string name = Console.ReadLine(); Console.Write("请输入要修改的成员的生日:"); string bir = Console.ReadLine(); //创建数据库连接类 SqlConnection conn = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123;");//编写连接字符串 //创建数据库操作类,创建过程是与刚创建的连接对象匹配起来 SqlCommand comm = conn.CreateCommand(); //编写操作语句 TSQL语句 comm.CommandText="update Users set birthday='"+bir+"' where usename='"+name+"'"; conn.Open(); int i = comm.ExecuteNonQuery(); conn.Close(); if (i > 0) Console.WriteLine("修改成功!"); else Console.WriteLine("修改失败!"); Console.ReadKey(); } } }
查询:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //创建数据库连接类 SqlConnection con = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123;"); //创建数据库操作类,创建过程是与刚创建的连接对象匹配起来 SqlCommand com = con.CreateCommand(); //编写操作语句 TSQL语句 com.CommandText = "select code,usename,password,nick,sex,nation,class,(select nationname from Nation where nationcode=Users.nation) as 民族,(select classname from Class where classcode=Users.class) as 班级,birthday from Users";// con.Open(); //读取操作,返回读取器对象 SqlDataReader dr= com.ExecuteReader(); if (dr.HasRows)//获取一个bool值,判断是否含有一行或多行 { while (dr.Read())//获取一个bool值,前进到下一条记录 { Console.WriteLine(dr["code"] + " " + dr["usename"] + " " + dr["password"] + " " + dr["nick"] + " " + (Convert.ToBoolean(dr["sex"]) ? "男" : "女") + " " + dr["民族"] + " " + dr["班级"] + " " + (Convert.ToDateTime(dr["birthday"]).ToShortDateString())); } } con.Close(); Console.ReadLine(); } } }