string strConn = @"server=localhost;database=数据库名;trusted_connection=true"; SqlConnection conn = new SqlConnection(strConn); String strSQL = "select count(*) from Table"; SqlCommand com = new SqlCommand(strSQL,strSQL ); SqlDataReader dr = com.ExecuteReader(); if (dr.Read()) { lable1.Text=dr[0].ToString(); } dr.Close();
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conStr"]); conn.Open(); string sql = "select count(ID) from Table where time between '"+TextBox1.text+"'and '"+TextBox2.text + "'"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteScalar() Label.Text = Convert.ToInt32(cmd.ExecuteScalar()).ToString();
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conStr"]); conn.Open(); string sql = "select count(ID) from Table where time between '"+TextBox1.text+"'and '"+TextBox2.text + "'"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteScalar() Label.Text = Convert.ToInt32(cmd.ExecuteScalar()).ToString();
asp.net夜话之七:ADO.NET介绍 ADO.NET是对Microsoft ActiveX Data Objects (ADO)一个跨时代的改进,它提供了平台互用性和可伸缩的数据访问。由于传送的数据都是XML格式的,因此任何能够读取XML格式的应用程序都可以进行数据处理。事实上,接受数据的组件不一定要是ADO .NET组件,它可以是基于一个Microsoft Visual Studio的解决方案,也可以是任何运行在其它平台上的任何应用程序。以前做数据库访问的时候,需要一直与数据库保持连接,直到获取完所有满足需要的数据之后才会断开数据库连接,这种数据库访问方式称之为连接式数据访问技术。相比于以前的连接式数据访问技术,ADO.NET除了提供连接式数据访问技术之外,还提供了另一种断开式解决方案,那就是在内存中模拟一个数据库,也就是内存中的数据库。我们知道在实际的数据库技术中,每个数据库就是一个业务逻辑单元,一般来说这个数据库包含了实现一个应用软件或者一个网站所需要的全部数据。 本篇中还讲述了自定义分页在数据库层的理论。
Command对象 Command对象也称为数据库命令对象,Command对象主要执行包括添加、删除、修改及查询数据的操作的命令。也可以用来执行存储过程。用于执行存储过程时需要将Command对象的CommandType 属性设置为CommandType.StoredProcedure,默认情况下CommandType 属性为CommandType.Text,表示执行的是普通SQL语句。 Command主要有三个方法: ExecuteNonQuery () :执行一个SQL语句,返回受影响的行数,这个方法主要用于执行对数据库执行增加、更新、删除操作,注意查询的时候不是调用这个方法。 ExecuteReader ():执行一个查询的SQL语句,返回一个DataReader对象。 ExecuteScalar ():从数据库检索单个值。这个方法主要用于统计操作。//实例化Connection对象 SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa"); //实例化Command对象 SqlCommand command = new SqlCommand("select count(1) as 男性人数 from UserInfo where sex=1", connection); //打开Connection对象 connection.Open(); //执行SQL语句 int count = int.Parse(command.ExecuteScalar().ToString()); //关闭Connection对象 connection.Close();
SqlConnection conn = new SqlConnection(strConn);
String strSQL = "select count(*) from Table";
SqlCommand com = new SqlCommand(strSQL,strSQL );
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
lable1.Text=dr[0].ToString();
}
dr.Close();
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
conn.Open();
string sql = "select count(ID) from Table where time between '"+TextBox1.text+"'and '"+TextBox2.text + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteScalar()
Label.Text = Convert.ToInt32(cmd.ExecuteScalar()).ToString();
conn.Open();
string sql = "select count(ID) from Table where time between '"+TextBox1.text+"'and '"+TextBox2.text + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteScalar()
Label.Text = Convert.ToInt32(cmd.ExecuteScalar()).ToString();
ADO.NET是对Microsoft ActiveX Data Objects (ADO)一个跨时代的改进,它提供了平台互用性和可伸缩的数据访问。由于传送的数据都是XML格式的,因此任何能够读取XML格式的应用程序都可以进行数据处理。事实上,接受数据的组件不一定要是ADO .NET组件,它可以是基于一个Microsoft Visual Studio的解决方案,也可以是任何运行在其它平台上的任何应用程序。以前做数据库访问的时候,需要一直与数据库保持连接,直到获取完所有满足需要的数据之后才会断开数据库连接,这种数据库访问方式称之为连接式数据访问技术。相比于以前的连接式数据访问技术,ADO.NET除了提供连接式数据访问技术之外,还提供了另一种断开式解决方案,那就是在内存中模拟一个数据库,也就是内存中的数据库。我们知道在实际的数据库技术中,每个数据库就是一个业务逻辑单元,一般来说这个数据库包含了实现一个应用软件或者一个网站所需要的全部数据。
本篇中还讲述了自定义分页在数据库层的理论。
Command对象也称为数据库命令对象,Command对象主要执行包括添加、删除、修改及查询数据的操作的命令。也可以用来执行存储过程。用于执行存储过程时需要将Command对象的CommandType 属性设置为CommandType.StoredProcedure,默认情况下CommandType 属性为CommandType.Text,表示执行的是普通SQL语句。
Command主要有三个方法:
ExecuteNonQuery () :执行一个SQL语句,返回受影响的行数,这个方法主要用于执行对数据库执行增加、更新、删除操作,注意查询的时候不是调用这个方法。
ExecuteReader ():执行一个查询的SQL语句,返回一个DataReader对象。
ExecuteScalar ():从数据库检索单个值。这个方法主要用于统计操作。//实例化Connection对象
SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa");
//实例化Command对象
SqlCommand command = new SqlCommand("select count(1) as 男性人数 from UserInfo where sex=1", connection);
//打开Connection对象
connection.Open();
//执行SQL语句
int count = int.Parse(command.ExecuteScalar().ToString());
//关闭Connection对象
connection.Close();
咋回事儿啊?