zoukankan      html  css  js  c++  java
  • Connection对象

    方法一:
    连接当前目录的一个Access数据库看文件,利用Connection对象打开一个数据库连接,然后利用State属性将状态显示到浏览器上。
    using System.Data.OleDb;

    protected
     void Page_Load(object sender, EventArgs e)
        {
            OleDbConnection conn 
    = new OleDbConnection();
            conn.ConnectionString 
    = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                
    "Data Source=" + Server.MapPath("person.mdb");
            conn.Open();
            Message.Text 
    = conn.State.ToString();
            conn.Close();
        }

    <form id="form1" runat="server">
        
    <div>
            
    <asp:Label ID="Message" runat="server" Width="288px"></asp:Label>&nbsp;</div>
    </form>

    方法二:
    可以通过Connection对象的构造函数传入连接串
    using System.Data.OleDb;

    protected
     void Page_Load(object sender, EventArgs e)
        {
            OleDbConnection conn 
    = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
                
    "Data Source=" + Server.MapPath("person.mdb"));
            conn.Open();
            Message.Text 
    = conn.State.ToString();
            conn.Close();
        }

    <form id="form1" runat="server">
        
    <div>
            
    <asp:Label ID="Message" runat="server" Width="288px"></asp:Label>&nbsp;</div>
    </form>

    方法三:
    连接SQL Server数据库,只需修改命名空间
    using System.Data.SqlClient;

    protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection conn 
    = new SqlConnection();
            conn.ConnectionString 
    = "server=localhost;database=pubs;uid=sa;pwd=''";
            conn.Open();
            Message.Text 
    = conn.State.ToString();
            conn.Close();
        }

    <form id="form1" runat="server">
        
    <div>
            
    <asp:Label ID="Message" runat="server" Width="288px"></asp:Label>&nbsp;</div>
    </form>
  • 相关阅读:
    取得元素节点的默认display值
    mass Framework emitter模块 v2
    memset函数详细说明
    八大排序算法总结
    电脑很卡,怎么办?这里帮你解决
    Android APK反编译详解(附图)
    java环境变量配置
    如何使用U盘安装操作系统 安装GHOST XP, xp纯净版
    c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
    Js apply 方法 详解
  • 原文地址:https://www.cnblogs.com/qixin622/p/755749.html
Copyright © 2011-2022 走看看