VB connect to sql server

calnastic
09-09-2011, 10:14 AM
Hi I'm new to VB, trying to learn how to connect to sql server.
When running the code below I got this message from visual studio:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Imports System.Data.SqlClient

Public Class _Default
Inherits System.Web.UI.Page

Dim insertQuery As String
Dim updateQuery As String
Dim deleteQuery As String


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Dim DBC As SqlConnection = New SqlConnection("Data source=C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\tempdb.mdf")

insertQuery = "INSERT INTO contact (name, number) VALUES([TextBox1.Text],[TextBox2.Text] )"
Dim myCommand As New SqlCommand(insertQuery)
myCommand.Connection = DBC
myCommand.Connection.Open()
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()

End Sub
End Class

The error points to "myCommand.Connection.Open()"
The sqlexpress server is running.
How can I fix it? Thank you.

PlausiblyDamp
09-09-2011, 01:25 PM
SQL Server connection strings aren't just a file name, http://www.connectionstrings.com/ is well worth a look for how one should be structured.

It looks as though you are trying to connect directly to tempDB as well, this is also a fairly unusual thing to do as well, typically you would connect to a named database and prefix things with a # character to refer to / create things in the tempDB

calnastic
09-09-2011, 02:53 PM
SQL Server connection strings aren't just a file name, http://www.connectionstrings.com/ is well worth a look for how one should be structured.

It looks as though you are trying to connect directly to tempDB as well, this is also a fairly unusual thing to do as well, typically you would connect to a named database and prefix things with a # character to refer to / create things in the tempDB

Thanks, I have modified the connection string as below:
Dim DBC As SqlConnection = New SqlConnection("Data source=lovegtt-pc\sqlexpress;Persist Security Info=True;Integrated Security=SSPI;Initial Catalog=tempdb")

insertQuery = "INSERT INTO dbo.contact (name, number) VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "' )"
Dim myCommand As New SqlCommand(insertQuery)
myCommand.Connection = DBC
myCommand.Connection.Open()
myCommand.ExecuteNonQuery() 'Line 27
myCommand.Connection.Close()

The Visual Studio no longer complains about the connection, but when it execute the insert command at line 27, I get this new error:
Invalid object name 'dbo.contact'.
I tried using the table name "contact" without the "dbo" prefix, but it doesn't work either. Any suggestion?

PlausiblyDamp
09-10-2011, 02:39 AM
Is there a table called contact in the tempDB database? If so are you really sure as this is unusual to say the least. Tempdb is a temporary database, sql server uses it to store temporary tables and similar - it isn't normally used for user tables (for starters everytime sql restarts it wipes tempdb clean anyway)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum