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.
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.