200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > mysql数据库有几种连接方法_几种常见的数据库连接方法

mysql数据库有几种连接方法_几种常见的数据库连接方法

时间:2019-02-07 02:40:51

相关推荐

mysql数据库有几种连接方法_几种常见的数据库连接方法

一、连接Access数据库

1.使用已有DSN的连接字符串进行连接(ODBC)

//导入命名空间

usingSystem.Data.Odbc;

protectedvoidPage_Load(Objectsender,EventArgse)

{

//设置连接字符串

Stringconnstr=@"DSN=sample";

//实例化Connection对象

OdbcConnectionmyConnection=newOdbcConnection(connstr);

//执行Open方法打开连接

myConnection.Open();

//执行SQL语句

OdbcCommandmyCommand=newOdbcCommand("select*fromsampletable",myConnection);

//将查询的结果赋给GridView的数据源

gv.DataSource=myCommand.ExecuteReader();

//绑定GridView

gv.DataBind();

//关闭连接

myConnection.Close();

}

2.使用无DSN的连接字符串进行连接(ODBC)

//导入命名空间

usingSystem.Data.Odbc;

protectedvoidPage_Load(Objectsender,EventArgse)

{

//设置连接字符串

Stringconnstr=@"Driver=MicrosoftAccessDriver(*.mdb);Dbq=c:\sample.mdb;";

//实例化Connection对象

OdbcConnectionmyConnection=newOdbcConnection(connstr);

//执行Open方法打开连接

myConnection.Open();

//执行SQL语句

OdbcCommandmyCommand=newOdbcCommand("select*fromsampletable",myConnection);

//将查询的结果赋给GridView的数据源

gv.DataSource=myCommand.ExecuteReader();

//绑定GridView

gv.DataBind();

//关闭连接

myConnection.Close();

}

3.使用连接字符串进行连接(OLEDB)

Data Provider 支持的OLEDB Provider:

SQLOLEDB:用来访问SQL Server数据库

MSDAORA:用来访问Oracle数据库

Microsoft.Jet.OLEDB.4.0:用来访问Access数据库。

//导入命名空间

usingSystem.Data.OleDb;

protectedvoidPage_Load(Objectsender,EventArgse)

{

//设置连接字符串

Stringconnstr=@"Provider=Microsoft.Jet.OleDb.4.0;DataSource=c:\sample.mdb;";

//实例化OleDbConnection对象

OleDbConnectionmyConnection=newOleDbConnection(connstr);

//执行Open方法打开连接

myConnection.Open();

//执行SQL语句

OleDbCommandmyCommand=newOleDbCommand("select*fromsampletable",myConnection);

//将查询的结果赋给GridView的数据源

gv.DataSource=myCommand.ExecuteReader();

//绑定GridView

gv.DataBind();

//关闭连接

myConnection.Close();

}

4.使用UDL文件进行连接

使用UDL文件连接数据源的步骤如下:

(1)新建一个记事本,其扩展名为.udl。

(2)双击该UDL文件,弹出“数据连接属性”对话框。

(3)该对话框首页显示“提供程序”选项卡,选择要使用的OLEDB提供程序。

(4)单击“下一步”,显示"l连接“选项卡”,设置好正确的参数后,单击“测试连接”。

使用连接字符串

//导入命名空间

usingSystem.Data.OleDb;

protectedvoidPage_Load(Objectsender,EventArgse)

{

//设置连接字符串

Stringconnstr=@"FILENAME=c:\oledb.udl";

//实例化OleDbConnection对象

OleDbConnectionmyConnection=newOleDbConnection(connstr);

//执行Open方法打开连接

myConnection.Open();

//执行SQL语句

OleDbCommandmyCommand=newOleDbCommand("select*fromsampletable",myConnection);

//将查询的结果赋给GridView的数据源

gv.DataSource=myCommand.ExecuteReader();

//绑定GridView

gv.DataBind();

//关闭连接

myConnection.Close();

}

二、连接MySQL数据库 1.使用已有DSN的连接字符串进行连接

//导入命名空间

usingSystem.Data.Odbc;

protectedvoidPage_Load(Objectsender,EventArgse)

{

//设置连接字符串

Stringconnstr=@"DSN=MySQL";

//实例化Connection对象

OdbcConnectionmyConnection=newOdbcConnection(connstr);

//执行Open方法打开连接

myConnection.Open();

//执行SQL语句

OdbcCommandmyCommand=newOdbcCommand("select*fromNames",myConnection);

//将查询的结果赋给GridView的数据源

gv.DataSource=myCommand.ExecuteReader();

//绑定GridView

gv.DataBind();

//关闭连接

myConnection.Close();

}

2.使用无DSN的连接字符串进行连接

//导入命名空间

usingSystem.Data.Odbc;

protectedvoidPage_Load(Objectsender,EventArgse)

{

//设置连接字符串

Stringconnstr=@"Driver=MySQLODBC3.51Driver;Server=localhost;Database=test;UID=root;PWD=yourpassword;Option=3;Port=3306";

//实例化Connection对象

OdbcConnectionmyConnection=newOdbcConnection(connstr);

//执行Open方法打开连接

myConnection.Open();

//执行SQL语句

OdbcCommandmyCommand=newOdbcCommand("select*fromNames",myConnection);

//将查询的结果赋给GridView的数据源

gv.DataSource=myCommand.ExecuteReader();

//绑定GridView

gv.DataBind();

//关闭连接

myConnection.Close();

}

三、连接Oracle数据库

1.使用 Data Provider(需要安装Oracle客户端)

//导入命名空间

usingSystem.Data.OracleClient;

publicvoidPage_Load(Objectsender,EventArgse)

{

//设置连接字符串

stringconnstring=@"DataSource=oraclesample;UserID=oracleid;Password=oraclepwd;";

//实例化OracleConnection对象

OracleConnectionconn=newOracleConnection(connstring);

//打开连接

connn.Open();

}

2.使用 Data Provider

//导入命名空间

usingSystem.Data.Odbc;

publicvoidPage_Load(Objectsender,EventArgse)

{

//设置连接字符串

stringconnstring=@"Driver=MicrosoftODBCforOracle;Server=oraclesample;PersisitSecurityInfo=False;Trusted_Connection=yes;";

//实例化OracleConnection对象

OdbcConnectionconn=newOdbcConnection(connstring);

//打开连接

connn.Open();

}

3.使用OLE Data Provider

//导入命名空间

usingSystem.Data.Oledb;

publicvoidPage_Load(Objectsender,EventArgse)

{

//设置连接字符串

stringconnstring=@"Provider=MSDAORA;DataSource=oraclesample;PersisitSecurityInfo=False;IntegratedSecurity=yes;";

//实例化OracleConnection对象

OleDbConnectionconn=newOleDbConnection(connstring);

//打开连接

connn.Open();

}

四、访问Excel 1.使用 Data Provider访问Excel

usingSystem.Data.Odbc;

protectedvoidPage_Load(Objectsender,EventArgse)

{

//设置连接字符串

stringconnstr=@"Driver=MicrosoftExcelDriver(*.xls);Dbq=c:\excelsample.xls;";

//实例化OdbcConnection对象

OdbcConnectionmyConnection=newOdbcConnection(connstr);

//执行Open方法打开连接

myConnection.Open();

//执行SQL语句

OdbcCommandmyCommand=newOdbcCommand("select*from[Sheet1$]",myConnection);

//用GridView来显示数据

gv.DataSource=myCommand.ExecuteReader();

gv.DataBind();

//调用Close方法关闭连接

myConnection.Close();

}

注:ConnectionString属性为Driver(驱动器名),Dbq ( 访问Excel时使用的SQL语句与访问数据库时使用的语句奏本相同,只是from后面的表名的写法不同,如"select * from [Sheet1$],表示访问的是Shee表,若要访问Sheet2,Sheet3,替换SQL语句中的Sheetl即可。

2.使用OLE Data Provider访问Excel

usingSystem.Data.OleDb;

protectedvoidPage_Load(Objectsender,EventArgse)

{

//设置连接字符串

stringconnstr=@"Provider=Microsoft.Jet.OleDb.4.0;DataSource=c:\excelsample.xls;ExtenedProperties=Excel8.0;";

//实例化OdbcConnection对象

OleDbConnectionmyConnection=newOleDbConnection(connstr);

//执行Open方法打开连接

myConnection.Open();

//执行SQL语句

OleDbCommandmyCommand=newOleDbCommand("select*from[Items$]",myConnection);

//用GridView来显示数据

gv.DataSource=myCommand.ExecuteReader();

gv.DataBind();

//调用Close方法关闭连接

myConnection.Close();

}

注:Conn}ctionString属性为Provider(提供程序名),Data Source(Excel文家爱女实际路径名),Extended Properties(附加属性)。其中,Extended Properties制定一些附加的属性,如Excel的版本(本例为Excel 8.0)和HDR值。HDR=Yes表示表格的第一行为标题,应用程序使用SQL语句查询时不会选择第一行的内容;HDR=No则表示应用程序会把表格中所选的全部内容(包括第一行)查询出来。

五、访问Txt文件 1.使用 Data Provider

stringconnstr=@"Driver=MicrosoftTextDriver(*.txt;*.csv);Dbq=c:\samplepath\;Extensions=asc,csv,tab,txt;";

OdbcConnectionmyConnection=newOdbcConnection(connstr);

OdbcCommandmyCommand=newOdbcCommand("select*fromtxtsample.txt",myConnection);

2.使用OLE Data Provider

stringconnstr=@"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\samplepath\;ExtendedProperties=**text;HDR=Yes;FMT=Delimited""";

OleDbConnectionmyConnection=newOleDbConnection(connstr);

OleDbCommandmyCommand=newOleDbCommand("select*fromtxtsample.txt",myConnection);

3.使用System.IO命名空间

System.IO命名空间包含的主要类:

File:提供用于创建、复制、删除、移动和打开文件的静态方法(即不需要创建类的实例,可直接调用类的方法)。

FileInfo:提供创建、复制、删除、移动和打开文件的实例方法(即需要创建类的实例,才能调用类的方法)。

StreamReader:从数据流中读取字符。

StreamWriter:从数据流中写入字符。

File类包含的主要方法

OpenText:打开现有的txt文件以进行读取。

Exists:确定制定的文件是否存在。

CreateText:创建或打开一个文件用于写入。

AppendText:将txt文本追加到现有文件。

protectedvoidPage_Load(Objectsender,EventArgse)

{

Response.Write("

"+"读取Txt文件的简单示例"+"

");

//创建StreamReader类的对象

StreamReaderobjstreamreader;

stringfilecont;

//打开现有的txt文件并将其赋值给StreamReader对象

objstreamreader=File.OpenText(@"c:\txtsample.txt");

//循环调用ReadLine方法读取txt文本,直至读完,并将结果显示在窗体中

while(objstreamreader.Peek()!=-1)

{

filecont=objstreamreader.ReadLine();

Response.Write(filecont+"

");

}

//读取完成,关闭StreamReader类的对象

objstreamreader.Close();

}

注:StreamReader的Peek方法能够返回制定StreamReader对象流中的下一个字符,但不把该字符从流中删掉;如果流中不再有文本字符可读,则返回-1。

protectedvoidPage_Load(Objectsender,EventArgse)

{

Response.Write("

"+"读取Txt文件的简单示例"+"

");

//定义新建txt文本的路径

stringFILE_NAME=@"c:\sample.txt";

//如果txt文件已存在,报错;否则,执行写操作

if(!File.Exists(FILE_NAME))

{

//创建SreamWriter对象

StreamWriterobjstreamwriter;

//创建txt文件并将其赋值给StreamWriter对象

objstreamwriter=File.CreateText(FILE_NAME);

//调用ReadLine方法向txt文本中写入一行字符

objstreamwriter.WriteLine("Writingtextsuccessfully!");

//写入完成,关闭StreamWriter类的对象

objstreamwriter.Close();

}

else

{

Response.Write("已经存在此文件!");

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。