欢迎来到
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
二、连接数据库
- class Program
- {
- public void Get()
- {
- SqlConnection c = new SqlConnection("server=.;database=Second;Trusted_Connection=SSPI");
- SqlCommand cmd = new SqlCommand("select*from gui", c);
- c.Open();
- SqlDataReader myreader = cmd.ExecuteReader();
- while (myreader.Read())
- {
- Console.WriteLine(myreader.GetValue(0)+" "+myreader.GetValue(1)+" "+myreader.GetValue(2));
- }
- myreader.Close();
- c.Close();
- }
- static void Main(string[] args)
- {
- Program c = new Program();
- c.Get();
- }
- }
三、插入功能
- public int insert()
- {
- SqlConnection con = new SqlConnection("server=127.0.0.1;uid =sa; pwd =wang;database =second");
- string sql = "insert into users(name,password) values('王五','789')";
- SqlCommand comm = new SqlCommand(sql, con);
- con.Open();
- int count = comm.ExecuteNonQuery();
- return count;
- }
四、查询功能
- public List<Users> select()
- {
- List<Users> list = new List<Users>();
- SqlConnection con = new SqlConnection("server=127.0.0.1;uid = sa; pwd =wang;database =second");
- SqlCommand cmd = new SqlCommand("select * from users", con);
- con.Open();
- SqlDataReader myreader = cmd.ExecuteReader();
- while (myreader.Read())
- {
- Users u = new Users();
- u.Id =(int) myreader.GetValue(0);
- u.Name =(string) myreader.GetValue(1);
- u.Password = (string)myreader.GetValue(2);
- list.Add(u);
- }
- }
五、调用查询功能
- foreach(Users u in list)
- {
- Console.WriteLine(u.Id+" "+u.Name);
- }
- List<Users> list = select();
- IEnumerator<Users> it = list.GetEnumerator();
- while(it.MoveNext())
- {
- Console.WriteLine(it.Current.Id + " " + it.Current.Name);
- }
更多精彩请点击 http://www.gopedu.com/article