2011-01-20 127 views
1

我一直在其他网站尝试和搜索数千次,但从来没有找到一个示例或简单的代码来使用。TCP/IP连接

我创建它使用ODBC连接,我也安装了MS SQL和配置为启用远程数据库信息共享的应用程序C#。我想使我的数据库可用于每个人使用我通过使用连接所做的这个应用程序。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace CaseMatter 
{ 

public partial class mainLog : Form 
{ 
    string userID, userName, userAddress, userFName, userLastName, userCity, userPassword, userTele; 
    public mainLog() 
    { 
     InitializeComponent(); 
     this.Size = new Size(442, 162); 

    } 


    private void Submitbtn_Click(object sender, EventArgs e) 
    { 

      string ConnectionString = 
       "Data Source=xx.xx.xx.xx,1433;Initial Catalog=master;Integrated Security=SSPI;"; 

      var conn = new SqlConnection(ConnectionString); 
      conn.Open(); 

      var strSQLCommand = "SELECT Name FROM example WHERE id='1'"; 
      var command = new SqlCommand(strSQLCommand, conn); 
      var reader = command.ExecuteReader(); 

      while (reader.Read()) 
      { 
       passwordBox.Text = reader.GetString(0); 
      } 

      reader.Close(); 
      conn.Close(); 

    } 
} 

}

我刚才编辑的代码,试了一下,我添加了一个尝试catch来处理SQL异常,但是当我点击提交按钮,它仍然冻结。

希望有人知道这一点。 错误: “与SQL Server建立连接时发生网络相关或实例特定错误未找到服务器或无法访问服务器验证实例名称是否正确,并且SQL Server配置为允许远程连接(provider:TCP提供程序,error:0 - 无连接可以作出,因为目标机器积极地拒绝它。)”

+1

请发布您收到的确切例外。看起来可能是一个安全问题,因为你没有指定登录凭证。 – 2011-01-20 17:12:37

+0

在编译该项目之前我没有收到任何错误或者这样的问题,但是在编译之后,框架冻结了 – Ivan 2011-01-20 17:15:47

+1

目前还不清楚发生了什么问题。你得到一个例外,不好的数据等... – JaredPar 2011-01-20 17:16:05

回答

1

在你的榜样,你都没有通过用户ID和密码。那是因为你不想包括你的凭证?我认为不是因为你正确地XXX出你的IP地址。我将首先提供可以访问数据库的SQL服务器帐户的凭据。

0

我注意到您使用SqlConnection这不是一个ODBC连接对象。用SqlConnection实际上你是通过ADO.NET连接的。为SqlConnection(ODBC和ADO.NET两者)的连接字符串语法可在以下网站上找到:

http://connectionstrings.com

顺便说一句,如果你连接到SQL 2005,连接字符串会是什么样子这个:

using System.Data.SqlClient; 

// trusted connection (SQL configured to use your windows ID) 
string ConnectionString = 
    "Data Source=xxxxxxx;Initial Catalog=xxxxxx;Integrated Security=SSPI;" 

// sql authentication (SQL manages its own users/pwds) 
//string ConnectionString = 
    //"Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;" 

var conn = new SqlConnection(ConnectionString); 
conn.Open(); 

var strSQLCommand = "SELECT Name from [example_autoincrement] where id='1'";  
var command = new SqlCommand(strSQLCommand, conn); 
var reader = command.ExecuteReader(); 

while (reader.Read()) 
{ 
    passwordBox.Text = reader.GetString(0); 
} 

reader.Close(); 
conn.Close(); 
0

好吧,这是一个奇怪的检查,但只是确保客户端和服务器上的时间和日期是准确的。 SSPI需要每个间隔5分钟的最大差距

2

我怀疑这里有四件事情中的一件发生在这里。

  1. SQL Server配置错误,未响应tcp连接。解决此问题的最简单方法是使用SQL Management Studio进行连接。如果你不能以这种方式连接,那么你必须看看sql服务器的服务设置。另外,请确保SQL浏览器服务正在服务器上运行。

  2. 的SQL Server配置为使用不同的TCP端口比你想的人。从连接字符串中删除“,1433”,然后重试。 SQL浏览器应该回应sql server正在侦听的实际端口。

  3. SQL Server有防火墙阻止远程连接。暂时关闭防火墙并查看是否可以连接。如果可以,请正确配置防火墙并重新打开。

  4. 本机上有某种类型的防火墙阻止该端口上的传出连接。尝试关掉你的看看是否有帮助。如果有,请正确配置并重新打开。


如果是这样,没有人远程连接到一个全新的SQL Server那么它很可能SQL服务器或Windows配置的配置中是完全。