欢迎来到unity学习、unity培训、unity企业培训教育专区,这里有很多U3D资源、U3D培训视频、U3D教程、U3D常见问题、U3D项目源码,我们致力于打造业内unity3d培训、学习第一品牌。一、SQL查询代码。
1、查询全部的行和列。
select*from student
2、查询部分行。
select name,password from student where id<>3 //查询student表中id不是3的name与password信息。
3、命名列名查询。
select id as 学号,name as 姓名,password as 密码 from student
4、去掉重复字段查询记录。
select distinct name from student //去掉student中name重复的行。
5、合并查询。
select*from student union select*from student2
6、查询非空行。
select name from student where name is not null
7、使用常量列。
select 姓名=name,'密码' password from student
8、限制固定行数。
select top 2 id,name,password from student where name='王五'
9、返回百分比行数。
select top 50 percent id,name,password from student where name='王五'
10、排序。
select id,name,password from student
where name='王五'
order by password asc
11、按多列排序。
select id as 学号,name as 姓名,password as 密码
from student
where password>100
order by id,name
二、在VS中往数据库插入(删除、更新)数据。
class Program
{
public int Insert()
{
SqlConnection s = new SqlConnection("server=.;database=Class2;Trusted_Connection=SSpI");
//string sql = "insert into student(name,password) values('王五','789')"; //插入数据。
//string sql = "delete from student where password=234"; //删除数据。
string sql = "update Student set password=234 where name='王五'"; //更新数据
SqlCommand comm = new SqlCommand(sql, s);
s.Open();
int count = comm.ExecuteNonQuery();
return count;
}
static void Main(string[] args)
{
Console.WriteLine(new Program().Insert());
Console.ReadKey();
}
}
三、用VS查询表中数据。
class Program
{
public List<Person> GetNum()
{
List<Person> p = new List<Person>();
SqlConnection s = new SqlConnection("server=.;database=Class2;Trusted_Connection=SSpI");
s.Open();
SqlCommand cmd = new SqlCommand("select*from student", s);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Person ss = new Person();
ss.SetId((int)reader.GetValue(0));
ss.SetName((string)reader.GetValue(1));
ss.SetPassword((int)reader.GetValue(2));
p.Add(ss);
}
reader.Close();
s.Close();
return p;
}
static void Main(string[] args)
{
List<Person> p = new Program().GetNum();
foreach (Person ss in p)
{
Console.WriteLine(ss.GetId() + " " + ss.GetName() + " " + ss.GetPassword());
}
Console.ReadKey();
}
}
学习Unity3D之SQL代码查询和用VS向数据库插入、删除、更新、查询信息。
最新推荐文章于 2022-01-14 21:34:06 发布
