sqlConnetion()失败,其参数使.udl文件成功连接 [英] sqlConnetion() failed with parameters which make .udl file succeed to connect

查看:82
本文介绍了sqlConnetion()失败,其参数使.udl文件成功连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows Server 2008上设置了一个SQL Server Express并创建了一些数据库&桌子就可以了。我使用.udl文件来远程测试与它的连接并且它是成功的:

在.udl文件中,显示的SQL服务器是< my server => / SQLEXPRESS,用户名&登录服务器的密码是:sa& < my password =>。



然后,我写了一个C#程序来做连接。第一个连接失败,但其参数完全与.udl连接中使用的参数:



 SqlConnection conn =  new  SqlConnection( 数据源= STAN-MTP-SQL \\ SQLEXPRESS; Database = MTP_TESTDB1; Integrated Security = true; User Id = sa; Password =<我的密码>); 
尝试 {conn.Open(); }





这种连接方式出现在网络上的大量示例程序中。但它失败了。错误消息是:ClientConnectionId:af804f7a-8485-4cc3-babb-le3b32546c92异常捕获。



所以,这两种方式(.udl& C#程序)使用相同的参数远程连接。测试情况是一样的。怎么来.udl成功但是C#程序失败了?

解决方案

它的工作原理:SqlConnection conn = new SqlConnection(@Server = 192.168.8.98\SQLEXPRESS; Database = MTP_TESTDB1 ;用户ID = sa;密码= stan);



我错误地在192.168.8.98之前添加了\\


< blockquote>这也有效,我认为它更好:

SqlConnection conn = new SqlConnection(@Server = STAN-MTP-SQL \ SQLEXPRESS; Initial Catalog = MTP_TESTDB1; User Id = sa; Password = stan);



这不起作用:

SqlConnection conn = new SqlConnection(@Server = STAN-MTP-SQL \\ \\ _SQLEXPRESS;初始目录= MTP_TESTDB1;集成安全性=真);


I set up a SQL Server Express on Windows Server 2008 and created some database & tables on it. I used a .udl file to test connectivity to it remotely and it's successful:
In .udl file, the SQL server shown is <my server="">/SQLEXPRESS, user name & password to login to server are: sa & <my password="">.

Then, I wrote a C# program to do connection. The first connection fails, but its parameters are totally as the ones used in .udl connectivity:

SqlConnection conn = new SqlConnection("Data Source=STAN-MTP-SQL\\SQLEXPRESS; Database=MTP_TESTDB1;Integrated Security=true;User Id=sa;Password=<My password>");
try { conn.Open(); }



This kind of connection way appears at lots of sample programs in web. But it failed. The error message is: "ClientConnectionId:af804f7a-8485-4cc3-babb-le3b32546c92 Exception caught."

So, the two ways (.udl & C# program) used the same parameters to connect remotely. The test situation are the same. How come .udl succeeded but C# program failed?

解决方案

It works: SqlConnection conn = new SqlConnection(@"Server=192.168.8.98\SQLEXPRESS; Database=MTP_TESTDB1;User Id=sa;Password=stan");

I mistakenly added "\\" before 192.168.8.98


This works too and I think it's better:
SqlConnection conn = new SqlConnection(@"Server=STAN-MTP-SQL\SQLEXPRESS; Initial Catalog=MTP_TESTDB1;User Id=sa;Password=stan");

This doesn't work:
SqlConnection conn = new SqlConnection(@"Server=STAN-MTP-SQL\SQLEXPRESS; Initial Catalog=MTP_TESTDB1;Integrated Security=true");


这篇关于sqlConnetion()失败,其参数使.udl文件成功连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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