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()'執(zhí)行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
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:\BegASPNET\Northwind.mdb"是指明數據源的位置,他的標準形式是"Data Source=MyDrive:MyPath\MyFile.MDB".
PS:
1."+="后面的"@"符號是防止將后面字符串中的"\"解析為轉義字符.
2.如果要連接的數據庫文件和當前文件在同一個目錄下,還可以使用如下的方法連接:
strConnection+="Data Source=";
strConnection+=MapPath("Northwind.mdb");
這樣就可以省得你寫一大堆東西了!
3.要注意連接字符串中的參數之間要用分號來分隔.
"OleDbConnection objConnection=new OleDbConnection(strConnection);"這一句是利用定義好的連接字符串來建立了一個鏈接對象,以后對數據庫的操作我們都要和這個對象打交道.
"objConnection.Open();"這用來打開連接.至此,與Access數據庫的連接完成.
2.C#連接SQL Server
using System.Data;
using System.Data.SqlClient;
..
string strConnection="user id=sa;password=;";
strConnection+="initial catalog=Northwind;Server=YourSQLServer;";
strConnection+="Connect Timeout=30";
SqlConnection objConnection=new SqlConnection(strConnection);
..
objConnection.Open();
objConnection.Close();
解釋:
連接SQL Server數據庫的機制與連接Access的機制沒有什么太大的區(qū)別,只是改變了Connection對象和連接字符串中的不同參數.
首先,連接SQL Server使用的命名空間不是"System.Data.OleDb",而是"System.Data.SqlClient".
其次就是他的連接字符串了,我們一個一個參數來介紹(注意:參數間用分號分隔):
"user id=sa":連接數據庫的驗證用戶名為sa.他還有一個別名"uid",所以這句我們還可以寫成"uid=sa".
"password=":連接數據庫的驗證密碼為空.他的別名為"pwd",所以我們可以寫為"pwd=".
這里注意,你的SQL Server必須已經設置了需要用戶名和密碼來登錄,否則不能用這樣的方式來登錄.如果你的SQL Server設置為Windows登錄,那么在這里就不需要使用"user id"和"password"這樣的方式來登錄,而需要使用"Trusted_Connection=SSPI"來進行登錄.
"initial catalog=Northwind":使用的數據源為"Northwind"這個數據庫.他的別名為"Database",本句可以寫成"Database=Northwind".
"Server=YourSQLServer":使用名為"YourSQLServer"的服務器.他的別名為"Data Source","Address","Addr".如果使用的是本地數據庫且定義了實例名,則可以寫為"Server=(local)\實例名";如果是遠程服務器,則將"(local)"替換為遠程服務器的名稱或IP地址.
"Connect Timeout=30":連接超時時間為30秒.
在這里,建立連接對象用的構造函數為:SqlConnection.
3.C#連接Oracle
using System.Data.OracleClient;
using System.Data;
//在窗體上添加一個按鈕,叫Button1,雙擊Button1,輸入以下代碼
private void Button1_Click(object sender, System.EventArgs e)
{
string ConnectionString="Data Source=sky;user=system;password=manager;";//寫連接串
OracleConnection conn=new OracleConnection(ConnectionString);//創(chuàng)建一個新連接
try
{
conn.Open();
OracleCommand cmd=conn.CreateCommand();
cmd.CommandText="select * from MyTable";//在這兒寫sql語句
OracleDataReader odr=cmd.ExecuteReader();//創(chuàng)建一個OracleDateReader對象
while(odr.Read())//讀取數據,如果odr.Read()返回為false的話,就說明到記錄集的尾部了
{
Response.Write(odr.GetOracleString(1).ToString());//輸出字段1,這個數是字段索引,具體怎么使用字段名還有待研究
}
odr.Close();
}
catch(Exception ee)
{
Response.Write(ee.Message); //如果有錯誤,輸出錯誤信息
}
finally
{
conn.Close(); //關閉連接
}
}
4.C#連接MySQL
using MySQLDriverCS;
// 建立數據庫連接
MySQLConnection DBConn;
DBConn = new MySQLConnection(new MySQLConnectionString("localhost","mysql","root","",3306).AsString);
DBConn.Open();
// 執(zhí)行查詢語句
MySQLCommand DBComm;
DBComm = new MySQLCommand("select Host,User from user",DBConn);
// 讀取數據
MySQLDataReader DBReader = DBComm.ExecuteReaderEx();
// 顯示數據
try
{
while (DBReader.Read())
{
Console.WriteLine("Host = {0} and User = {1}", DBReader.GetString(0),DBReader.GetString(1));
}
}
finally
{
DBReader.Close();
DBConn.Close();
}
//關閉數據庫連接
DBConn.Close();
5.C#連接IBM DB2
OleDbConnection1.Open();
//打開數據庫連接
OleDbDataAdapter1.Fill(dataSet1,"Address");
//將得來的數據填入dataSet
DataGrid1.DataBind();
//綁定數據
OleDbConnection1.Close();
//關閉連接
//增加數據庫數據
在Web Form上新增對應字段數量個數的TextBox,及一個button,為該按鍵增加Click響應事件代碼如下:
this.OleDbInsertCommand1.CommandText = "INSERTsintosADDRESS(NAME,
EMAIL, AGE, ADDRESS) VALUES
('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')";
OleDbInsertCommand1.Connection.Open();
//打開連接
OleDbInsertCommand1.ExecuteNonQuery();
//執(zhí)行該SQL語句
OleDbInsertCommand1.Connection.Close();
//關閉連接
6.C#連接SyBase(OleDb)