数据库测试链接的代码

2015-12-01 15:30:32  访问(2009) 赞(0) 踩(0)

// aspx 页面 //

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ConnTest.aspx.cs" Inherits="ConnTest"  ValidateRequest="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    数据库链接串:<asp:TextBox ID="txt_Conn" runat="server" Text="" Width="95%"></asp:TextBox><br /><br />
    <asp:Button ID="btn_Test" runat="server" Text="测试连接" OnClick="btn_Test_Click" /><br /><br />
    测试结果:<asp:TextBox ID="txt_Result" runat="server" Width="95%"></asp:TextBox>
     
    
    </div>
    </form>
</body>
</html>

// CS页面 //
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class ConnTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_Test_Click(object sender, EventArgs e)
    {
        try
        {
            DoOK();
        }
        catch (Exception err)
        {
            txt_Result.Text = "发生异常:" + err.Message;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    protected void DoOK()
    {
        string dbConnectionString = txt_Conn.Text.Trim();

        if (dbConnectionString == null || dbConnectionString.Length == 0)
        {
            txt_Result.Text = "发生错误:" + "请输入数据库链接串";
            return;
        }

        string theResult = SqlServerConnTest(dbConnectionString);

        txt_Result.Text = theResult;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="dbConnectionString"></param>
    /// <returns></returns>
    protected string SqlServerConnTest(string dbConnectionString)
    {
        string theResult = null;

        SqlConnection conn = null;
        SqlCommand cmd = null;

        try
        {
            conn = new SqlConnection(dbConnectionString);
            conn.Open();

            cmd = new SqlCommand();
            cmd.CommandText = "select getdate() as retValue ";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = conn;

            object obj = cmd.ExecuteScalar();

            if (obj != null)
            {
                theResult = "连接成功,当前数据库时间为:" + obj.ToString();
            }
            else
            {
                theResult = "连接成功";
            }
        }
        catch (Exception err)
        {
            theResult = "-连接失败,发生异常:" + err.Message;
        }
        finally
        {
            if (cmd != null)
            {
                cmd.Parameters.Clear();
                cmd.Dispose();
                cmd = null;
            }

            if (conn != null)
            {
                if (conn.State != ConnectionState.Closed)
                {
                    conn.Close();
                }

                conn.Dispose();
                conn = null;
            }
        }

        return theResult;
    }
}



标签:SqlServer    数据库    连接    测试连接    ConnTest 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)