萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> asp.net編程 >> asp.net 數據庫連接的方法

asp.net 數據庫連接的方法

Sub LoadCommandList()
Dim objConn As New Odbc.OdbcConnection
Dim objCmd As New Odbc.OdbcCommand
Dim objDataReader As Odbc.OdbcDataReader
CommandList.Items.Clear()
objConn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};Dbq=" & Application.StartupPath & "database.mdb"
Debug.Print(Application.StartupPath & "database.mdb")
objConn.Open()'連接數據庫
objCmd.Connection = objConn
objCmd.CommandText = "select * from Commands"
objDataReader = objCmd.ExecuteReader()'執行SQL語句
While objDataReader.Read()'讀數據,以下和數據庫連接無關
CommandList.Items.Add(objDataReader.GetString(0) & "," & objDataReader.GetString(1))
End While
objConn.Close()
End Sub



Asp.net連接SQL Server2000數據庫例程詳解:
<%@ Import Namespace="System.Data" %>
<%@ Import NameSpace="System.Data.SqlClient" %>
<script laguage="VB" runat="server">
sub page_load(sender as Object,e as EventArgs)
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ds as DataSet
'1.連接數據庫
myConnection = New SqlConnection( "server=localhost;database=Pubs;uid=ueytjdf;pwd=doekdf" )
myConnection.Open()
la1.text="Connection Opened!"

'2.建立一個新表
myCommand = New SqlCommand( "CREATE TABLE [test] ([id] [int] IDENTITY (1, 1) NOT NULL ,[name] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,[sex] [char] (10) COLLATE Chinese_PRC_CI_AS NULL )", myConnection )
myCommand.ExecuteNonQuery()
la2.text="New table created!"

'2 添加紀錄
myCommand = New SqlCommand( "Insert into [test] (name,sex) values( '黃志文','男' )", myConnection )
myCommand.ExecuteNonQuery()
la3.text="New Record Inserted!"

'3 更新數據
myCommand = New SqlCommand( "UPDATE [test] SET name='Smith' where name='李明'", myConnection )
myCommand.ExecuteNonQuery()
la4.text="Record Updated!"

'4 刪除數據
myCommand = New SqlCommand( "delete from [test] where name='Smith'", myConnection )
myCommand.ExecuteNonQuery()
la5.text="Record Deleted!"

'5 用DataGrid顯示數據
myCommand = New SqlCommand( "select * from [test]", myConnection )
MyDataGrid.DataSource=myCommand.ExecuteReader()
MyDataGrid.DataBind()
end sub
</script>
<html>
<body>
<asp:label id="la1" runat="server" /><br>
<asp:label id="la2" runat="server" /><br>
<asp:label id="la3" runat="server" /><br>
<asp:label id="la4" runat="server" /><br>
<asp:label id="la5" runat="server" /><br>
<ASP:DataGrid id="MyDataGrid" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="10pt"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="#eeeeee"
>
</asp:DataGrid>

</body>
</html>
----------------------------------------------------------------------------------------------------
ASP.net連接access數據庫例程
<%@ Import Namespace="System.Data" %>
<%@ Import NameSpace="System.Data.OleDb" %>
<script laguage="VB" runat="server">
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
sub page_load(sender as Object,e as EventArgs)
'1.連接數據庫
dim dbname as string
dbname=server.mappath("authors.mdb")
myConnection = New OleDbConnection( "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source="&dbname )
myConnection.Open()
la1.text="Connection Opened!"

'2.添加記錄
myCommand = New OleDbCommand( "Insert INTO Authors(Authors,country) Values('Simson','usa')", myConnection )
myCommand.ExecuteNonQuery()
la2.text="New Record Inserted!"

'3 更新數據(Access)
myCommand = New OleDbCommand( "UPDATE Authors SET Authors='Bennett' WHERE Authors = 'Simson'", myConnection )
myCommand.ExecuteNonQuery()
la3.text="Record Updated!"

'4 刪除數據(access)
myCommand = New OleDbCommand( "DELETE FROM Authors WHERE Authors = 'David'", myConnection )
myCommand.ExecuteNonQuery()
la4.text="Record Deleted!"

'5 使用DateGrid顯示數據
myCommand = New OleDbCommand( "select * FROM Authors", myConnection )
MyDataGrid.DataSource=myCommand.Executereader()
MyDataGrid.DataBind()

end sub
</script>
<html>
<body>
<asp:label id="la1" runat="server" /><br>
<asp:label id="la2" runat="server" /><br>
<asp:label id="la3" runat="server" /><br>
<asp:label id="la4" runat="server" /><br>
<ASP:DataGrid id="MyDataGrid" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="10pt"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="#eeeeee"
>
</asp:DataGrid>

</body>
</html>
1.C#連接連接Access
 

[復制此代碼]CODE:
using System.Data;
using System.Data.OleDb;
..
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";
strConnection+=@"Data Source=C:BegASPNETNorthwind.mdb";
OleDbConnection objConnection=new OleDbConnection(strConnection);
..
objConnection.Open();
objConnection.Close();


解釋:
 連接Access數據庫需要導入額外的命名空間,所以有了最前面的兩條using命令,這是必不可少的!
 strConnection這個變量裡存放的是連接數據庫所需要的連接字符串,他指定了要使用的數據提供者和要使用的數據源.
 "Provider=Microsoft.Jet.OleDb.4.0;"是指數據提供者,這裡使用的是Microsoft Jet引擎,也就是Access中的數據引擎,asp.net就是靠這個和Access的數據庫連接的.
 "Data Source=C:BegASPNETNorthwind.mdb"是指明數據源的位置,他的標准形式是"Data Source=MyDrive:MyPathMyFile.MDB".
PS:
 1."+="後面的"@"符號是防止將後面字符串中的""解析為轉義字符.
 2.如果要連接的數據庫文件和當前文件在同一個目錄下,還可以使用如下的方法連接:
  strConnection+="Data Source=";
  strConnection+=MapPath("Northwind.mdb");
  這樣就可以省得你寫一大堆東西了!
 3.要注意連接字符串中的參數之間要用分號來分隔.
 "OleDbConnection

copyright © 萬盛學電腦網 all rights reserved