troubleshooting Question

Connecting to a MSSQL Database Using ASP.NET

Avatar of introlux
introluxFlag for United Kingdom of Great Britain and Northern Ireland asked on
ASP.NETDatabases
11 Comments1 Solution141 ViewsLast Modified:
Hi,

I have a sample code which I am trying to put together a web page that checks the MSSQL database connection, if it can connect successfully then I would like it to display "Success" and if it cannot "Failed"

using System;
using System.Windows.Forms;
using System.Data.SqlClient; 

namespace WindowsApplication1

{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection connection ;
            SqlCommand command ;
            string sql = null;
            connetionString = "Data Source=ServerName;Initial Catalog=TEST;User ID=TEST;Password=TEST";

            sql = "select max(table_catalog) as x from information_schema.tables";

            connection = new SqlConnection(connetionString);
            try

            {
                connection.Open();
                command = new SqlCommand(sql, connection);
                command.ExecuteNonQuery();
                command.Dispose();
                connection.Close();
                MessageBox.Show (" ExecuteNonQuery in SqlCommand executed !!");
            }

            catch (Exception ex)

            {
                MessageBox.Show("Can not open connection ! ");
            }
        }
    }
}

Any help will be appreciated.

Thanks,

introlux
ASKER CERTIFIED SOLUTION
introlux
Techincal Director

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 11 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros