8 June 2012

Check connection string is valid or not in C#


//Check connection string is valid or not in C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace SupportingToolsforCode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string connectionstring = ConfigurationManager.AppSettings["ConnectionString"];

         ///

        /// check connection string is valid or not
        ///

        ///
        ///
        private void btnCheck_Click(object sender, EventArgs e)
        {
           
           
            if(!(String.IsNullOrEmpty(connectionstring)))
            {
                try
                {

                    //To check using SqlHelper
                    // SqlHelper objSqlHelper = new SqlHelper(connectionstring);

                    using (SqlConnection conn = new SqlConnection(connectionstring))
                    {
                        conn.Open();
                        MessageBox.Show("Connection String is valid");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(Convert.ToString(ex));
                }              
            }

        }
}



No comments:

Post a Comment

Comments Welcome

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...