欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 通用的连接数据库实例代码

程序员文章站 2024-02-28 18:09:58
view code复制代码 代码如下:<%@ page language="c#" autoeventwireup="true" codefile="default....

view code

复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>


<center>
<h2><font face="宋体">访问数据库的通用代码实例</font>
</h2>
</center>
<body>
    <form id="form1" runat="server">
    <div>

    <font face="宋体">
<p align="center">1.请输入相应数据库连接字符串</p>
<p align="center">
<asp:textbox id="connstrtextbox" runat="server" width="600"></asp:textbox>
</p>
<p align="center">2.请输入相应sql查询命令语句</p>
<p align="center">
<asp:textbox id="sqltexttextbox" runat="server" width="600"></asp:textbox>
</p>
<p align="center">3.请选择所连接的数据库类型</p>
<p align="center">
    <asp:dropdownlist id="dbdropdownlist" runat="server" width="204px">
        <asp:listitem selected="true">access</asp:listitem>
        <asp:listitem>sqlserver</asp:listitem>
        <asp:listitem>oracle</asp:listitem>
        <asp:listitem>db2</asp:listitem>
    </asp:dropdownlist>
</p>
<p align="center">

<asp:button id="button1" runat="server" onclick="button1_click"  text="通用数据库连接代码测试" />

</p>
<p align="center">
<asp:label id="lblmessage" runat="server" font-bold="true" forecolor="red"></asp:label>
</p>
    </form>
</font>
</div>

asp.net页面

复制代码 代码如下:

using system;
using system.configuration;
using system.data;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;

public partial class _default : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {
        //通用数据库连接代码,这里以连接access数据库为测试示例
        if (!ispostback)
        {
           connstrtextbox.text = "provider=microsoft.jet.oledb.4.0; data source=" + server.mappath("user.mdb");
           sqltexttextbox.text = "select count(*) from info where name='小顾'";
            lblmessage.text = "";
        }
    }
    protected void button1_click(object sender, eventargs e)
    {

        //定义数据库连接字符串
        string myconnectionstring = this.connstrtextbox.text;
        //定义查询操作的sql语句
        string mysql = this.sqltexttextbox.text;
        //定义所要连接的数据库类型为access
        string mytype = this.dbdropdownlist.selectedvalue;
        system.data.idbconnection myconnection = null;
        // 根据数据库类型,创建相应的 connection 对象
        switch (mytype)
        {
            //选择的数据库类型为“sqlserver”,创建sqlconnection类数据库连接对象
            case "sqlserver":
                myconnection = new system.data.sqlclient.sqlconnection(myconnectionstring);
                break;
            case "oracle":
                myconnection = new system.data.oracleclient.oracleconnection(myconnectionstring);
                break;
            //选择的数据库类型为“access”,创建oledbconnection类数据库连接对象
            case "access":
                myconnection = new system.data.oledb.oledbconnection(myconnectionstring);
                break;
            //选择的数据库类型为“db2”,创建oledbconnection类数据库连接对象
            case "db2":
                myconnection = new system.data.odbc.odbcconnection(myconnectionstring);
                break;
            default:
                myconnection = new system.data.oledb.oledbconnection(myconnectionstring);
                break;
        }
        execute(myconnection, mysql);
    }
    public void execute(system.data.idbconnection myconnection, string strquery)
    {
        //使用 createcommand() 方法生成 command 对象
        system.data.idbcommand mycommand = myconnection.createcommand();
        //执行定义的sql查询语句
        mycommand.commandtext = strquery;
        try
        {
            //打开数据库连接
            myconnection.open();
            //定义查询的结果信息
            string myinfo = "测试连接成功!符合查询要求的记录共有:" + mycommand.executescalar().tostring() + "条!";
            //输出查询结果信息
            lblmessage.text = myinfo;
        }
        catch (exception ex)
        {
            //输出错误异常
            response.write(ex.tostring());
        }
        finally
        {
            //关闭数据库连接
            myconnection.close();
        }
    }
}

本段程序的核心代码为

复制代码 代码如下:

//选择的数据库类型为“sqlserver”,创建sqlconnection类数据库连接对象
case "sqlserver":
           myconnection = new system.data.sqlclient.sqlconnection(myconnectionstring);
                break;
case "oracle":
           myconnection = new system.data.oracleclient.oracleconnection(myconnectionstring);
                break;
            //选择的数据库类型为“access”,创建oledbconnection类数据库连接对象
case "access":
           myconnection = new system.data.oledb.oledbconnection(myconnectionstring);
                break;
            //选择的数据库类型为“db2”,创建oledbconnection类数据库连接对象
case "db2":
           myconnection = new system.data.odbc.odbcconnection(myconnectionstring);
                break;
default:
            myconnection = new system.data.oledb.oledbconnection(myconnectionstring);
                break;


如果你要其它连接我们还可以增加一些连接代码哦。