473,378 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to connect How to check SQL connection in C#.Net?

Hi,

i want check my SQL server connection before to connect with DB, and i need to update the status of SQL Server connection in my GUI.

can any one give some idea to get output?
Aug 19 '10 #1
5 12312
I f you are interested getting the solution in VB.NET I think I have the solution for you
Aug 19 '10 #2
Just post code here(only connection checking code) from that i can able to convert C#.net
Aug 19 '10 #3
Following is how to know if youare connected to the database and it continues help you to back p the DB as well. if you want more , you can check my entire article on msdn.com

http://social.msdn.microsoft.com/For...559cca?lc=1033



GEORGESNAFFAH is the sqlconnection string that targets to the database



Dim GEORGESNAFFAH As New SqlConnection("Data Source=.\SQLEXPRESS;Integrated Security=True")





' Defining the SQL Commands.



Dim cmd As SqlCommand







' we will define the sql string containing the sql to be executed , we have 1 so we define 1 string.



Dim sql As String


'Open Connection_ & _string and definition of SQL CODE

GEORGESNAFFAH.Open()



' If the Connection is openned succesfully the state of GEORGESNAFFAH Connectionstring will display a message



If GEORGESNAFFAH.State = ConnectionState.Open Then

MsgBox("Connection Successfully Created - Press OK to start the Backup process - This might take a few minutes .")




'Script to Back up the Database

sql ="BACKUP DATABASE [data2] TO DISK = N'C:\DB_Backup\data2.bak' WITH NOFORMAT, NOINIT, NAME = N'LGAS_OFFICE-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10"






' command = new sql command (sql + name of the sqlconnection)

cmd =New SqlCommand(sql, GEORGESNAFFAH)

cmd.ExecuteNonQuery()







'Close Connection

cmd.Dispose()







' After that GEORGESNAFFAH will close and the Form will automatically Close

GEORGESNAFFAH.Close()

MsgBox("Database Backup Completed Successfully")





'Close the form



Me.Close()







End If



End Sub
Aug 19 '10 #4
let me know if it helped you
Aug 19 '10 #5
Thanks George,
Here is the code i am checking SQL connection but i couldn't able to get status frequently

Scenario :

* stop the sql server service from services window
* Running the project and will show status "Connection Not Available"
* Start Sql server service and displaying "Connection Live"
* And Again Stop SQL server service and i am not getting the status as ""Connection Not Available". It's returning status as "Connection Live"
* It's not get in to the catch block

Code:

private void timer1_Tick(object sender, EventArgs e)
{
bool Flag = false;
try
{
using (SqlConnection con = new SqlConnection(strcon))
{
con.Open();
}
}
catch (SqlException s)
{
Flag = true;
label1.Text = "Connection Not available";
}
finally
{
if (Flag == false)
{
label1.Text = "Connection Live";
}
}
}
Aug 19 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mel | last post by:
Please help
0
by: Jay Douglas | last post by:
Hello, I'm trying to create a Data Links Property window that behaves just like the dialog box in Visual Studio .Net for MS Sql Server connections. I've found a lot of examples that use...
2
by: Mondal | last post by:
Hi, I am using MySQL 5.0 beta and Active Python 2.4. I have the correct version of MySQLdb installed. The problem is that I can't connect to MySQL through the Active Python interactive window. ...
14
by: martin1 | last post by:
All, I want to check wether db connection/open or not, if not it will post db connection error to user in message box The code is like: Dim objConnection As New SqlConnection _...
3
by: jku | last post by:
Hello, after installing DB2 V8.2.3 on OpenSuse 10.1 (in vmware) we have created a database instance with the restore command: db2 "restore database $OLD_DB user db2inst1 using db2inst1 from...
3
by: yes_its_just_me | last post by:
Hi everyone, I haven't used PHP since version 4 and am trying to use it for a new project. All I'm trying to do is connect to a MySQL database and show the contents of that database (as a start)....
4
by: eruth | last post by:
There are loads of post on this, but nothing that seems to cover my exact problem ;) I have an ASP.Net 1.1 web application running on my local machine. I want to connect to an SQL 2005 server...
0
by: =?Utf-8?B?UmljaGFyZCBC?= | last post by:
We have been receiving a connection error at one of our sites, our application works elsewhere. The error occurs when our application written in vb.net 2.0 running as a Windows Service under the...
1
by: Johannes Bauer | last post by:
Hello group, I've run into a small problem with pgdb which is actually not PostgreSQL specific - I just do not understand the Python syntax at one point. I'm trying to initialize a connection...
5
Claus Mygind
by: Claus Mygind | last post by:
When I force a failure on the connection to my MySQL server in php, I get this error message. "<br /> <b>Warning</b>: mysqli::mysqli() : (28000/1045): Access denied for user...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.