初学.net遇到的问题:数据库访问连接问题

类别:Asp 点击:0 评论:0 推荐:

错误一:运行代码时出现如下错误:
-------------------------------------------------------------------
编译器错误信息: BC30002: 未定义类型“ADOConnection”。

源错误:
行 11: <Script Language="VB" Runat="Server">
行 12: Sub Button1_Click(sender As Object, E As EventArgs)
行 13: Dim cnA As ADOConnection=New ADOConnection
行 14: Dim cmA As ADOCommand=New ADOCommand
行 15: cnA.Provider="Microsoft.Jet.OLEDB.4.0"
源文件: g:\inetpub\wwwroot\WebApplication1\WebForm3.aspx    行: 13 
-----------------------------------------------------------------------------
这是因为NameSpace出错!
使用如下:
 <%@ import Namespace="system.data" %>
<%@ import Namespace="system.data.Oledb" %>
对应变量声明为:
 dim myconn as OledbConnection
 dim mycommand as OledbCommand

 <%@ import Namespace="system.data" %>
<%@ import Namespace="system.data.SqlClient" %>
对应变量声明为:
 dim myconn as SqlConnection
 dim mycommand as SqlCommand

 <%@ import Namespace="system.data" %>
<%@ import Namespace="system.data.ODBC" %>
对应变量声明为:
 dim myconn as ODBCConnection
 dim mycommand as ODBCCommand

测试程序分别如下:
=============================
<%@ import Namespace="system.data" %>
<%@ import Namespace="system.data.Oledb"  %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <title>WebForm1</title>
 </HEAD>
 <BODY>
 <script language="vb" runat="server">
 Sub Page_load(Sender As Object, E As EventArgs)
 dim ds as dataset
 dim myconn as OledbConnection
 dim mycommand as OledbCommand
 myconn=new Oledbconnection("Provider=SQLOLEDB; Data Source=(local);uid=sa; pwd=pssword; Initial Catalog=database")
   myconn.open()
   mycommand=new Oledbcommand("select * from user_info",myconn)
   Dim myReader As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
   Datagrid1.DataSource =myreader
   Datagrid1.DataBind() 
 End Sub
 </script>
  <P>
   <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
   <FONT face="宋体">
    <asp:datagrid id="DataGrid1" runat="server"></asp:datagrid></FONT>
   <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
   <meta content="JavaScript" name="vs_defaultClientScript">
   <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  </P>
 </BODY>
</HTML>

==============================
<%@ import Namespace="system.data" %>
<%@ import Namespace="system.data.SqlClient" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <title>WebForm1</title>
 </HEAD>
 <BODY>
 <script language="vb" runat="server">
 Sub Page_load(Sender As Object, E As EventArgs)
 dim ds as dataset
 dim myconn as sqlconnection
 dim mycommand as SqlCommand
 myconn=new Sqlconnection("server=(local); uid=sa; pwd=huangfeng;database=jxjyzb")
   myconn.open()
   mycommand=new Sqlcommand("select * from user_info",myconn)
   Dim myReader As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
   Datagrid1.DataSource =myreader
   Datagrid1.DataBind()
 End Sub
 </script>
  <P>
   <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
   <FONT face="宋体">
   <asp:datagrid id="DataGrid1" runat="server"></asp:datagrid></FONT>
   <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
   <meta content="JavaScript" name="vs_defaultClientScript">
   <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  </P>
 </BODY>
</HTML>

==============================

<%@ import Namespace="system.data" %>
<%@ import Namespace="system.data.ODBC" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <title>WebForm1</title>
 </HEAD>
 <BODY>
 <script language="vb" runat="server">
 Sub Page_load(Sender As Object, E As EventArgs)
 dim ds as dataset
 dim myconn as Odbcconnection
 dim mycommand as OdbcCommand
 myconn=new ODbcconnection("DRIVER={SQL Server};uid=yourusername;pwd=yourpassword;SERVER=(local);DATABASE=yourdatabasename")
   myconn.open()
   mycommand=new ODBCcommand("select * from user_info",myconn)
   Dim myReader As ODBCDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
   Datagrid1.DataSource =myreader
   Datagrid1.DataBind()
 End Sub
 </script>
  <P>
   <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
   <FONT face="宋体">
   <asp:datagrid id="DataGrid1" runat="server"></asp:datagrid></FONT>
   <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
   <meta content="JavaScript" name="vs_defaultClientScript">
   <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  </P>
 </BODY>
</HTML>

==============================
以上程序编写各类和方法的使用可参考:
http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemdataoledboledbcommandmemberstopic.asp

注意在使用数据库连接时,应对应使用不同种的格式字符串连接:
如果您使用凭据(用户名和密码)来连接到 SQL Server 数据库,则连接字符串形式如下:

Using the SQL Server .NET Data Provider:
SqlConnection conn = new SqlConnection(
          "server=YourServer; uid=YourUserName; pwd=YourStrongPwd;" +
          "database=YourDatabase");

Using the OLE DB .NET Data Provider:
OleDbConnection conn = new OleDbConnection(
          "Provider=SQLOLEDB; Data Source=YourServer;" +
          "uid=YourUserName; pwd=YourStrongPwd; Initial Catalog=YourDatabase");

Using the ODBC .NET Data Provider:
ODBCConnection conn = new ODBCConnection(
          "DRIVER={SQL Server};SERVER=MyServer;uid=YourUserName; pwd=YourStrongPwd;DATABASE=northwind;");

如果您需要连接到安装在同一计算机上的 SQL Server 的特定实例(只在 SQL Server 2000 或以后版本中提供的功能),则连接字符串形式如下:

Using the SQL Server .NET Data Provider:
SqlConnection conn = new SqlConnection(
          "server=YourServer\Instance; uid=YourUserName; pwd=YourStrongPwd;" +
          "database=YourDatabase");

如果您利用显式凭据(用户名和密码)连接到 Oracle 数据库,则连接字符串形式如下:

OleDbConnection conn = new OleDbConnection(
          "Provider=MSDAORA; Data Source=YourDatabaseAlias;" +
          "User ID=YourUserName; Password=YourStrongPwd;");

以上我查了好多网上下载的CHM帮助文件,测试源程序,都测试出现类似错误。仔细查阅微软在线帮助,发现System.Date.Sql在新版本的.Net框架中已经不再使用了,这点值得新手注意。
数据安全访问设计详细参考如下:http://www.microsoft.com/china/msdn/library/architecture/architecture/architecturetopic/BuildSucApp/BSAAsecmod12.mspx


本文地址:http://com.8s8s.com/it/it7971.htm