我正在做一个连接到同一网络中的数据库的应用程序。DBMS是Server 2005,我想测试到ip入口的ping
,然后通过ado.net测试到数据库的连接:
Ping p = new Ping();
try
{
PingReply pingReply6 = p.Send(ip.Text);
MessageBox.Show("IP: " + pingReply6.Address + " \n Etat : " + pingReply6.Status);
string connexionString;
connexionString = @"Data Source=Owner;Initial Catalog=base;Integrated Security=true;";
IDbConnection connexion = new SqlConnection(connexionString);
try { connexion.Open(); MessageBox.Show(" the connection is established "); }
catch(Exception ex) { MessageBox.Show(ex.ToString()); }
}
catch { MessageBox.Show("Invalid IP"); }
如您所见,我想通过adress ip连接到数据库,我测试ping,它可以工作,但是当我在服务器中测试到数据库的连接时,它不能工作。
发布于 2013-04-15 10:32:20
像这样的东西可能会有帮助
connexionString = @"Data Source=" + pingReply6.Address + "\Owner;Initial Catalog=base;Integrated Security=true;";
在上面的.
中,连接字符串表示localhost。
正如@RemusRusanu所说,IP地址被认为是糟糕的形式,然而,上述功能将发挥作用。主机名会更好
发布于 2013-04-15 10:34:31
使用SqlConnectionStringBuilder
。将DataSource
属性分配给要连接到的服务器名。当您想要打开连接时,从构建器中提取ConnectionString
。这保证了正确格式化的连接字符串。
一般来说,使用IP是一种不好的做法。用电脑的名字。
对ping的测试与连接无关。SQL连接可以使用与TCP/IP无关的协议(例如。共享内存)。取决于各种corpnet ICMP设置,Ping可能会失败。Ping可以成功,但是基于防火墙和IPSec规则,连接可能会失败。TCP/IP连接不能保证T连接。
https://stackoverflow.com/questions/16012897
复制相似问题