【问题标题】:How can I connect to MSSQL Express with ASP.NET如何使用 ASP.NET 连接到 MSSQL Express
【发布时间】:2014-08-26 06:18:13
【问题描述】:

我有一个应用程序 - 从 Visual Studio 2010 创建 - 我有一个专用服务器。服务器的操作系统是 windows server 2003。我的 web 应用程序在 Visual Studio 的本地主机和我的 IIS 本地主机上运行。但是当我将它发布到服务器时,它给了我这个错误。

Login failed for user 'sa'. 


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 'sa'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[SqlException (0x80131904): Login failed for user 'sa'.]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5066458
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
   System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +35
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) +183
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) +239
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +195
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +232
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +185
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +33
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +524
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +66
   System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +479
   System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +108
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +126
   System.Data.SqlClient.SqlConnection.Open() +125
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +123
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +319
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +94
   BerksanWebSite.Default2.Page_Load(Object sender, EventArgs e) in C:\Users\tekizer\documents\visual studio 2010\Projects\BerksanWebSite\BerksanWebSite\Default2.aspx.cs:94
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1022

这是我在 web.config 文件中的连接字符串标签

 <connectionStrings>
    <add name="websitesiConnectionString" connectionString="Server=xx.xxx.xxx.xxx; Database=websitesi;User ID=xxxx;Password=xxxxxx;"/>

  </connectionStrings>

我创建了一个类文件来调用 c# 代码的连接字符串。所以这是我在 c# 类中的 c# 连接变量。

private static string connectionstring_ = "Server=xx.xxx.xxx.xxx; Database=websitesi;User ID=xxxx;Password=xxxxxx;";

我从我的 c# 页面调用它来连接我在专用服务器上的数据库。

SqlConnection baglanti = new SqlConnection(berksanproje.connectionstring);
if(baglanti.State!=ConnectionState.Open)
{
baglanti.Open();
}

在我的专用服务器的 ASP.NET Web 应用程序属性中 ASP.NET 版本 4.0.30319 当我单击编辑配置并单击常规选项卡时,它会给出:

LocalSqlServer : Data Source=.\SQLEXPRESS;Initial Catalog=berksanwebsitesi;Integrated Security=True;

websitesiConnectionString : Server=xx.xxx.xxx.xxx; Database=websitesi;User ID=xxxx;Password=xxxxxx;

我可以通过 MSSQL 从我的计算机远程连接到我的数据库。但是我发布后无法连接到我的 aspx 页面。有什么方法可以将我的数据库与 asp.net 连接起来

注意:我没有写任何与用户 sa 联系的地方。我没用过。

【问题讨论】:

  • 看起来您的 ASP 页面尝试通过 sql server 身份验证(用户名 + 密码)进行连接,而您的计算机则通过 Windows 身份验证(集成安全性)进行连接。那么,SQL Server 是否配置为能够使用 sql server 身份验证(即“混合模式”)? msdn.microsoft.com/library/ms144284.aspx——另外,如果你有一个命名实例,你可能需要指定 (Server=xx.xxx.xxx.xx\InstanceName)
  • 是的。我可以从我的电脑用 IP 连接。 1433端口打开
  • 服务中是否运行 SQL Server??
  • 是的。 - 我在上面写了 - 。它通过 Visual Studio 的 localhost 在我的计算机上运行。数据库在专用服务器上。

标签: c# asp.net .net sql-server


【解决方案1】:

我将 windows server 2008 安装到我的专用服务器上。然后在使用 Management Studio 安装 .NET Framework 4.0 和 MSSQL Server 2008 R2 Express 之后,我的系统就可以正常工作了。

【讨论】:

    【解决方案2】:

    看来你必须使用windows认证或者你没有sa用户,

    我假设您使用 Windows 身份验证进行连接,因此将所有连接字符串更改如下:

    Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
    

    请参阅此link 了解更多详细信息,您的情况可能不需要 Windows 身份验证,因此请检查链接并找到适合您的连接字符串

    【讨论】:

    • 你有 Sql server 'sa' 用户吗?当你连接到你的 sql server 实例时,你使用 windows 身份验证吗??
    • 我有一个用户。但我没有在任何地方使用它。当我尝试使用 sa 时,那里没有任何变化。我不知道我该如何解决这个问题
    • @enderariç 你可以使用 sql server management studio 连接到你的 sql server 使用用户 sa???
    • 是的,我可以使用用户 sa 连接到 sql server management studio
    • 我建议像 Data Source=myServerAddress;Initial Catalog=websitesi; User ID=sa;Password=yourpassword; 这样替换所有连接字符串。我假设您的密码是 yourpassword
    猜你喜欢
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    相关资源
    最近更新 更多