无法打开与SQL Server的连接 [英] Could not open a connection to SQL Server

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

问题描述

这是我每次单击注册按钮时都会遇到的异常

This is the exception that i get every time when i click the register button

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server). The number of line in which i get error is line 107 (con.Open).

代码如下:



The code is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Collections.Specialized;
using System.Data;
using System.Text.RegularExpressions;
namespace Sup
{
   /// <summary>
   /// Interaction logic for Signup.xaml
   /// </summary>
   public partial class Signup : Window
   {
      public Signup()
      {
          InitializeComponent();
      }
      private void Reset()
      {
         textBoxAddress.Text = "";
         textBoxEmail.Text = "";
         textBoxFirstName.Text = "";
         textBoxLastName.Text = "";
         passwordBox1.Password = "";
         passwordBoxConfirm.Password = "";
      }
      private void registerButton_Click(object sender, RoutedEventArgs e)
      {
         if (textBoxEmail.Text.Length == 0)
         {
            MessageBox.Show("Enter an email.");
            textBoxEmail.Focus();
         }

         else if (!Regex.IsMatch(textBoxEmail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
         {
            MessageBox.Show("Enter a valid email.");
            textBoxEmail.Select(0, textBoxEmail.Text.Length);
            textBoxEmail.Focus();
         }
         else
         {
            string firstname = textBoxFirstName.Text;
            string lastname = textBoxLastName.Text;
            string email = textBoxEmail.Text;
            string password = passwordBox1.Password;

            if (passwordBox1.Password.Length == 0)
            {
               MessageBox.Show("Enter password.");
               passwordBox1.Focus();
            }
            else if (passwordBoxConfirm.Password.Length == 0)
            {
               MessageBox.Show("Enter Confirm password.");
               passwordBoxConfirm.Focus();
            }
            else if (passwordBox1.Password != passwordBoxConfirm.Password)
            {
               MessageBox.Show("Confirm password must be same as password.");
               passwordBoxConfirm.Focus();
            }
            else
            {
               string address = textBoxAddress.Text;
               SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=logins;Integrated Security=SSPI");

               con.Open();

               SqlCommand cmd = new SqlCommand("Insert into Registration (FirstName,LastName,Email,Password,Address) values('" + firstname + "','" + lastname + "','" + email + "','" + password + "','" + address + "')", con);

               cmd.CommandType = CommandType.Text;
               cmd.ExecuteNonQuery();
               con.Close();
               MessageBox.Show("You have Registered successfully.");
               Reset();
            }
         }
      }
   }
}





[edit] indexation reduced [/ edit]



[edit]indexation reduced[/edit]

推荐答案

))
{
MessageBox.Show( 输入有效的电子邮件。);
textBoxEmail .Select( 0 ,textBoxEmail.Text.Length);
textBoxEmail.Focus();
}
else
{
string firstname = textBoxFirstName.Text;
string lastname = textBoxLastName.Text;
string email = textBoxEmail.Text;
string password = passwordBox1.Password;

if (passwordBox1.Password.Length == 0
{
MessageBox.Show( 输入密码。);
passwordBox1.Focus();
}
else if (passwordBoxConfirm.Password.Length == 0
{
MessageBox.Show( 输入确认密码。);
passwordBoxConfirm.Focus();
}
else if (passwordBox1.Password!= passwordBoxConfirm.Password)
{
MessageBox.Show( 确认密码必须与密码相同。 );
passwordBoxConfirm.Focus();
}
else
{
string address = textBoxAddress.Text;
SqlConnection con = new SqlConnection( 数据Source =(local); Initial Catalog = logins; Integrated Security = SSPI);

con.Open();

SqlCommand cmd = new SqlCommand( 插入注册(FirstName,LastName,电子邮件,密码,地址)值(' + firstname + ',' + lastname + ',' + email + ',' +密码+ ',' +地址+ ') ,con);

cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show( 您已成功注册。);
重置();
}
}
}
}
}
")) { MessageBox.Show("Enter a valid email."); textBoxEmail.Select(0, textBoxEmail.Text.Length); textBoxEmail.Focus(); } else { string firstname = textBoxFirstName.Text; string lastname = textBoxLastName.Text; string email = textBoxEmail.Text; string password = passwordBox1.Password; if (passwordBox1.Password.Length == 0) { MessageBox.Show("Enter password."); passwordBox1.Focus(); } else if (passwordBoxConfirm.Password.Length == 0) { MessageBox.Show("Enter Confirm password."); passwordBoxConfirm.Focus(); } else if (passwordBox1.Password != passwordBoxConfirm.Password) { MessageBox.Show("Confirm password must be same as password."); passwordBoxConfirm.Focus(); } else { string address = textBoxAddress.Text; SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=logins;Integrated Security=SSPI"); con.Open(); SqlCommand cmd = new SqlCommand("Insert into Registration (FirstName,LastName,Email,Password,Address) values('" + firstname + "','" + lastname + "','" + email + "','" + password + "','" + address + "')", con); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("You have Registered successfully."); Reset(); } } } } }





索引减少[/ edit]



[edit]indexation reduced[/edit]


几乎可以肯定,这是你的连接字符串 - 对我来说看起来很奇怪!



尝试使用Server Explorer窗格在VS中设置连接:

1)打开服务器资源管理器。

2)右单击数据连接并选择添加连接

3)在随后的对话框中,选择您的数据源和数据库,指定安全信息,然后按测试连接按钮。 />
4)当连接工作时,按确定

5)在服务器资源管理器窗格中突出显示数据库,然后查看属性窗格。将显示连接字符串的工作示例,您可以将其复制并粘贴到您的应用程序或配置文件中。



并且不要硬编码连接strings:它意味着当你发布软件时,你必须为每个服务器重新编译它....
Almost certainly, it's your connection string - it looks odd to me!

Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.

And don't "hard-code" connections strings: it means when you release the software, you have to re-compile it for each server....


基本上,当您无法连接到SQL Server时,问题可能是:

1)网络问题,

2)SQL Server配置问题。

3)防火墙问题,

4)客户端驱动程序问题,

5)应用程序配置问题。

6)身份验证和登录问题。

Basically, when you failed to connect to your SQL Server, the issue could be:
1) Network issue,
2) SQL Server configuration issue.
3) Firewall issue,
4) Client driver issue,
5) Application configuration issue.
6) Authentication and logon issue.



基于错误消息你没有允许远程连接到那台机器,所以更改设置中的选项



查看这些

< a href =http://blogs.msdn.com/b/sql_protocols/archive/2008/04/30/steps-to-troubleshoot-connectivity-issues.aspx>解决SQL连接问题的步骤 [ ^ ]

SQL Server 2005连接问题疑难解答 - 第一部分 [ ^ ]

解决SQL Server 2005中的连接问题 - 第二部分 [ ^ ]

解决SQL Server 2005中的连接问题 - 第III部分 [< a href =http://blogs.msdn.com/b/sql_protocols/archive/2005/12/22/506607.aspxtarget =_ blanktitle =New Window> ^ ]



BTW检查这一堆也是为了将来的错误


Based on the error message you didn't allow the remote connection to that machine so change the options in settings.

Check these
Steps to troubleshoot SQL connectivity issues[^]
SQL Server 2005 Connectivity Issue Troubleshoot - Part I[^]
Troubleshoot Connectivity Issue in SQL Server 2005 - Part II[^]
Troubleshoot Connectivity Issue in SQL Server 2005 - Part III[^]

BTW check this bunch also for future errors


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

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