尝试在另一台计算机上启动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
结束课程
答案 0 :(得分:1)
答案 1 :(得分:0)
取决于您的sqlserver版本。
检查您的服务器名称,只需打开cmd并输入SQLCMD -L
。
检查您的服务器实例名称sqllocaldb i
。
在sql server express 2012中,实例名称通常为(LocalDB)\v11.0
。