首页 WinForm/WPF C# 数据库连接是否正常测试

C# 数据库连接是否正常测试

作者:胡同里的砖头 围观群众:1961 更新于:2013-12-19

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class SetDB : Form
    {
        public SetDB()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 连接测试是否成功
        /// </summary>
        public bool isConnect { get; set; }

        private void frmDb_Load(object sender, EventArgs e)
        {
            this.CenterToParent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //将得到配置值传给主安装程序类
            string serverName = textBox1.Text.Trim();
            string dbName = textBox2.Text.Trim();
            string userName = textBox3.Text.Trim();
            string password = textBox4.Text.Trim();
            isConnect = TestConnection(serverName, dbName, userName, password);//测试连接,此处调用其它类中的代码。
            if (isConnect == true)
            {
                label5.Text = "数据库连接测试成功!";
                button2.Enabled = true;
            }
            else
            {
                button2.Enabled = false;
                label5.Text = "数据库连接测试失败!";
            }
        }

        /// <summary>
        /// 测试连接
        /// </summary>
        /// <param name="serverName"></param>
        /// <param name="dbName"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        private bool TestConnection(string serverName, string dbName, string userName, string password)
        {
            string connectionString = GetConnectionString(serverName, dbName, userName, password);
            SqlConnection connection = new SqlConnection(connectionString);
            try
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }
                return true;
            }
            catch
            {
                CloseConnection(connection);
                return false;
            }
        }

        /// <summary>
        /// 得到连接字符串
        /// </summary>
        /// <param name="serverName"></param>
        /// <param name="dbName"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        private string GetConnectionString(string serverName, string dbName, string userName, string password)
        {
            string connectionString = "server={0};database={1};uid={2};pwd={3}";
            connectionString = string.Format(connectionString, serverName, dbName, userName, password);
            return connectionString;
        }

        /// <summary>
        /// 关闭数据库
        /// </summary>
        /// <param name="connection"></param>
        private void CloseConnection(SqlConnection connection)
        {
            if (connection != null)
            {
                //关闭数据库
                if (connection.State != System.Data.ConnectionState.Closed)
                {
                    connection.Close();
                    connection.Dispose();
                }
            }
        }
    }
}

  • 本文标题: C# 数据库连接是否正常测试
  • 文章分类:【WinForm/WPF】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8