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

  一、连接Access数据库
  1.使用已有DSN的连接字符串进行连接(ODBC)
ContractedBlock.gif ExpandedBlockStart.gif 使用DSN进行连接
None.gif//导入命名空间
None.gif
using System.Data.Odbc;
None.gif
None.gif
protected void Page_Load(Object sender,EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  
//设置连接字符串
InBlock.gif
  String connstr=@"DSN=sample";
InBlock.gif  
//实例化Connection对象
InBlock.gif
  OdbcConnection myConnection = new OdbcConnection(connstr);
InBlock.gif  
//执行Open方法打开连接
InBlock.gif
  myConnection.Open();
InBlock.gif  
//执行SQL语句
InBlock.gif
  OdbcCommand myCommand = new OdbcCommand("select * from sampletable",myConnection);
InBlock.gif  
//将查询的结果赋给GridView的数据源
InBlock.gif
  gv.DataSource = myCommand.ExecuteReader();
InBlock.gif  
//绑定GridView
InBlock.gif
  gv.DataBind();
InBlock.gif  
//关闭连接
InBlock.gif
  myConnection.Close();
ExpandedBlockEnd.gif}

  2.使用无DSN的连接字符串进行连接(ODBC)
ContractedBlock.gif ExpandedBlockStart.gif 不使用DSN进行连接
None.gif//导入命名空间
None.gif
using System.Data.Odbc;
None.gif
None.gif
protected void Page_Load(Object sender,EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  
//设置连接字符串
InBlock.gif
  String connstr=@"Driver=Microsoft Access Driver (*.mdb);Dbq=c:\sample.mdb;";
InBlock.gif  
//实例化Connection对象
InBlock.gif
  OdbcConnection myConnection = new OdbcConnection(connstr);
InBlock.gif  
//执行Open方法打开连接
InBlock.gif
  myConnection.Open();
InBlock.gif  
//执行SQL语句
InBlock.gif
  OdbcCommand myCommand = new OdbcCommand("select * from sampletable",myConnection);
InBlock.gif  
//将查询的结果赋给GridView的数据源
InBlock.gif
  gv.DataSource = myCommand.ExecuteReader();
InBlock.gif  
//绑定GridView
InBlock.gif
  gv.DataBind();
InBlock.gif  
//关闭连接
InBlock.gif
  myConnection.Close();
ExpandedBlockEnd.gif}

  3.使用连接字符串进行连接(OLEDB)
OLEDB.NET Data Provider 支持的OLEDB Provider:
SQLOLEDB:用来访问SQL Server数据库
MSDAORA:用来访问Oracle数据库
Microsoft.Jet.OLEDB.4.0:用来访问Access数据库。
ContractedBlock.gif ExpandedBlockStart.gif 使用连接字符串
None.gif//导入命名空间
None.gif
using System.Data.OleDb;
None.gif
None.gif
protected void Page_Load(Object sender,EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  
//设置连接字符串
InBlock.gif
  String connstr=@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=c:\sample.mdb;";
InBlock.gif  
//实例化OleDbConnection对象
InBlock.gif
  OleDbConnection myConnection = new OleDbConnection(connstr);
InBlock.gif  
//执行Open方法打开连接
InBlock.gif
  myConnection.Open();
InBlock.gif  
//执行SQL语句
InBlock.gif
  OleDbCommand myCommand = new OleDbCommand("select * from sampletable",myConnection);
InBlock.gif  
//将查询的结果赋给GridView的数据源
InBlock.gif
  gv.DataSource = myCommand.ExecuteReader();
InBlock.gif  
//绑定GridView
InBlock.gif
  gv.DataBind();
InBlock.gif  
//关闭连接
InBlock.gif
  myConnection.Close();
ExpandedBlockEnd.gif}

  4.使用UDL文件进行连接
使用UDL文件连接数据源的步骤如下:
(1)新建一个记事本,其扩展名为.udl。
(2)双击该UDL文件,弹出“数据连接属性”对话框。
(3)该对话框首页显示“提供程序”选项卡,选择要使用的OLEDB提供程序。
(4)单击“下一步”,显示"l连接“选项卡”,设置好正确的参数后,单击“测试连接”。
ContractedBlock.gif ExpandedBlockStart.gif 使用UDL进行连接
None.gif使用连接字符串
None.gif
//导入命名空间
None.gif
using System.Data.OleDb;
None.gif
None.gif
protected void Page_Load(Object sender,EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  
//设置连接字符串
InBlock.gif
  String connstr=@"FILE NAME=c:\oledb.udl";
InBlock.gif  
//实例化OleDbConnection对象
InBlock.gif
  OleDbConnection myConnection = new OleDbConnection(connstr);
InBlock.gif  
//执行Open方法打开连接
InBlock.gif
  myConnection.Open();
InBlock.gif  
//执行SQL语句
InBlock.gif
  OleDbCommand myCommand = new OleDbCommand("select * from sampletable",myConnection);
InBlock.gif  
//将查询的结果赋给GridView的数据源
InBlock.gif
  gv.DataSource = myCommand.ExecuteReader();
InBlock.gif  
//绑定GridView
InBlock.gif
  gv.DataBind();
InBlock.gif  
//关闭连接
InBlock.gif
  myConnection.Close();
ExpandedBlockEnd.gif}

  二、连接MySQL数据库
  1.使用已有DSN的连接字符串进行连接
ContractedBlock.gif ExpandedBlockStart.gif 使用DSN进行连接
None.gif//导入命名空间
None.gif
using System.Data.Odbc;
None.gif
None.gif
protected void Page_Load(Object sender,EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  
//设置连接字符串
InBlock.gif
  String connstr=@"DSN=MySQL";
InBlock.gif  
//实例化Connection对象
InBlock.gif
  OdbcConnection myConnection = new OdbcConnection(connstr);
InBlock.gif  
//执行Open方法打开连接
InBlock.gif
  myConnection.Open();
InBlock.gif  
//执行SQL语句
InBlock.gif
  OdbcCommand myCommand = new OdbcCommand("select * from Names",myConnection);
InBlock.gif  
//将查询的结果赋给GridView的数据源
InBlock.gif
  gv.DataSource = myCommand.ExecuteReader();
InBlock.gif  
//绑定GridView
InBlock.gif
  gv.DataBind();
InBlock.gif  
//关闭连接
InBlock.gif
  myConnection.Close();
ExpandedBlockEnd.gif}

  2.使用无DSN的连接字符串进行连接
ContractedBlock.gif ExpandedBlockStart.gif 不使用DSN进行连接
None.gif//导入命名空间
None.gif
using System.Data.Odbc;
None.gif
None.gif
protected void Page_Load(Object sender,EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  
//设置连接字符串
InBlock.gif
  String connstr=@"Driver=MySQL ODBC 3.51 Driver;Server=localhost;Database=test;UID=root;PWD=yourpassword;Option=3;Port=3306";
InBlock.gif  
//实例化Connection对象
InBlock.gif
  OdbcConnection myConnection = new OdbcConnection(connstr);
InBlock.gif  
//执行Open方法打开连接
InBlock.gif
  myConnection.Open();
InBlock.gif  
//执行SQL语句
InBlock.gif
  OdbcCommand myCommand = new OdbcCommand("select * from Names",myConnection);
InBlock.gif  
//将查询的结果赋给GridView的数据源
InBlock.gif
  gv.DataSource = myCommand.ExecuteReader();
InBlock.gif  
//绑定GridView
InBlock.gif
  gv.DataBind();
InBlock.gif  
//关闭连接
InBlock.gif
  myConnection.Close();
ExpandedBlockEnd.gif}

  三、连接Oracle数据库
  1.使用Oracle.NET Data Provider(需要安装Oracle客户端)
ContractedBlock.gif ExpandedBlockStart.gif Oracle.NET Data Provider
None.gif//导入命名空间
None.gif
using System.Data.OracleClient;
None.gif
None.gif
public void Page_Load(Object sender,EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  
//设置连接字符串
InBlock.gif
  string connstring = @"Data Source=oraclesample;User ID=oracleid;Password=oraclepwd;";
InBlock.gif  //实例化OracleConnection对象
InBlock.gif  OracleConnection conn = new OracleConnection(connstring);
InBlock.gif  //打开连接
InBlock.gif  connn.Open();
InBlock.gif}

  2.使用ODBC.NET Data Provider
ContractedBlock.gif ExpandedBlockStart.gif ODBC.NET Data Provider
None.gif//导入命名空间
None.gif
using System.Data.Odbc;
None.gif
None.gif
public void Page_Load(Object sender,EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  
//设置连接字符串
InBlock.gif
  string connstring = @"Driver=Microsoft ODBC for Oracle;Server=oraclesample;Persisit Security Info=False;Trusted_Connection=yes;";
InBlock.gif  
//实例化OracleConnection对象
InBlock.gif
  OdbcConnection conn = new OdbcConnection(connstring);
InBlock.gif  
//打开连接
InBlock.gif
  connn.Open();
ExpandedBlockEnd.gif}

3.使用OLE DB.NET Data Provider
ContractedBlock.gif ExpandedBlockStart.gif OleDb.NET Data Provider
None.gif//导入命名空间
None.gif
using System.Data.Oledb;
None.gif
None.gif
public void Page_Load(Object sender,EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  
//设置连接字符串
InBlock.gif
  string connstring = @"Provider=MSDAORA;Data Source=oraclesample;Persisit Security Info=False;Integrated Security=yes;";
InBlock.gif  
//实例化OracleConnection对象
InBlock.gif
  OleDbConnection conn = new OleDbConnection(connstring);
InBlock.gif  
//打开连接
InBlock.gif
  connn.Open();
ExpandedBlockEnd.gif}

  四、访问Excel
  1.使用ODBC.NET Data Provider访问Excel
ContractedBlock.gif ExpandedBlockStart.gif 使用ODBC.NET Data Provider访问Excel
None.gifusing System.Data.Odbc;
None.gif
None.gif
protected void Page_Load(Object sender,EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  
//设置连接字符串
InBlock.gif
  string connstr = @"Driver=Microsoft Excel Driver(*.xls);Dbq=c:\excelsample.xls;";
InBlock.gif  
//实例化OdbcConnection对象
InBlock.gif
  OdbcConnection myConnection = new OdbcConnection(connstr);
InBlock.gif  
//执行Open方法打开连接
InBlock.gif
  myConnection.Open();
InBlock.gif  
//执行SQL语句
InBlock.gif
  OdbcCommand myCommand = new OdbcCommand("select * from [Sheet1$]",myConnection);
InBlock.gif  
//用GridView来显示数据
InBlock.gif
  gv.DataSource = myCommand.ExecuteReader();
InBlock.gif  gv.DataBind();
InBlock.gif  
//调用Close方法关闭连接
InBlock.gif
  myConnection.Close();
ExpandedBlockEnd.gif}
注:ConnectionString属性为Driver(驱动器名),Dbq ( 访问Excel时使用的SQL语句与访问数据库时使用的语句奏本相同,只是from后面的表名的写法不同,如"select  * from [Sheet1$],表示访问的是Shee表,若要访问Sheet2,Sheet3,替换SQL语句中的Sheetl即可。

  2.使用OLE DB.NET Data Provider访问Excel
ContractedBlock.gif ExpandedBlockStart.gif 使用OleDb.NET Data Provider访问Excel
None.gifusing System.Data.OleDb;
None.gif
None.gif
protected void Page_Load(Object sender,EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  
//设置连接字符串
InBlock.gif
  string connstr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=c:\excelsample.xls;Extened Properties=Excel 8.0;";
InBlock.gif  
//实例化OdbcConnection对象
InBlock.gif
  OleDbConnection myConnection = new OleDbConnection(connstr);
InBlock.gif  
//执行Open方法打开连接
InBlock.gif
  myConnection.Open();
InBlock.gif  
//执行SQL语句
InBlock.gif
  OleDbCommand myCommand = new OleDbCommand("select * from [Items$]",myConnection);
InBlock.gif  
//用GridView来显示数据
InBlock.gif
  gv.DataSource = myCommand.ExecuteReader();
InBlock.gif  gv.DataBind();
InBlock.gif  
//调用Close方法关闭连接
InBlock.gif
  myConnection.Close();
ExpandedBlockEnd.gif}
注: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
ContractedBlock.gif ExpandedBlockStart.gif 使用ODBC.NET Data Provider
None.gifstring connstr = @"Driver=Microsoft Text Driver(*.txt;*.csv);Dbq=c:\samplepath\;Extensions=asc,csv,tab,txt;";
None.gifOdbcConnection myConnection 
= new OdbcConnection(connstr);
None.gifOdbcCommand myCommand 
= new OdbcCommand("select * from txtsample.txt",myConnection);

  2.使用OLE DB.NET Data Provider
ContractedBlock.gif ExpandedBlockStart.gif 使用OleDb.NET Data Provider
None.gifstring connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\samplepath\;Extended Properties=**text;HDR=Yes;FMT=Delimited""";
None.gifOleDbConnection myConnection 
= new OleDbConnection(connstr);
None.gifOleDbCommand myCommand 
= new OleDbCommand("select * from txtsample.txt",myConnection);

  3.使用System.IO命名空间
  System.IO命名空间包含的主要类:
File:提供用于创建、复制、删除、移动和打开文件的静态方法(即不需要创建类的实例,可直接调用类的方法)。
FileInfo:提供创建、复制、删除、移动和打开文件的实例方法(即需要创建类的实例,才能调用类的方法)。
StreamReader:从数据流中读取字符。
StreamWriter:从数据流中写入字符。
  File类包含的主要方法
OpenText:打开现有的txt文件以进行读取。
Exists:确定制定的文件是否存在。
CreateText:创建或打开一个文件用于写入。
AppendText:将txt文本追加到现有文件。
ContractedBlock.gif ExpandedBlockStart.gif 读取txt文件
None.gif<%@Import Namespace="System.IO"%> 
None.gif
<script language="C#" runat="server">
None.gif    
protected void Page_Load(Object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        Response.Write(
"<h3>"+"读取Txt文件的简单示例"+"<br></h3>");
InBlock.gif        
//创建StreamReader类的对象
InBlock.gif
        StreamReader objstreamreader; 
InBlock.gif        
string filecont;
InBlock.gif        
//打开现有的txt文件并将其赋值给StreamReader对象
InBlock.gif
        objstreamreader =File.OpenText(@"c:\txtsample.txt");
InBlock.gif        
//循环调用ReadLine方法读取txt文本,直至读完,并将结果显示在窗体中
InBlock.gif
          while(objstreamreader.Peek()!=-1
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            filecont 
= objstreamreader.ReadLine();
InBlock.gif            Response.Write(filecont
+"<br>");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//读取完成,关闭StreamReader类的对象
InBlock.gif
        objstreamreader.Close();
ExpandedBlockEnd.gif    }

None.gif
</script>

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

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

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(
"已经存在此文件!");
ExpandedSubBlockEnd.gif        }
        
ExpandedBlockEnd.gif    }

None.gif
</script>

转载于:https://www.cnblogs.com/hide0511/archive/2006/09/05/495212.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值