好多ASP爱好者可能很怀念ADO,虽然大多已经迁移到ASP.NET了,可是我感觉ADO的对象模型设计的也很好,现在演示一下在ASP里如何操作ADO,如果是在ASP.NET里使用ADO的话记着在页面头里设置如下语句
<%@ Page AspCompat="TRUE"%>
using System;
namespace ConsoleApplication1
{
/// <summary>
/// Class2 的摘要说明。
/// </summary>
public class Class2
{
private static ADODB.Connection cn;
private static ADODB.Recordset rs;
[STAThread]
public static void Main()
{
String strConn = "Provider=SQLOLEDB;SERVER=onlytiancai;UID=sa;PWD=sa;DATABASE=pubs;";
cn = new ADODB.Connection();
cn.ConnectionString=strConn;
//打开数据库
try
{
cn.Open(strConn,"","",-1); //如果需要修改用户和密码在这里修改
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
//打开记录集
rs= new ADODB.Recordset();
rs.Open("select * from titles",cn,ADODB.CursorTypeEnum.adOpenKeyset,ADODB.LockTypeEnum.adLockOptimistic, (int)ADODB.CommandTypeEnum.adCmdText);
//如果记录集为空,输出一个错误信息
if (rs.BOF || rs.EOF)
{
Console.WriteLine("没有找到任何记录,请检查你的sqlserver的表");
}
//循环输出title
Console.WriteLine("原始记录集");
while(!rs.EOF)
{
Console.WriteLine(rs.Fields["Title"].Value.ToString());
rs.MoveNext();
}
//把记录集的第一条修改一下
rs.MoveFirst();
rs.Update("Title", "Inside DirectX");
rs.Update("Type", "Business");
rs.Update("ytd_sales", 1705);
rs.UpdateBatch(ADODB.AffectEnum.adAffectCurrent);
//在记录集的最后添加一条新记录
rs.MoveLast();
object[] objOne = new object[6];
objOne[0]="title_id";
objOne[1]="title";
objOne[2]="type";
objOne[3] = "price";
objOne[4]="ytd_sales";
objOne[5]="pubdate";
object[] objTwo = new object[6];
objTwo[0]="TZ9999";
objTwo[1]="Programming Outlook and Exchange";
objTwo[2]="beginners";
objTwo[3]=19.25;
objTwo[4]=9999;
object dt;
dt = DateTime.Now;
objTwo[5]=dt;
rs.AddNew(objOne,objTwo);
//再把记录集滚动到最前面,重新输出记录集
rs.MoveFirst();
Console.WriteLine("更改了第一行,又添加了一个新行后的记录集");
while(!rs.EOF)
{
Console.WriteLine(rs.Fields["Title"].Value.ToString());
rs.MoveNext();
}
//最后把记录集还原成原始状态
rs.MoveFirst();
rs.Update("title","But is It User Friendly?");
rs.Update("Type","popular_comp");
rs.Update("ytd_sales","8790");
rs.UpdateBatch(ADODB.AffectEnum.adAffectCurrent);
rs.MoveFirst();
object dummy;
cn.Execute(" delete from titles where title like 'Programming Outlook and Exchange'",out dummy, -1);
Console.WriteLine(dummy);
rs.Close();
cn.Close();
//为了在调试的时候窗口不会一闪而过,做下面的处理
Console.ReadLine();
}
}
}
namespace ConsoleApplication1
{
/// <summary>
/// Class2 的摘要说明。
/// </summary>
public class Class2
{
private static ADODB.Connection cn;
private static ADODB.Recordset rs;
[STAThread]
public static void Main()
{
String strConn = "Provider=SQLOLEDB;SERVER=onlytiancai;UID=sa;PWD=sa;DATABASE=pubs;";
cn = new ADODB.Connection();
cn.ConnectionString=strConn;
//打开数据库
try
{
cn.Open(strConn,"","",-1); //如果需要修改用户和密码在这里修改
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
//打开记录集
rs= new ADODB.Recordset();
rs.Open("select * from titles",cn,ADODB.CursorTypeEnum.adOpenKeyset,ADODB.LockTypeEnum.adLockOptimistic, (int)ADODB.CommandTypeEnum.adCmdText);
//如果记录集为空,输出一个错误信息
if (rs.BOF || rs.EOF)
{
Console.WriteLine("没有找到任何记录,请检查你的sqlserver的表");
}
//循环输出title
Console.WriteLine("原始记录集");
while(!rs.EOF)
{
Console.WriteLine(rs.Fields["Title"].Value.ToString());
rs.MoveNext();
}
//把记录集的第一条修改一下
rs.MoveFirst();
rs.Update("Title", "Inside DirectX");
rs.Update("Type", "Business");
rs.Update("ytd_sales", 1705);
rs.UpdateBatch(ADODB.AffectEnum.adAffectCurrent);
//在记录集的最后添加一条新记录
rs.MoveLast();
object[] objOne = new object[6];
objOne[0]="title_id";
objOne[1]="title";
objOne[2]="type";
objOne[3] = "price";
objOne[4]="ytd_sales";
objOne[5]="pubdate";
object[] objTwo = new object[6];
objTwo[0]="TZ9999";
objTwo[1]="Programming Outlook and Exchange";
objTwo[2]="beginners";
objTwo[3]=19.25;
objTwo[4]=9999;
object dt;
dt = DateTime.Now;
objTwo[5]=dt;
rs.AddNew(objOne,objTwo);
//再把记录集滚动到最前面,重新输出记录集
rs.MoveFirst();
Console.WriteLine("更改了第一行,又添加了一个新行后的记录集");
while(!rs.EOF)
{
Console.WriteLine(rs.Fields["Title"].Value.ToString());
rs.MoveNext();
}
//最后把记录集还原成原始状态
rs.MoveFirst();
rs.Update("title","But is It User Friendly?");
rs.Update("Type","popular_comp");
rs.Update("ytd_sales","8790");
rs.UpdateBatch(ADODB.AffectEnum.adAffectCurrent);
rs.MoveFirst();
object dummy;
cn.Execute(" delete from titles where title like 'Programming Outlook and Exchange'",out dummy, -1);
Console.WriteLine(dummy);
rs.Close();
cn.Close();
//为了在调试的时候窗口不会一闪而过,做下面的处理
Console.ReadLine();
}
}
}
这是一个简单的演示哦,注意调用COM组件的时候设置对线程模型哦,我感觉这样使用ADO性能上不会比ADO.NET慢多少的,谁有兴趣可以测试一下哦.