1、查询全部
select*from users
2、查询部分数据
select id name from users where name='zhangsan'
3、去掉重复字段查询记录(这是针对一个字段,不能十多个)
select distinct name from
4、合并查询(合并两表中相同的字段)
select*from users union select*from score
5、用AS来命名列
select id as 编号,name as 姓名 from users
6、用=来命名列
select ‘编号’=id,‘姓名’=name from users
7、查询空行
select name from users where password is null
8、查询非空行
select name from users where name is not null
9、使用常量列
select 姓名=name,‘密码’as password from users
10、限制固定行数
select top 3*from users
11、返回百分之多少行
select top 20 percent *from users
12、升序
select*from users order by id
或
select*from users order by id asc
13、降序
select*from users order by id desc
14、按多列排序(当排序的值相同时,按第二个字段排序)
select *from users order by name,id
*连接数据库*
程序访问数据库的步骤
程序访问数据库的步骤
public void Get
{
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())
{
Console.WriteLine(myreader.GetValue(0) + " " +
myreader.GetValue(1));
}
myreader.Close();
con.Close();
}
插入功能
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);
}
更多精彩请关注:http://www.gopedu.com/