Sub LoadCommandList()
Dim objConn As New OdbcOdbcConnection
Dim objCmd As New OdbcOdbcCommand
Dim objDataReader As OdbcOdbcDataReader
CommandListItemsClear()
objConnConnectionString = "Driver={Microsoft Access Driver (*mdb)};Dbq=" & ApplicationStartupPath & "databasemdb"
DebugPrint(ApplicationStartupPath & "databasemdb")
objConnOpen()连接数据库
objCmdConnection = objConn
objCmdCommandText = "select * from Commands"
objDataReader = objCmdExecuteReader()执行SQL语句
While objDataReaderRead()读数据以下和数据库连接无关
CommandListItemsAdd(objDataReaderGetString() & "" & objDataReaderGetString())
End While
objConnClose()
End Sub
二
Aspnet连接SQL Server数据库例程详解:
<%@ Import Namespace="SystemData" %>
<%@ Import NameSpace="SystemDataSqlClient" %>
<script laguage="VB" runat="server">
sub page_load(sender as Objecte as EventArgs)
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ds as DataSet
连接数据库
myConnection = New SqlConnection( "server=localhost;database=Pubs;uid=ueytjdf;pwd=doekdf" )
myConnectionOpen()
latext="Connection Opened!"
建立一个新表
myCommand = New SqlCommand( "CREATE TABLE [test] ([id] [int] IDENTITY ( ) NOT NULL [name] [char] () COLLATE Chinese_PRC_CI_AS NULL [sex] [char] () COLLATE Chinese_PRC_CI_AS NULL )" myConnection )
myCommandExecuteNonQuery()
latext="New table created!"
添加纪录
myCommand = New SqlCommand( "Insert into [test] (namesex) values( 黄志文男 )" myConnection )
myCommandExecuteNonQuery()
latext="New Record Inserted!"
更新数据
myCommand = New SqlCommand( "UPDATE [test] SET name=Smith where name=李明" myConnection )
myCommandExecuteNonQuery()
latext="Record Updated!"
删除数据
myCommand = New SqlCommand( "delete from [test] where name=Smith" myConnection )
myCommandExecuteNonQuery()
latext="Record Deleted!"
用DataGrid显示数据
myCommand = New SqlCommand( "select * from [test]" myConnection )
MyDataGridDataSource=myCommandExecuteReader()
MyDataGridDataBind()
end sub
</script>
<html>
<body>
<asp:label id="la" runat="server" /><br>
<asp:label id="la" runat="server" /><br>
<asp:label id="la" runat="server" /><br>
<asp:label id="la" runat="server" /><br>
<asp:label id="la" runat="server" /><br>
<ASP:DataGrid id="MyDataGrid" runat="server"
BorderColor="black"
BorderWidth=""
GridLines="Both"
CellPadding=""
CellSpacing=""
FontName="Verdana"
FontSize="pt"
HeaderStyleBackColor="#aaaadd"
AlternatingItemStyleBackColor="#eeeeee"
>
</asp:DataGrid>
</body>
</html>
ASPnet连接access数据库例程
<%@ Import Namespace="SystemData" %>
<%@ Import NameSpace="SystemDataOleDb" %>
<script laguage="VB" runat="server">
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
sub page_load(sender as Objecte as EventArgs)
连接数据库
dim dbname as string
dbname=servermappath("authorsmdb")
myConnection = New OleDbConnection( "PROVIDER=MicrosoftJetOLEDB;DATA Source="&dbname )
myConnectionOpen()
latext="Connection Opened!"
添加记录
myCommand = New OleDbCommand( "Insert INTO Authors(Authorscountry) Values(Simsonusa)" myConnection )
myCommandExecuteNonQuery()
latext="New Record Inserted!"
更新数据(Access)
myCommand = New OleDbCommand( "UPDATE Authors SET Authors=Bennett WHERE Authors = Simson" myConnection )
myCommandExecuteNonQuery()
latext="Record Updated!"
删除数据(access)
myCommand = New OleDbCommand( "DELETE FROM Authors WHERE Authors = David" myConnection )
myCommandExecuteNonQuery()
latext="Record Deleted!"
使用DateGrid显示数据
myCommand = New OleDbCommand( "select * FROM Authors" myConnection )
MyDataGridDataSource=myCommandExecutereader()
MyDataGridDataBind()
end sub
</script>
<html>
<body>
<asp:label id="la" runat="server" /><br>
<asp:label id="la" runat="server" /><br>
<asp:label id="la" runat="server" /><br>
<asp:label id="la" runat="server" /><br>
<ASP:DataGrid id="MyDataGrid" runat="server"
BorderColor="black"
BorderWidth=""
GridLines="Both"
CellPadding=""
CellSpacing=""
FontName="Verdana"
FontSize="pt"
HeaderStyleBackColor="#aaaadd"
AlternatingItemStyleBackColor="#eeeeee"
>
</asp:DataGrid>
</body>
</html>
C#连接连接Access
[
复制此代码]CODE:
using System
Data;
using System
Data
OleDb;
string strConnection="Provider=Microsoft
Jet
OleDb
;";
strConnection+=@"Data Source=C:BegASPNETNorthwind
mdb";
OleDbConnection objConnection=new OleDbConnection(strConnection);
objConnection
Open();
objConnection
Close();
解释:
连接Access数据库需要导入额外的命名空间所以有了最前面的两条using命令这是必不可少的!
strConnection这个变量里存放的是连接数据库所需要的连接字符串他指定了要使用的数据提供者和要使用的数据源
"Provider=MicrosoftJetOleDb;"是指数据提供者这里使用的是Microsoft Jet引擎也就是Access中的数据引擎aspnet就是靠这个和Access的数据库连接的
"Data Source=C:BegASPNETNorthwindmdb"是指明数据源的位置他的标准形式是"Data Source=MyDrive:MyPathMyFileMDB"
PS:
"+="后面的"@"符号是防止将后面字符串中的""解析为转义字符
如果要连接的数据库文件和当前文件在同一个目录下还可以使用如下的方法连接:
strConnection+="Data Source=";
strConnection+=MapPath("Northwindmdb");
这样就可以省得你写一大堆东西了!
要注意连接字符串中的参数之间要用分号来分隔
"OleDbConnection objConnection=new OleDbConnection(strConnection);"这一句是利用定义好的连接字符串来建立了一个链接对象以后对数据库的操作我们都要和这个对象打交道
"objConnectionOpen();"这用来打开连接至此与Access数据库的连接完成
C#连接SQL Server
[
复制此代码]CODE:
using System
Data;
using System
Data
SqlClient;
string strConnection="user id=sa;password=;";
strConnection+="initial catalog=Northwind;Server=YourSQLServer;";
strConnection+="Connect Timeout=
";
SqlConnection objConnection=new SqlConnection(strConnection);
objConnection
Open();
objConnection
Close();
解释:
连接SQL Server数据库的机制与连接Access的机制没有什么太大的区别只是改变了Connection对象和连接字符串中的不同参数
首先连接SQL Server使用的命名空间不是"SystemDataOleDb"而是"SystemDataSqlClient"
其次就是他的连接字符串了我们一个一个参数来介绍(注意:参数间用分号分隔):
"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=":连接超时时间为秒
在这里建立连接对象用的构造函数为:SqlConnection
C#连接Oracle
[
复制此代码]CODE:
using System
Data
OracleClient;
using System
Data;
//在窗体上添加一个按钮
叫Button
双击Button
输入以下代码
private void Button
_Click(object sender
System
EventArgs e)
{
string ConnectionString="Data Source=sky;user=system;password=manager;";//写连接串
OracleConnection conn=new OracleConnection(ConnectionString);//创建一个新连接
try
{
conn
Open();
OracleCommand cmd=conn
CreateCommand();
cmd
CommandText="select * from MyTable";//在这儿写sql语句
OracleDataReader odr=cmd
ExecuteReader();//创建一个OracleDateReader对象
while(odr
Read())//读取数据
如果odr
Read()返回为false的话
就说明到记录集的尾部了
{
Response
Write(odr
GetOracleString(
)
ToString());//输出字段
这个数是字段索引
具体怎么使用字段名还有待研究
}
odr
Close();
}
catch(Exception ee)
{
Response
Write(ee
Message); //如果有错误
输出错误信息
}
finally
{
conn
Close(); //关闭连接
}
}
C#连接MySQL
[
复制此代码]CODE:
using MySQLDriverCS;
// 建立数据库连接
MySQLConnection DBConn;
DBConn = new MySQLConnection(new MySQLConnectionString("localhost"
"mysql"
"root"
""
)
AsString);
DBConn
Open();
// 执行查询语句
MySQLCommand DBComm;
DBComm = new MySQLCommand("select Host
User from user"
DBConn);
// 读取数据
MySQLDataReader DBReader = DBComm
ExecuteReaderEx();
// 显示数据
try
{
while (DBReader
Read())
{
Console
WriteLine("Host = {
} and User = {
}"
DBReader
GetString(
)
DBReader
GetString(
));
}
}
finally
{
DBReader
Close();
DBConn
Close();
}
//关闭数据库连接
DBConn
Close();
C#连接IBM DB
[
复制此代码]CODE:
OleDbConnection
Open();
//打开数据库连接
OleDbDataAdapter
Fill(dataSet
"Address");
//将得来的数据填入dataSet
DataGrid
DataBind();
//绑定数据
OleDbConnection
Close();
//关闭连接
//增加数据库数据
在Web Form上新增对应字段数量个数的TextBox及一个button为该按键增加Click响应事件代码如下
[
复制此代码]CODE:
this
OleDbInsertCommand
CommandText = "INSERTsintosADDRESS(NAME
EMAIL
AGE
ADDRESS) VALUES
(
"+TextBox
Text+"
"+TextBox
Text+"
"+TextBox
Text+"
"+TextBox
Text+"
)";
OleDbInsertCommand
Connection
Open();
//打开连接
OleDbInsertCommand
ExecuteNonQuery();
//执行该SQL语句
OleDbInsertCommand
Connection
Close();
//关闭连接
C#连接SyBase
(OleDb)
[
复制此代码]CODE:
Provider=Sybase
ASEOLEDBProvider
;Initial Catalog=数据库名;User ID=用户名;Data Source=数据源;Extended Properties="";Server Name=ip地址;Network Protocol=Winsock;Server Port Address=