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

Consistency level in Azure cosmos db

 Consistency level in Azure cosmos db Azure Cosmos DB offers five well-defined consistency levels to provide developers with the flexibility...