查询单条数据
//数据库语句
select ClassId, ClassName from StudentClass where ClassId=1
//C# 数据库查询多条数据
//定义SQL语句
string sql = "select ClassId, ClassName from StudentClass where ClassId=1";
//连接ADO.NET做数据查询
SqlConnection conn = new SqlConnection(connString);//连接数据库(连接数据库实例)
SqlCommand cmd = new SqlCommand(sql, conn); // 发送sql命令
StudentClass student = new StudentClass();
conn.Open(); //打开数据库连接
//CommandBehavior.CloseConnection 检测并自动关闭数据库连接
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
//解析数据
if (reader.Read())
{
student = new StudentClass()
{
ClassId = Convert.ToInt32(reader["ClassId"]),
ClassName = reader["ClassName"].ToString()
};
}
//展示数据
Console.WriteLine("班级Id" + student.ClassId + "\t" + "班级名称" + student.ClassName);
Console.Read();
//for(int j=0;j< result.l)
List<test> list = new List<test>();
List<int> i = new List<int>();
查询多条数据
//数据库语句
select ClassId, ClassName from StudentClass
//C# 数据库查询多条数据
//定义SQL语句
string sql = "select ClassId, ClassName from StudentClass";
//连接ADO.NET做数据查询
SqlConnection conn = new SqlConnection(connString);//连接数据库(连接数据库实例)
SqlCommand cmd = new SqlCommand(sql, conn); // 发送sql命令
List<StudentClass> sutlist = new List<StudentClass>();
conn.Open(); //打开数据库连接
//CommandBehavior.CloseConnection 检测并自动关闭数据库连接
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
//解析数据
while(reader.Read())
{
StudentClass student = new StudentClass()
{
ClassId = Convert.ToInt32(reader["ClassId"]),
ClassName = reader["ClassName"].ToString()
};
sutlist.Add(student);
}
//展示数据
foreach (var student in sutlist)
{
Console.WriteLine("班级Id" + student.ClassId + "\t" + "班级名称" + student.ClassName);
};
Console.Read();
增
//数据库语句
//字符串前加N 表示转义中文字符
insert into StudentClass (ClassId,ClassName) values(5,N'HTML5')
insert into StudentClass (ClassId,ClassName) values(6,N'Layui')
insert into StudentClass (ClassId,ClassName) values(7,N'C#进阶')
//c#操作数据插入
string sql = "insert into StudentClass (ClassId,ClassName) values(8,N'python进阶')";
//连接ADO.NET做数据查询
SqlConnection conn = new SqlConnection(connString);//连接数据库(连接数据库实例)
SqlCommand cmd = new SqlCommand(sql, conn); // 发送sql命令
conn.Open(); //打开数据库连接
int result = cmd.ExecuteNonQuery();
Console.WriteLine("返回结果为: " + result);
//解析数据
Console.Read();
删
//数据库删除语句
//条件删除
delete from StudentClass where ClassId=8
//c#中条件删除数据库数据
//和增加相比只需要修改SQL语句
//定义SQL语句
string sql = "delete from StudentClass where ClassId=7";
//连接ADO.NET做数据查询
SqlConnection conn = new SqlConnection(connString);//连接数据库(连接数据库实例)
SqlCommand cmd = new SqlCommand(sql, conn); // 发送sql命令
conn.Open(); //打开数据库连接
int result = cmd.ExecuteNonQuery();
Console.WriteLine("返回结果为: " + result);
//解析数据
Console.Read();
改
//数据库修改数据语句
//条件修改
update StudentClass set ClassName=N'Layui基础' where ClassId=6
//c#中条件修改数据库数据
//和增加相比只需要修改SQL语句
//定义SQL语句
string sql = "update StudentClass set ClassName=N'Layui进阶' where ClassId=6";
//连接ADO.NET做数据查询
SqlConnection conn = new SqlConnection(connString);//连接数据库(连接数据库实例)
SqlCommand cmd = new SqlCommand(sql, conn); // 发送sql命令
conn.Open(); //打开数据库连接
int result = cmd.ExecuteNonQuery();
Console.WriteLine("返回结果为: " + result);
//解析数据
Console.Read();