查询基础以及数据库的连接

来源:互联网 发布:q群排名优化 编辑:程序博客网 时间:2024/06/03 23:50
欢迎来到unity学习unity培训unity企业培训教育专区,这里有很多U3D资源U3D培训视频U3D教程U3D常见问题U3D项目源码,我们致力于打造业内unity3d培训、学习第一品牌。
一、查询基础
--创建数据库
create database Class2
--使用数据库
use Class2
--创建表
create table student
--查询表
select*from Student
--查询部分行
select name,password from student where id<>3
--命名列名并查询
select id 学号,name 姓名,password 密码 from student 
--为表添加一列
alter table Student add age int null
--更改新添加列的属性
alter table Student alter column age varchar(10)
--删除新添加的列
alter table Student drop column age
--更新表中一条数据
update Student set password=234 where name='张三'
--删除表中一条数据
delete from Student where name='张三'
--向表中插入一条数据
insert into student(name,password)values('王五',234)
--去掉重复字段查询记录
select distinct name from student
--合并查询(合并两表中相同的字段)
select*from student union select*from student3
--查询非空行
select name from student where name is not null
--使用常量列
select 姓名=name,'密码' password from student
--限制固定行数
select top 1 id,name,password from student where name='王五'
--返回百分比行数
select top 50 percent id,name,password from student where name='王五'
--排序
select id 学号,name 姓名,password 密码
from student 
where password>111
--按多列排序
select id,name,password from student 
where name='王五'
order by password asc

二、连接数据库
  1. class Program
  2.     {
  3.         public void Get()
  4.         {
  5.             SqlConnection c = new SqlConnection("server=.;database=Second;Trusted_Connection=SSPI");
  6.             SqlCommand cmd = new SqlCommand("select*from gui", c);
  7.             c.Open();
  8.             SqlDataReader myreader = cmd.ExecuteReader();
  9.             while (myreader.Read())
  10.             { 
  11.                 Console.WriteLine(myreader.GetValue(0)+" "+myreader.GetValue(1)+" "+myreader.GetValue(2));
  12.             }
  13.             myreader.Close();
  14.             c.Close();
  15.         }
  16.         static void Main(string[] args)
  17.         {
  18.             Program c = new Program();
  19.             c.Get();

  20.         }
  21.     }
三、插入功能
  1. public int insert()
  2. {
  3.             SqlConnection con = new SqlConnection("server=127.0.0.1;uid =sa; pwd =wang;database =second");
  4.             string sql = "insert into users(name,password) values('王五','789')";
  5.             SqlCommand comm = new SqlCommand(sql, con);
  6.             con.Open();
  7.             int count = comm.ExecuteNonQuery();
  8.             return count;
  9.  }
四、查询功能
  1. public List<Users> select() 
  2. {
  3.             List<Users> list = new List<Users>();
  4.             SqlConnection con = new SqlConnection("server=127.0.0.1;uid = sa; pwd =wang;database =second");
  5.             SqlCommand cmd = new SqlCommand("select * from users", con);
  6.             con.Open();
  7.             SqlDataReader myreader = cmd.ExecuteReader();
  8.             while (myreader.Read())
  9.             {
  10.                 Users u = new Users();
  11.                 u.Id =(int) myreader.GetValue(0);
  12.                 u.Name =(string) myreader.GetValue(1);
  13.                 u.Password = (string)myreader.GetValue(2);
  14.                 list.Add(u);
  15.             }
五、调用查询功能
  1.  foreach(Users u in list)
  2. {
  3.        Console.WriteLine(u.Id+"   "+u.Name);
  4.  }
  5.        List<Users> list = select();
  6.        IEnumerator<Users> it = list.GetEnumerator();
  7.   while(it.MoveNext())
  8. {
  9.        Console.WriteLine(it.Current.Id + "  " + it.Current.Name);
  10. }

更多精彩请点击 http://www.gopedu.com/article

0 0