无法连接到SQL Server 2005数据库 [英] unable to connect to sql server 2005 database

查看:89
本文介绍了无法连接到SQL Server 2005数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法连接到SQL SERVER数据库.

机器设置:
Windows 7
VS2005标准
SQLEXPRESS 2005

我的代码:

I am unable to to connect to the SQL SERVER database.

The machine setup:
Windows 7
VS2005 Standard
SQLEXPRESS 2005

My code :

Imports System.Data
Imports System.Data.SqlClient
Public Class Dosen
    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim reader As SqlDataReader
    Dim constring As String
    Dim SqlString As String
    Dim numAffected As Integer

    Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Clic
        SqlString = "Insert Into Dosen values (''" & id.Text.Trim & "'',''" & nama.Text.Trim & ")"
        cmd = New SqlCommand(SqlString, con)
        numAffected = cmd.ExecuteNonQuery
        con.Close()
        MessageBox.Show("Data dosen berhasil disimpan", "SUKSES", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub Dosen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''TODO: This line of code loads data into the ''_FKIP_UNILAKDataSet.Dosen'' table. You can move, or remove it, as needed.
        Me.DosenTableAdapter.Fill(Me._FKIP_UNILAKDataSet.Dosen)
        constring = "Data source=ragaz-pc\sqlexpress.FKIP-UNILAK.dbo;Initial catalog=FKIP-UNILAK; Integrated security=true"
        con = New SqlConnection(constring)
        con.Open()
    End Sub
End Class


错误消息是:
建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称正确,并且已将SQL Server配置为允许远程连接. (提供者:SQL网络接口,错误:26-指定服务器/实例时出错)

[edit]标记,添加的代码块,忽略文本中的HTML"选项已禁用-OriginalGriff [/edit]


The error message was :
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)

[edit]Tags, Code block added, "Ignore HTML in text" option disabled - OriginalGriff[/edit]

推荐答案

首先,不要进行连接方式:在需要时创建并打开新连接,完成后立即关闭并处置该连接.连接是宝贵的稀缺资源,不应捆绑在一起,以等待用户按下按钮!

其次,检查您的连接字符串:我认为您不需要数据源中的".FKIP-UNILAK.dbo".在VS中的服务器资源管理器中查找-右键单击数据库,然后选择属性".您需要的详细信息应该位于连接字符串"下的属性"窗格的顶部.
Firstly, don''t do your connection that way: Create and open a new connection when you need it, close and dispose the connection immediately you have finished. Connections are valuable and scarce resources, which should not be tied up waiting for your user to press a button!

Secondly, check your connection string: I don''t think you need the ".FKIP-UNILAK.dbo" in the data source. Look in the Server explorer in VS - right click the DB and select "properties". The details you need should be at the top of the Properties pane under "Connection String"


可能是外围应用配置错误或sql服务配置问题.
请查看此线程的潜在原因和解决方法: ^ ]

如果需要,请在此处参见设置外围应用配置的步骤. .

如果这不能解决您的问题,请查看以下线程:
http://forums.asp.net/t/1334649.aspx [ SQL服务器修复错误(提供者:命名管道 [
It might be surface area configuration error or sql service configuration issue.
Have a look at this thread for potential reasons and resolutions: Resolving A network-related or instance-specific error occurred while establishing a connection to SQL Server[^]

If needed, See steps for setting up Surface area configuration here..

If this does not resolve your issue, have a look at these thread:
http://forums.asp.net/t/1334649.aspx[^]
SQL SERVER FIX ERROR (provider: Named Pipes [^]
Quite few possible reasons and resolutions are proposed here too.


Dim tbl As New DataTable
Private dvw As DataView
Private b As Boolean
Private SqlCon As New System.Data.SqlClient.SqlConnection("server=Morteza-PC\sql2005;user id=sa;pwd=sqlmaster;data source=Morteza-PC\sql2005;database=ParsContract")
Private cmd As New SqlClient.SqlCommand("SELECT * FROM Personalinfo", SQL.sqlcon)

SQL.sqlcon.Open()
Dim sdr As SqlClient.SqlDataReader = cmd.ExecuteReader
Dim fc As Integer
While (sdr.Read)
    'populating columns
    If Not b Then
        For fc = 0 To sdr.FieldCount - 1
            tbl.Columns.Add(sdr.GetName(fc))
        Next
        b = True
    End If
    'populating rows
    Dim row As DataRow = tbl.NewRow
    For fc = 0 To sdr.FieldCount - 1
        row(fc) = sdr(fc)
    Next
    tbl.Rows.Add(row)
End While
dvw = New DataView(tbl)
Me.dgrPersonalInfo.DataSource = dvw
SQL.sqlcon.Close()


这篇关于无法连接到SQL Server 2005数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆