asp.net 中 使用 ado 的 Recordset GetString 函数 By shawl.qiu
keyword: using ado in .net说明:
这个问题本来不想写个博的, 不过有鉴于这个功能非常不错, 也确实费了我一两小时, 就总结一下啰.
Ado GetString 这个方法在使用 ASP 时屡用不爽呀,
不过跑到 Ado.net, 居然发现没这个功能...
到论坛发帖也请教不出个所以然, 最后放狗搜, 发现N多老外也很奇怪没这个功能, 那些 MVP 的解释是: ado.net 不是 ado, 因此某些方法没有不足为奇...
最后还是记得某时看 MSDN 时, 知道可以使用 .NET 调用 COM 方法,
然后就自然的使用 COM 方法解决了.
目录:
1. 编译 com
2. 在 .aspx 页中使用(至于编译引用什么的, 自己看着办)
3. 补充一下, web.config 内容, 请按需要修改
shawl.qiu
2007-03-04
http://blog.csdn.net/btbtd
内容:
1. 编译 com
以下操作按你自己的具体路径:
从命令行切换到目录:
H:/WINDOWS/Microsoft.NET/Framework/v1.1.4322/Bin
编译:
TlbImp "H:/Program Files/Common Files/System/ado/msado15.dll" /out:ADODB.dll
... 晕, bin 知道是什么吧, 那就拷贝呀...
2. 在 .aspx 页中使用(至于编译引用什么的, 自己看着办):
- <%@ Page Language="C#" AutoEventWireup="True" %>
- <%@ Assembly Name="ADODB" %><%@ import Namespace="ADODB" %>
- <script runat="server">
- string SqlCnnOleDb = ConfigurationSettings.AppSettings["SqlCnnOleDb"]+"";
- string SqlQryStr = "select * from allCat order by cat1id, cat2id, cat3id asc";
- void Page_Load(Object s, EventArgs e)
- {
- HttpContext.Current.Response.Write
- (
- Adodb.GetStringRs(SqlQryStr, SqlCnnOleDb, -1, "##", "##@", "null")
- );
- } // end Page_Load
- /*-----------------------------------------------------------------------------------*/
- * shawl.qiu c# Adodb class v1.0
- /*-----------------------------------------------------------------------------------*/
- //---------------------------------------------------------------------begin class Adodb
- public class Adodb
- {
- //-----------------------------------begin event
- public Adodb()
- {
- }
- ~Adodb()
- {
- }
- //-----------------------------------end event
- //-----------------------------------begin public constant
- //-----------------------begin about
- public const String auSubject = "shawl.qiu c# Adodb class";
- public const String auVersion = "v1.0";
- public const String au = "shawl.qiu";
- public const String auEmail = "shawl.qiu@gmail.com";
- public const String auBlog = "http://blog.csdn.net/btbtd";
- public const String auCreateDate = "2007-3-3";
- //-----------------------end about
- //-----------------------------------end public constant
- //-----------------------------------begin private constant
- //-----------------------------------end private constant
- //-----------------------------------begin public static method
- public static string GetStringRs
- (
- CursorTypeEnum ctEnum,
- LockTypeEnum enumLt,
- string SqlQryStr,
- string SqlCnnOleDb,
- int NumRows,
- string ColumnDelimeter,
- string RowDelimeter,
- string NullExpr
- )
- {
- string result = "";
- Recordset rs = new Recordset();
- rs.Open
- (
- SqlQryStr,
- SqlCnnOleDb,
- ctEnum,
- enumLt,
- -1
- );
- result =
- rs.GetString
- (
- StringFormatEnum.adClipString,
- NumRows,
- ColumnDelimeter,
- RowDelimeter,
- NullExpr
- );
- rs.Close();
- rs=null;
- return result;
- } // end public static string GetStringRs
- public static string GetStringRs
- (
- string SqlQryStr,
- string SqlCnnOleDb,
- int NumRows,
- string ColumnDelimeter,
- string RowDelimeter,
- string NullExpr
- )
- {
- return
- GetStringRs
- (
- CursorTypeEnum.adOpenKeyset,
- LockTypeEnum.adLockOptimistic,
- SqlQryStr,
- SqlCnnOleDb,
- NumRows,
- ColumnDelimeter,
- RowDelimeter,
- NullExpr
- );
- } // end public static string GetStringRs
- //-----------------------------------end public static method
- }
- //---------------------------------------------------------------------end class Adodb
- </script>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>shawl.qiu template</title>
- </head>
- <body>
- <form runat="server">
- </form>
- </body>
- </html>
3. 补充一下, web.config 内容, 请按需要修改:
- <?xml version="1.0" encoding="UTF-8" ?>
- <configuration>
- <appSettings>
- <!--<add key="sq" value="just a test" />-->
- <!--Response.Write(ConfigurationSettings.AppSettings["sq"]+""); -->
- <add key="SqlCnn" value="
- data source = 127.0.0.1;
- user id = sqCsAtSysUser;
- password = sqCsAtSysUserPwd;
- persist security info = false;
- initial catalog = sqCsAtSys
- " />
- <!-- string SqlCnn = ConfigurationSettings.AppSettings["SqlCnn"]+""; -->
- <add key="SqlCnnOleDb" value="
- provider=sqloledb;
- data source=127.0.0.1;
- user id=sqCsAtSysUser;
- password=sqCsAtSysUserPwd;
- persist security info=false;
- initial catalog=sqCsAtSys
- " />
- <!-- string SqlCnnOleDb = ConfigurationSettings.AppSettings["SqlCnnOleDb"]+""; -->
- </appSettings>
- <system.web>
- <httpRuntime maxRequestLength="20480"
- useFullyQualifiedRedirectUrl="true"
- executionTimeout="90"
- />
- <customErrors defaultRedirect="/include/error/generalError.html"
- mode="RemoteOnly" >
- <error statusCode="404" redirect="/include/error/error404.html" />
- </customErrors>
- <globalization
- requestEncoding="UTF-8"
- responseEncoding="UTF-8"
- fileEncoding="UTF-8"
- />
- <!-- <sessionState mode="InProc"
- cookieless="false"
- timeout="20" >
- </sessionState>-->
- <sessionState mode="SQLServer"
- cookieless="true"
- timeout="20"
- sqlConnectionString="data source=127.0.0.1;user id=sqCsAtSysUser;password=sqCsAtSysUserPwd"
- >
- </sessionState>
- <compilation defaultLanguage="c#"
- debug="true"
- numRecompilesBeforeAppRestart="15">
- <assemblies>
- <add assembly="ADODB"/>
- </assemblies>
- </compilation>
- </system.web>
- </configuration>