Something to try
Something to try
Something to try
Something to try
Something to try
Something to try Something to try Something to try Something to try Something to try Something to try Something to try Something to try
Something to try Something to try
Something to try
Go Back  Xtreme Visual Basic Talk > > > Something to try


Reply
 
Thread Tools Display Modes
  #1  
Old 07-29-2013, 05:03 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default Something to try


Here is something that i have never been
able to do and that is to connect to the
master server, if you want to call it that.

I guess for me, that would be sqlexpress 2012

Anyway, i just wanted to share some code that
would allow you to connect to your master server
and from there, you can create a new database,
if you write the code for it. This code just
opens and closes a connection.

Code:
Imports System.Data.SqlClient

' A couple of variables
Private ConnectionString As String = "Data Source=.\SQLEXPRESS;Integrated Security=True;Connect Timeout=30;User Instance=True"
Private conn As SqlConnection


Private Sub btnOpenCon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenCon.Click

    ' This bit of code, opens a connection to your master
    ' sql server, so that you can create a new database.

    ' This is just a demo, to show how to open a connection to the server

       
    ' Create a connection
    conn = New SqlConnection(ConnectionString)

    ' Open the connection
    If conn.State <> ConnectionState.Open Then conn.Close()

        Try
            conn.Open()
            MsgBox("Connection Open")

            conn.Close()
            MsgBox("Connection Closed")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

End Sub
Reply With Quote
  #2  
Old 07-29-2013, 05:18 PM
VB_Alien VB_Alien is offline
Senior Contributor
 
Join Date: Apr 2004
Posts: 854
Default

Here is a simple way to open a sql database(mdf)
as long as your using sql express server.

Code:
Imports System.Data.SqlClient

' Some variables
Private ConnectionString As String
Private DbName As String
Private conn As SqlConnection

Private Sub btnOpenDb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenDb.Click

    ' Just testing to open a connection to database

    Dim Fd As New OpenFileDialog

    Fd.Filter = "Sql Database|*.mdf"
    Fd.Title = "Open Database"
    Fd.FileName = ""

    'Load a mdf database file name into the DbName String
    If Fd.ShowDialog = Windows.Forms.DialogResult.OK Then
         DbName = Fd.FileName
    Else
         Exit Sub
    End If

    ' Here is the connection string, without using a password or user name
    ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=" + DbName + ";Integrated Security=True;User Instance=True"

    ' Close any open connections
    If conn.State = ConnectionState.Open Then conn.Close()

    Try
       ' Create a connection
       conn = New SqlConnection(ConnectionString)

       conn.Open()
       MsgBox("Connection Open")
       
       conn.Close()
       MsgBox("Connection Closed")

    Catch ex As Exception
       MsgBox(ex.Message, , "Error Opening Database")

       ' Close the connection
       If conn.State = ConnectionState.Open Then conn.Close()
    End Try
End Sub
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Something to try
Something to try
Something to try Something to try
Something to try
Something to try
Something to try Something to try Something to try Something to try Something to try Something to try Something to try
Something to try
Something to try
 
Something to try
Something to try
 
-->