select * from users
2. 查询部分行
select id,name from users where name='张三'
select id,name from users where name<>'张三'
3.去掉重复字段查询记录
select distinct name from users
4.合并查询(合并两表中相同的字段)所查字段要类型相同
select id from users union select id from score
5. 用AS来命名列
select id as 编号,name as 姓名 from users
6. 用 = 来命名列
select 编号 =id ,姓名=name from users
select '编号' =id ,'姓名'=name from users
7.查询空行
select id, name from users where password is null
8.查询非空行
select name from users where name is not null
9.使用常量列(默认值)
select name as 姓名 ,'密码' as password from users
10.限制固定行数
select top 3 * from users
11. 返回百分之多少行
select top 50 percent * from users
12. 排序
SELECT <列名> FROM <表名> [WHERE <查询条件表达式>] [ORDER BY <排序的列名>[ASC或DESC]]
asc:升序 desc:降序
升序
select * from users order by id
select * from users order by id asc
降序
select * from users order by id desc
按多列排序(当排序的值相同时,按第二个字段排序)
select * from users order by name,id
- public void Get()
- {
- SqlConnection con = new SqlConnection("server=.;database =users;Trusted_Connection=SSPI");
- SqlCommand cmd=new SqlCommand("select * from teacher",con);
- con.Open();
- SqlDataReader myreader=cmd.ExecuteReader();
- while (myreader.Read())
- {
- Console.WriteLine(myreader.GetValue(0) + " " +myreader.GetValue(1));
- }
- myreader.Close();
- con.Close();
- }
- public int insert()
- {
- SqlConnection con = new SqlConnection("server=.;database =users;Trusted_Connection=SSPI");
- string sql = "insert into teacher(name,age,id) values('赵六',23,114)";
- SqlCommand comm = new SqlCommand(sql, con);
- con.Open();
- int count = comm.ExecuteNonQuery();
- return count;
- }
{
public string name;
{
this.name = name;
}
public string Getname()
{
return name;
}
public int age;
public void Setage(int age)
{
this.age = age;
public int Getage()
{
return age;
}
public int id;
{
set { id = value; }
get { return id; }
}
}
public List<People> select()
{
List<People> list = new List<People>();
SqlConnection con = new SqlConnection("server=.;database=Joker;Trusted_Connection=SSPI");
- con.Open();
SqlCommand com = new SqlCommand("select*from teacher",con);
SqlDataReader myreader = com.ExecuteReader();
while (myreader.Read())
{
People p = new People ();
p.Id=(int)myreader.GetValue(0);
p.Setname ((string)myreader.GetValue(1));
p.Getname();
p.Setage((int)myreader.GetValue(2));
p.Getage();
list.Add(p);
}
myreader.Close();
- con.Close();
return list;
foreach (People p in list)
{
Console.WriteLine(p.Getname()+" "+p.Getage()+" "+p.Id);
}
}
{
Class3 a = new Class3();
a.select();
a.Get();
IEnumerator<People> it = list.GetEnumerator();
while (it.MoveNext())
{
Console.WriteLine(it.Current.Getage()+" "+it.Current.Getname ()+" "+it.Current.Id);
}