Reading a Book that uses a class Method to connect to a DB But I Don't Know how to set it up?
What does this function Mean is this using a server ALias?
How do I Setup the DB?
I think its meant to use these Files But Ive never used this type of DB:
DB_Attach.bat
DB_Attach.sql
Halloween.mdf
Halloween_log.LDF
Private Shared Function Connection() As SqlConnection
Dim ConHalloween As New SqlConnection
Dim sConnectionString As String = "Data Source=Doug\VSDOTNET;" _
& "Initial Catalog=Halloween;Integrated Security=SSPI"
ConHalloween.ConnectionString = sConnectionString
Return ConHalloween
End Function
DB_Attach.bat is a bat file. You should be able to open this with notepad and view its contents
DB_Attach.sql is most likely a script to create tables and or stored procedures, etc. You can also open this with notepad and view its contents. You would run that script (in my case) in say query analyser and it would create tables, etc.
That function is basically just a function that you can call to get your connection. IMO this function is not needed as you can put your Connection String in your web.config file.
Here is an example
Code:
Private Sub OpenSqlConnection()
Dim connectionString As String = Connection()
Using connection As New SqlConnection(connectionString)
connection.Open()
End Using
End Sub
Private Shared Function Connection() As SqlConnection
Dim ConHalloween As New SqlConnection
Dim sConnectionString As String = "Data Source=Doug\VSDOTNET;" _
& "Initial Catalog=Halloween;Integrated Security=SSPI"
ConHalloween.ConnectionString = sConnectionString
Return ConHalloween
End Function
Putting this in your web.config file
Code:
<connectionStrings>
<add name="oConn" connectionString="Data Source=Doug\VSDOTNET;Initial Catalog=Halloween;Integrated Security=SSPI"/>
</connectionStrings>
'You can then access it like this
Private Sub OpenSqlConnection()
Dim connectionString As String = Configurationmanager.AppSettings("oConn").ToString()
Using connection As New SqlConnection(connectionString)
connection.Open()
End Using
End Sub
I believe the script is for an sql server database. Unless you have Access installed on your machine, nothing can create it for you.
You might be able to download mySQL which I belive is a free database by Microsoft. You might be able to use that sql script with mySQL but im not 100% sure on that.
The connection String above is creating a sql connection and if you use a different database other than (I think just) SQL server, youll have to create your connnection a different way (Instead of using sqlconnection i belive it might odbconnection or something similar).
Ive figured it out, but not in the orthodox way, When I create a DB in Visual Studio it creates a .mdf and .ldf file in C:/program Files/SQL Server/MSSQl/Data
So I created a DB with the Same name and replaced the files, It registered the tables in VS now?
But would still be keen to figure out how its meant to be done.
Bookmarks