本文介绍了无法连接到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]

推荐答案



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数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 09:24
查看更多