为什么我会收到此错误消息? "An error has occurred while establishing a connection to the server"



当我试图在另一台机器上启动我的windows应用程序时,遇到了以下错误。

"在建立与SQL Server的连接。找不到服务器或无法访问。验证实例名称是否正确,以及SQL Server已配置为允许远程连接。(提供者:已命名管道提供程序,错误:40-无法打开与SQL的连接服务器)。">

在我的机器中,应用程序正在工作,但在用户的机器中它不工作。我尝试了很多解决方案,无论是在stackoverflow还是其他网站,都没有帮助我解决这个问题。数据库是本地的,所以我没有使用任何服务器或类似的东西。奇怪的是,登录表单实际上是有效的,但其他表单不起作用。这是来自其中一个表单的代码,这些表单无法使想法更接近。我希望你能帮我解决这个问题,因为我真的没有时间了。我使用的是VB.NET和SQL SERVER。

Imports System.Data
Imports System.Data.SqlClient
Public Class Form13
'Dim cmd As New SqlCommand
Dim cn As New SqlConnection("database=Leave_manager;server=(local);integrated security=true")
Private Sub Form13_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: cette ligne de code charge les données dans la table 'Leave_managerDataSet1.Addemployees'. Vous pouvez la déplacer ou la supprimer selon vos besoins.
Me.AddemployeesTableAdapter.Fill(Me.Leave_managerDataSet1.Addemployees)
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
cn.Open()
Dim sqlcmd As New SqlCommand("select Fname,Lname from Addemployees where ID = '" & ComboBox1.Text & "'", cn)
Dim myreader As SqlDataReader
myreader = sqlcmd.ExecuteReader()
myreader.Read()
If myreader.HasRows Then
TextBox2.Text = myreader.Item("Fname").ToString
TextBox1.Text = myreader.Item("Lname").ToString
End If
cn.Close()
Catch ex As Exception
MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
Finally
cn.Close()
End Try
Try
cn.Open()
Dim sqlcmd As New SqlCommand("SELECT Leave_num FROM Addemployees WHERE ID='" & ComboBox1.Text & "'", cn)
Dim myreader1 As SqlDataReader
myreader1 = sqlcmd.ExecuteReader()
myreader1.Read()
If myreader1.HasRows Then
TextBox6.Text = myreader1.Item("Leave_num").ToString
End If
cn.Close()
Catch
MessageBox.Show("Error!", "exit", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox6.Clear()
Me.CheckBox1.Checked = False
Me.Hide()
Form6.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If MessageBox.Show("Do you really want to Restore is record?", "Record", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
Try
cn.Open()
Dim cmd As New System.Data.SqlClient.SqlCommand
Dim RA As Integer
cmd = New SqlCommand("update Addemployees set Leave_num ='" & TextBox3.Text & "'  where ID='" & ComboBox1.Text & "' ", cn)
cmd.Connection = cn
RA = cmd.ExecuteNonQuery()
MessageBox.Show("Process successful!", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
cn.Close()
Catch
MessageBox.Show("Error!", "exit", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If MessageBox.Show("Do you really want to Restore all records?", "Record", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
If Me.TextBox4.ReadOnly = False Then
Try
cn.Open()
Dim cmd2 As New System.Data.SqlClient.SqlCommand
Dim Rb As Integer
cmd2 = New SqlCommand("update Addemployees set Leave_num ='" & TextBox4.Text & "'", cn)
cmd2.Connection = cn
Rb = cmd2.ExecuteNonQuery()
MessageBox.Show("Process successful!", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
cn.Close()
Catch
MessageBox.Show("Error!", "exit", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
ElseIf Me.TextBox4.ReadOnly = True Then
MessageBox.Show("Error!, please select selectr all option", "exit", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox6.Clear()
Me.CheckBox1.Checked = False
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If Me.CheckBox1.Checked = True Then
Me.TextBox4.ReadOnly = False
ElseIf Me.CheckBox1.Checked = False Then
Me.TextBox4.ReadOnly = True
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub

结束类`

  1. 错误-"(提供程序:命名管道提供程序,错误:40-无法打开到SQL Server的连接)";指出了SQL Server部分的配置错误。请检查并确保服务器接受通过命名管道的连接
  2. "在我的机器中,应用程序正在工作,但在用户的机器中它不工作"。。。。。。a) 请检查防火墙是否可以访问端口1433(或任何其他定义的端口),b) 请转到用户计算机上的服务,重新启动"SQL Server"c) 在服务本身中,如果需要,请重新启动"Windows防火墙">
  3. "当用户输入密码和用户名时,应用程序就会工作,他会转到下一个出现问题的表单"。。似乎您的每个表单都需要身份验证
  4. 我建议你从用户的机器上取下数据库,转移到你的机器上,提供对用户机器的访问,并从用户机器上进行测试。您需要创建一个SQL服务器用户(而不是作为远程机器的windows用户)并对其进行测试。我认为这应该解决数据库连接问题

取决于您的sqlserver版本。检查您的服务器名称,只需打开cmd并键入SQLCMD -L。请检查您的服务器实例名称sqllocaldb i

在sql server express 2012中,实例名称通常为(LocalDB)v11.0

相关内容

  • 没有找到相关文章

最新更新