ASP.Net中命名空间Namespace浅析和使用例子
关于Namespace(命名空间)的使用,我常用
<%@ImportNamespace="System.Data"%>
,这是在引用为我们提供的Namespace,这和ASP不同的,我们在ASP.net必须先引用与我们操作有关的Namespace后才能使用相应的功能。其实说白了,一个Namespace;就是一个组件。
这个是关于ASP.net的高级应用。
我下面简单的列举一些常用的Namespace
<%@ImportNamespace="System.Data"%>处理数据时用到 <%@ImportNamespace="System.Data.ADO"%>使用ADO.net;时用到 <%@ImportNamespace="System.Data.SQL"%>SQLServer数据库专用 <%@ImportNamespace="System.Data.XML"%>不用看处理XML用到 <%@ImportNamespace="System.IO"%>处理文件时用到 <%@ImportNamespace="System.Web.Util"%>发邮件时大家会用到 <%@ImportNamespace="System.Text"%>文本编码时用到
操作数据库需要的东东
讲解了Namespace,我们可以正式来讨论数据库的应用了。
从上面的可以看出,我们操作数据库,我们需要引用下面两个Namespace
<%@ImportNamespace="System.Data"%> <%@ImportNamespace="System.Data.SQL"%>
其实System.Data.SQL可以用System.Data.ADO来代替,SQL是SQLServer专用,ADO可以支持任何数据库(只要在主机上存在相应的驱动就行了,如Access,Mysql,Oracle之类的)。
无论是ADO还是SQL,他们都有几个基本的对象用于操作
Connections连结到一个数据库,以便于后面的应用(类似ADO中的Connections) Commands执行SQL语句的地方 DataReader读取执行后返回的数据内容 DataSet储存数据,功能强大,我们会具体讲解 DataSetCommand执行SQL语句,并把数据存入DataSet
这里面可能最难理解的就是DataSet,我们先不去管他。
Connections(SQLConection或者ADOConnection)
它的主要任务就是建立一个与数据库服务器的连接
<%@PageLanguage="C#"%> <%@ImportNamespace="System.Data"%> <%@ImportNamespace="System.Data.SQL"%> <scriptLanguage="C#"Runat="Server"> publicvoidPage_Load(Objectsrc,EventArgse) { stringstrProvider="server=localhost;uid=sa;pwd=;database=aspcn"; SQLConnectionMyConnection=newSQLConnection(strProvider); } </script>
上面我们建立了一个名为MyConnection的联结,就好像我们在ASP中用ADODB.Connection打开了一个联结,这个联结我们在Command或者DataSetCommand中将会使用。
它的一些有用的属性和方法有
ConnectionString 取得或设置连结数据库的语句 ConnectionTimeout 取得或设置连结数据库的最长时间,也是就超时时间 DataBase 取得或设置在数据库服务器上要打开的数据库名 DataSource 取得或设置DSN,大家不会陌生吧:) Password 取得或设置密码 UserID 取得或设置登陆名 State 取得目前联结的状态 Open() 打开联结 Close() 关闭联结 Clone() 克隆一个联结。
我们也通过一个小例子来看看他们的用法:
SQLConnectionmyConnection=newSQLConnection(); myConnection.DataSource="mySQLServer"; myConnection.Password=""; myConnection.UserID="sa"; myConnection.ConnectionTimeout=30; myConnection.Open(); myConnection.Database="northwind"; myConnection.IsolationLevel=IsolationLevel.ReadCommitted Commands(SQLCommand或者ADOCommand)
上面的程序中我们打开了一个联结,这里我们就需要来使用这个,看例子比较好:
<%@PageLanguage="C#"%> <%@ImportNamespace="System.Data"%> <%@ImportNamespace="System.Data.SQL"%> <scriptLanguage="C#"Runat="Server"> publicvoidPage_Load(Objectsrc,EventArgse) { stringstrProvider="server=localhost;uid=sa;pwd=;database=aspcn"; stringstrIndex="select*fromaspcnwherepurview='webmaster'"; SQLConnectionMyConnection=newSQLConnection(strProvider); SQLCommandMyCommand=newSQLCommand(strIndex,MyConnection); MyConnection.Open();//打开联结 MyCommand.ExecuteNonQuery();//执行SQL,但不返回任何记录 MyConnection.Close(); } </script>
在上面的例子中我们建立SQLCommand对象时引用了两个参数(strIndex,MyConnection),从源程序中我们也可以看出来strIndex代表的是执行的SQL语句,MyConnection是我们先前建立的联结.然后我们就要先打开MyConnnection,然后再执行这个SQL语句。
我们在这里执行用的是ExecuteNonQuery()方法,这样不返回记录集,只是返回受影响的记录个数。
这里我们打开和关闭数据库也可以这样做。
stringstrProvider="server=localhost;uid=sa;pwd=;database=aspcn"; stringstrIndex="select*fromaspcnwherepurview='webmaster'"; SQLConnectionMyConnection=newSQLConnection(strProvider); SQLCommandMyCommand=newSQLCommand(strIndex,MyConnection); MyCommand.ActiveConnection.Open(); MyCommand.ExecuteNonQuery(); MyCommand.ActiveConnection.Close();
所得结果和先前的一样。所以执行一条SQL语句有很多种方法。而且还不只两种,我们后面学了DataSetCommand,那打开方法就是N种了:)这就需要看你的习惯和程序的要求了;)
我们先来看看Command常用的方法和属性
ActiveConnection 取得或设置联结Connections CommandText 执行的SQL语句或储存过程(StoredProcedure)名 CommandTimeout 执行的最长时间 CommandType Command操作的类型(StoredProcedure,Text,TableDirect)三种,默认Text Parameters 操作储存过程时使用 Execute() 执行SQL语句或储存过程 ExecuteNonQuery() 同上,区别在于不返回记录集 Clone() 克隆Command
同样看一个例子:
stringmySelectQuery="SELECT*FROMCategoriesORDERBYCategoryID"; stringmyConnectString="userid=sa;password=;database=northwind;server=mySQLServer"; SQLCommandmyCommand=newSQLCommand(mySelectQuery); myCommand.ActiveConnection=newSQLConnection(myConnectString); myCommand.CommandTimeout=15; myCommand.CommandType=CommandType.Text;