none
how to check whether SQL SERVER installed & database is attached in vb.net 2010

    Question

  • hi

    I am using sql server 2005 with vb.net. I want to check whether database is attached or not at application startup. If application find that the database is not attached with sql server then I want to generate a custom exception msgbox like

    msgbox("Database is not attached with this application. Please attached your database to run this application or contact to the software vendor")

    but It seems that the application generate an sql exception msg and I don't like it. please give me vb.net codes.

    Saturday, September 07, 2013 6:14 AM

Answers

  • Hi haqayyum,

    First, I want to inform that this forum is to discuss SQL Server questions, regarding the question about how to write VB.NET codes, we can post the question to VB.NET forum. I tested the following codes and found they worked on my computer, you can also refer to them:

    Public sqlcon As String = "Data Source=ABDULQAYYUM-PC\SQLEXPRESS;Initial Catalog=LedgerDB;User Id=sa;Password=sa123;MultipleActiveResultSets=True"
            Dim dbcon As New SqlConnection(sqlcon)
            dbcon.Open()
            Dim dbcom As New SqlCommand("select db_id('DBNameHere')", dbcon)
            Dim rows As Integer = dbcom.ExecuteNonQuery()
            If rows <= 0 Then
                MessageBox.Show("Database is not attached with this application. Please attached your database to run this application or contact to the software vendor")
            End If
            dbcon.Close()

    For more detail information, you can refer to the following link:

    VB.NET forum:
    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral

    Using ADO.NET for beginners
    http://www.codeproject.com/Articles/8477/Using-ADO-NET-for-beginners


    Allen Li
    TechNet Community Support


    Monday, September 09, 2013 2:34 AM

All replies

  • Try this code:

    IF DB_ID('test') IS NULL 
        PRINT 'not attached'
    ELSE 
        PRINT 'ok!'


    The most important motivation for the research work that resulted in the relational model was the objective of providing a sharp and clear boundary between the logical and physical aspects of database management. - E. F. Codd


    My blog

    • Proposed as answer by Saeid Hasani Saturday, September 07, 2013 7:23 PM
    Saturday, September 07, 2013 6:18 AM
  • thanks Saeid Hasani

    when I used above query in management studio then perfect result but how to use this query in vb.net please give me a suggestion

    Saturday, September 07, 2013 6:42 AM
  • You have SERVER NAME!

    You have connection string!

    Just put this code in vb code and use it.


    The most important motivation for the research work that resulted in the relational model was the objective of providing a sharp and clear boundary between the logical and physical aspects of database management. - E. F. Codd


    My blog

    Saturday, September 07, 2013 6:53 AM
  • yes I have the following where I can put?

    Public sqlcon As String = "Data Source=ABDULQAYYUM-PC\SQLEXPRESS;Initial Catalog=LedgerDB;User Id=sa;Password=sa123;MultipleActiveResultSets=True" Public dbcon As New SqlConnection(sqlcon)

    • Edited by haqayyum Saturday, September 07, 2013 6:55 AM
    Saturday, September 07, 2013 6:54 AM
  • Have a look at this link:

    SqlCommand Constructor


    The most important motivation for the research work that resulted in the relational model was the objective of providing a sharp and clear boundary between the logical and physical aspects of database management. - E. F. Codd


    My blog

    Saturday, September 07, 2013 7:00 AM
  • I already familiar with your provided link but I don't understand where I can put your above query
    Saturday, September 07, 2013 7:06 AM
  • I am not a VB.NET expert.

    But there is a good sample in this link:

    how i write an sql command in vb.net?!


    The most important motivation for the research work that resulted in the relational model was the objective of providing a sharp and clear boundary between the logical and physical aspects of database management. - E. F. Codd


    My blog

    Saturday, September 07, 2013 8:55 AM
  • Hi haqayyum,

    First, I want to inform that this forum is to discuss SQL Server questions, regarding the question about how to write VB.NET codes, we can post the question to VB.NET forum. I tested the following codes and found they worked on my computer, you can also refer to them:

    Public sqlcon As String = "Data Source=ABDULQAYYUM-PC\SQLEXPRESS;Initial Catalog=LedgerDB;User Id=sa;Password=sa123;MultipleActiveResultSets=True"
            Dim dbcon As New SqlConnection(sqlcon)
            dbcon.Open()
            Dim dbcom As New SqlCommand("select db_id('DBNameHere')", dbcon)
            Dim rows As Integer = dbcom.ExecuteNonQuery()
            If rows <= 0 Then
                MessageBox.Show("Database is not attached with this application. Please attached your database to run this application or contact to the software vendor")
            End If
            dbcon.Close()

    For more detail information, you can refer to the following link:

    VB.NET forum:
    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral

    Using ADO.NET for beginners
    http://www.codeproject.com/Articles/8477/Using-ADO-NET-for-beginners


    Allen Li
    TechNet Community Support


    Monday, September 09, 2013 2:34 AM
  • thanks

    that is exactly what i want.

    Monday, September 09, 2013 8:41 AM