11.26ADO练习

来源:互联网 发布:单片机控制8个led灯 编辑:程序博客网 时间:2024/06/14 10:15

ADO中连接数据库并查询数据库里面的数据
新建控制台应用程序
class Program
{
static void Main(string[] args)
{string constr="data source=服务器名称;initial catalog=要查询的数据库;user id=  password=";

using(SqlConnection con=new SqlConnection (constr))
{
string  sql="select count(*) from Student";
using(SqlCommand cmd=new SqlCommand(sql,con))
{
con.open();
int  number=(int)cmd.ExcuteScalar();
//能返回一行一列的数据(第一行第一列)
console.writeline("student表中一共有{0}条数据",number);
}
}
console.writeLine("ok");
console.readkey();
}

command 对象的ExecuteScalar()方法,返回的是查询(select)


--------------------------------------
全局变量  @@identity//获取表中最新自动增长的值
string constr="data source=服务器名称;initial catalog=要查询的数据库;user id=  password=";
using(SqlConnection)
{string sql="select  @@identity"
using(SqlCommand  cmd)
{
con,open()
int number =(int)cmd.ExecuteScalar();
Console.WriteLine("最近插入student 表中的id 为{0}",number);
}
}
---------------------------------------
insert  into Student output inserted.sId values()
--------------------------------------------
Cmd 对象的ExecuteReader 方法
返回值为SqlDataRader类型的值。SqlDataReader 每次read()向下走一条记录,都会将上一条记录销毁,所以我们说datareader 是只进的。用SqldataReader 只能读取数据,不能修改数据。因为查出来的结果是在一个独立的服务器中。已经与原表脱离了关系。
当我们使用DataRader,必须connection对象是Open()的状态。
 

添加web窗体
 page_load
{
if(!isPostBack)
string con="data source=;initial catalog= ,user Id=;password=";
using(SqlConnection con=new SqlConnnection(constr))
{
string sql="select * from Class";

using(SqlCommand cmd=new SqlCommand(sql,con))
{
con.open()
using(SqlDataReader reader=cmd.ExecuteReader())
{
if(reader.HasRows)
{
while(reader.read())
    {
     --弱类型 GetValue()
      Ressponse.Write( reader.GetValue(0).ToString());//第一行第一列
      Ressponse.Write( reader.GetValue(1).ToString());//第一行第二列
      Ressponse.Write( reader.GetValue(2).ToString());
     --索引
      Ressponse.Write( reader[1].ToString());//第一行第二列
       --强类型方法(要考虑Null的情况,考虑字段类型和赋值的变量类型)
    //double id=reader.GetFloat(0);数据库中的Float 比net中的float范围大,此时需要使用Double来接收
      int id=reader.GetInt32(0);
     string cName=reader.GetInt32(1);
     string cDesc=reader.GetInt32(2);
     //string cDesc=reader.IsDBNull(2)?"空":reader.GetString(2);
 Response.Write(id.ToString()+"--"+cName+"--"+cDesc)
     Response.Write("<br>")
     }
}
}
}
}
}

新建网页 web
练习2
Page_Load
{
连接数据库
 string sql="select * from Student;select * from Class"
using(SqlCommand)
{
con.open();
using(SqlDataReader reader=cmd.ExecuteReader())
{
if(reader.HasRows)
{
#region  读取一个结果集的方法
while (reader.Read())
{
//内层循环获取列
for(int i=0;i<reader.FieldCount;i++)
{
Response.Write(reader.GetValue(i));
}
Response.Write("<br>");
}
}
}
}
}
读取多个结果集的方法  do...while(先来读取第一个结果集,while中判断是否还有结果集,如果有在do中再次读取第二个结果集,依次类推)
if()
{
do
{
if(reader.HasRow)
{
while(reader.read())
{
for(int i=0;i< FieldCount;i++)
{
Response.Write(reader.GetValue(i));
}
Response.Write("<br>")
}
}
}
while(reader.NextResult());
}

SqlDataReader 的getValue,索引,以及强类型方法的区别。

reader.FieldCount 获取一行中的列数。
查询多个结果集 do..while


连接池
connection Pool

连接字符串 pooling=False
Stopwatch=new 
watch.Start();
for(lint i=0;i<2000;i++)
{using(SqlConnection=new  )
{con.open();
con.Close();
}
}
watch.Stop();
Response.Write(watch.Elapsed);
Response.Write("连接打开又关闭了");

原创粉丝点击