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

来源:互联网 发布:读读日报和知乎日报 编辑:程序博客网 时间:2024/06/02 02:29

这篇文章写的很清楚。推荐一下。;链接地址:

引自: http://www.cnblogs.com/hide0511/archive/2006/09/05/495212.html

                http://www.cnblogs.com/sjrhero/articles/1833509.html

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

连接Access数据库

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

//导入命名空间
using System.Data.Odbc;

protectedvoid Page_Load(Object sender,EventArgs e)
{
//设置连接字符串
String connstr=@"DSN=sample";
//实例化Connection对象
OdbcConnection myConnection =new OdbcConnection(connstr);
//执行Open方法打开连接
myConnection.Open();
//执行SQL语句
OdbcCommand myCommand =new OdbcCommand("select * from sampletable",myConnection);
//将查询的结果赋给GridView的数据源
gv.DataSource = myCommand.ExecuteReader();
//绑定GridView
gv.DataBind();
//关闭连接
myConnection.Close();
}

 

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

//导入命名空间
using System.Data.Odbc;

protectedvoid Page_Load(Object sender,EventArgs e)
{
//设置连接字符串
String connstr=@"Driver=Microsoft Access Driver (*.mdb);Dbq=c:\sample.mdb;";
//实例化Connection对象
OdbcConnection myConnection =new OdbcConnection(connstr);
//执行Open方法打开连接
myConnection.Open();
//执行SQL语句
OdbcCommand myCommand =new OdbcCommand("select * from sampletable",myConnection);
//将查询的结果赋给GridView的数据源
gv.DataSource = myCommand.ExecuteReader();
//绑定GridView
gv.DataBind();
//关闭连接
myConnection.Close();
}

 

3.使用连接字符串进行连接(OLEDB)
OLEDB.NET Data Provider 支持的OLEDB Provider:
SQLOLEDB:用来访问SQL Server数据库
MSDAORA:用来访问Oracle数据库
Microsoft.Jet.OLEDB.4.0:用来访问Access数据库。

 

//导入命名空间
using System.Data.OleDb;

protectedvoid Page_Load(Object sender,EventArgs e)
{
//设置连接字符串
String connstr=@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=c:\sample.mdb;";
//实例化OleDbConnection对象
OleDbConnection myConnection =new OleDbConnection(connstr);
//执行Open方法打开连接
myConnection.Open();
//执行SQL语句
OleDbCommand myCommand =new OleDbCommand("select * from sampletable",myConnection);
//将查询的结果赋给GridView的数据源
gv.DataSource = myCommand.ExecuteReader();
//绑定GridView
gv.DataBind();
//关闭连接
myConnection.Close();
}

4.使用UDL文件进行连接
使用UDL文件连接数据源的步骤如下:
(1)新建一个记事本,其扩展名为.udl。
(2)双击该UDL文件,弹出“数据连接属性”对话框。
(3)该对话框首页显示“提供程序”选项卡,选择要使用的OLEDB提供程序。
(4)单击“下一步”,显示"l连接“选项卡”,设置好正确的参数后,单击“测试连接”

使用连接字符串
//导入命名空间
using System.Data.OleDb;

protectedvoid Page_Load(Object sender,EventArgs e)
{
//设置连接字符串
String connstr=@"FILE NAME=c:\oledb.udl";
//实例化OleDbConnection对象
OleDbConnection myConnection =new OleDbConnection(connstr);
//执行Open方法打开连接
myConnection.Open();
//执行SQL语句
OleDbCommand myCommand =new OleDbCommand("select * from sampletable",myConnection);
//将查询的结果赋给GridView的数据源
gv.DataSource = myCommand.ExecuteReader();
//绑定GridView
gv.DataBind();
//关闭连接
myConnection.Close();
}

连接MySQL数据库

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

//导入命名空间
using System.Data.Odbc;

protectedvoid Page_Load(Object sender,EventArgs e)
{
//设置连接字符串
String connstr=@"DSN=MySQL";
//实例化Connection对象
OdbcConnection myConnection =new OdbcConnection(connstr);
//执行Open方法打开连接
myConnection.Open();
//执行SQL语句
OdbcCommand myCommand =new OdbcCommand("select * from Names",myConnection);
//将查询的结果赋给GridView的数据源
gv.DataSource = myCommand.ExecuteReader();
//绑定GridView
gv.DataBind();
//关闭连接
myConnection.Close();
}

 

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

//导入命名空间
using System.Data.Odbc;

protectedvoid Page_Load(Object sender,EventArgs e)
{
//设置连接字符串
String connstr=@"Driver=MySQL ODBC 3.51 Driver;Server=localhost;Database=test;UID=root;PWD=yourpassword;Option=3;Port=3306";
//实例化Connection对象
OdbcConnection myConnection =new OdbcConnection(connstr);
//执行Open方法打开连接
myConnection.Open();
//执行SQL语句
OdbcCommand myCommand =new OdbcCommand("select * from Names",myConnection);
//将查询的结果赋给GridView的数据源
gv.DataSource = myCommand.ExecuteReader();
//绑定GridView
gv.DataBind();
//关闭连接
myConnection.Close();
}

连接Oracle数据库

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

//导入命名空间
using System.Data.OracleClient;

publicvoid Page_Load(Object sender,EventArgs e)
{
//设置连接字符串
string connstring =@"Data Source=oraclesample;User ID=oracleid;Password=oraclepwd;";
//实例化OracleConnection对象
OracleConnection conn =new OracleConnection(connstring);
//打开连接
connn.Open();
}

 

2.使用ODBC.NET Data Provider

//导入命名空间
using System.Data.Odbc;

publicvoid Page_Load(Object sender,EventArgs e)
{
//设置连接字符串
string connstring =@"Driver=Microsoft ODBC for Oracle;Server=oraclesample;Persisit Security Info=False;Trusted_Connection=yes;";
//实例化OracleConnection对象
OdbcConnection conn =new OdbcConnection(connstring);
//打开连接
connn.Open();
}

 

3.使用OLE DB.NET Data Provider

//导入命名空间
using System.Data.Oledb;

publicvoid Page_Load(Object sender,EventArgs e)
{
//设置连接字符串
string connstring =@"Provider=MSDAORA;Data Source=oraclesample;Persisit Security Info=False;Integrated Security=yes;";
//实例化OracleConnection对象
OleDbConnection conn =new OleDbConnection(connstring);
//打开连接
connn.Open();
}

 

访问Excel 

1.使用ODBC.NET Data Provider访问Excel

使用ODBC.NET Data Provider访问Excel 

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

 

2.使用OLE DB.NET Data Provider访问Excel

using System.Data.OleDb;

protectedvoid Page_Load(Object sender,EventArgs e)
{
//设置连接字符串
string connstr =@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=c:\excelsample.xls;Extened Properties=Excel 8.0;";
//实例化OdbcConnection对象
OleDbConnection myConnection =new OleDbConnection(connstr);
//执行Open方法打开连接
myConnection.Open();
//执行SQL语句
OleDbCommand myCommand =new OleDbCommand("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.使用ODBC.NET Data Provider

string connstr =@"Driver=Microsoft Text Driver(*.txt;*.csv);Dbq=c:\samplepath\;Extensions=asc,csv,tab,txt;";
OdbcConnection myConnection 
=new OdbcConnection(connstr);
OdbcCommand myCommand 
=new OdbcCommand("select * from txtsample.txt",myConnection);

 

2.使用OLE DB.NET Data Provider

string connstr =@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\samplepath\;Extended Properties=**text;HDR=Yes;FMT=Delimited""";
OleDbConnection myConnection 
=new OleDbConnection(connstr);
OleDbCommand myCommand 
=new OleDbCommand("select * from txtsample.txt",myConnection);

 

3.使用System.IO命名空间
  System.IO命名空间包含的主要类:
  File:提供用于创建、复制、删除、移动和打开文件的静态方法(即不需要创建类的实例,可直接调用类的方法)。
  FileInfo:提供创建、复制、删除、移动和打开文件的实例方法(即需要创建类的实例,才能调用类的方法)。
  StreamReader:从数据流中读取字符。
  StreamWriter:从数据流中写入字符。
  File类包含的主要方法
  OpenText:打开现有的txt文件以进行读取。
  Exists:确定制定的文件是否存在。
  CreateText:创建或打开一个文件用于写入。
  AppendText:将txt文本追加到现有文件。

<%@Import Namespace="System.IO"%> 
<script language="C#" runat="server">
protectedvoid Page_Load(Object sender, EventArgs e)
{
Response.Write(
"<h3>"+"读取Txt文件的简单示例"+"<br></h3>");
//创建StreamReader类的对象
StreamReader objstreamreader; 
string filecont;
//打开现有的txt文件并将其赋值给StreamReader对象
objstreamreader =File.OpenText(@"c:\txtsample.txt");
//循环调用ReadLine方法读取txt文本,直至读完,并将结果显示在窗体中
while(objstreamreader.Peek()!=-1
{
filecont 
= objstreamreader.ReadLine();
Response.Write(filecont
+"<br>");
}
//读取完成,关闭StreamReader类的对象
objstreamreader.Close();
}
</script>

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

<%@Import Namespace="System.IO"%> 
<script language="C#" runat="server">
protectedvoid Page_Load(Object sender, EventArgs e)
{
Response.Write(
"<h3>"+"读取Txt文件的简单示例"+"<br></h3>");
//定义新建txt文本的路径
string FILE_NAME =@"c:\sample.txt";
//如果txt文件已存在,报错;否则,执行写操作
if (!File.Exists(FILE_NAME))
{
//创建SreamWriter对象
StreamWriter objstreamwriter;
//创建txt文件并将其赋值给StreamWriter对象
objstreamwriter = File.CreateText(FILE_NAME);
//调用ReadLine方法向txt文本中写入一行字符
objstreamwriter.WriteLine("Writing text successfully!");
//写入完成,关闭StreamWriter类的对象
objstreamwriter.Close();
}
else
{
Response.Write(
"已经存在此文件!");

}
</script>

以上内容引自: http://www.cnblogs.com/hide0511/archive/2006/09/05/495212.html
0 0