2013-03-31 87 views
0

我正在阅读一本教自己ASP.NET的书。到目前为止,它很好,甚至在SQL Developer中的SQL,但我无法从外部应用程序连接。ASP.NET中Microsoft SQL Server 2008 R2的连接字符串

我正在使用Visual Studio 2010 Pro SP1,SQL Server 2008 R2标准和使用IIS 7.5的Windows 7 Professional SP1。我在ASP.NET中使用的语言是VB.NET但很高兴为C#答案。

我永远无法得到的连接字符串工作

connectionString="Server=ASUS-P5Q\MSSQLSERVER;Database=Dorknozzle; User Id=xxxxxxxxx; Password=xxxxxxxxx;" 

我总是碰到下面的错误

Server Error in '/' Application.
Login failed for user 'ASUS-P5Q\xxxxxx'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'ASUS-P5Q\xxxxxxx'.

Source Error:

Line 17: conn.Open()**

我关闭用户帐户控制,防病毒和防火墙,但没有作品:-(

+0

为什么'华硕P5Q \ MSSQLSERVER'?你尝试过简单的'localhost:port'吗? (因为我猜你试图从同一台机器连接) – walther

+0

你可以使用Sql Server Management Studio连接到该服务器/数据库/用户/传递组合吗?或者使用命令行获得结果:'sqlcmd -S ASUS-P5Q \ MSSQLSERVER -d Dorknozzle -U xxx -P xxx -Q“select * from table”'? –

+0

您是否安装了带有特定命名实例的SQL Server - 或者只是标准的默认实例?如果你使用了默认的实例,那么你应该可以在你的连接字符串中将'.','(local)'或'ASUS-P5Q'作为实例名称。不需要'MSSQLSERVER' ....所以试试看:'server = ASUS-P5Q; Database = ....'看看是否有效 –

回答

0

由于@MTAdmin注意到,你似乎使用集成身份验证..

使用此方法时,只能使用登录名作为凭据,因此请确保您的应用程序池以该用户身份运行(本例中为'ASUS-P5Q \ xxxxxx')并使用以下连接字符串。

Server=ASUS-P5Q\MSSQLSERVER;Database=Dorknozzle;Integrated Security=SSPI;

以供将来参考,我建议使用ConnectionStrings.com当你忘了,偷偷摸摸的连接字符串,因为我总是这样。

0

错误消息似乎表明您正在使用集成身份验证( Windows用户名和密码)。有很多原因可能导致失败。

为了简单起见,您也可以尝试使用Sql身份验证:在Sql中创建一个用户并在您的连接字符串中指定用户名和密码。我通常从一个Sql用户名和密码开始,然后在部署之前决定我的最终身份验证方法。

0

要连接到本地数据库引擎,您可以使用\语法为:\ SQL2008:

connectionString="Server=.\MSSQLSERVER;Database=Dorknozzle; User Id=xxxxxxxxx; Password=xxxxxxxxx;Integrated Security=false;" 
0

我如何解决我的问题是我在SQL Management Studio下创建了一个用户,并使用SQL Server用户身份验证。这是最终为我工作的串...

的connectionString =“数据源=华硕P5Q \ MSSQLSERVER,1433;初始目录= Dorknozzle;集成安全性= FALSE;用户ID = SQLDT;密码=# ###” 的providerName = “System.Data.SqlClient的”/>

谢谢你们的帮助:)

0
using System; 
using System.Collections.Generic; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Text; 

namespace User_Management 
{ 
    class DatabaseConnection 
    { 
     public string GetConnectionString() 
     { 
      return @"server=AFORE\MSSQLSERVER; Integrated Security=SSPI; Database = prictice;"; 
     } 

     public SqlConnection GetConnectionObj() 
     { 
      SqlConnection connectionObj=new SqlConnection(GetConnectionString()); 
      connectionObj.Open(); 
      return connectionObj; 
     } 
     public void ExecuteSqlCommandAndCloseConnection(string insertQueryString, SqlConnection connectionObj) 
     { 
      SqlCommand sqlCommandObj = new SqlCommand(insertQueryString, connectionObj); 
      sqlCommandObj.ExecuteNonQuery(); 
      connectionObj.Close(); 
     } 
     public void ExecuteSqlCommandOnly(string insertQueryString, SqlConnection connectionObj) 
     { 
      SqlCommand sqlCommandObj = new SqlCommand(insertQueryString, connectionObj); 
      sqlCommandObj.ExecuteNonQuery(); 

     } 
    } 
} 
相关问题