当前位置:编程学习 > C#/ASP.NET >>

C# 连接数据库问题

 用户代码未处理 System.Data.SqlClient.SqlException
  Message="无法打开登录所请求的数据库 \"ShoppingManagerSystem\"。登录失败。\r\n用户 'XP-201110181322\\Administrator' 登录失败。"
  Source=".Net SqlClient Data Provider"
  ErrorCode=-2146232060
  Class=11
  LineNumber=65536
  Number=4060
  Procedure=""
  Server="\\\\.\\pipe\\CB9A9560-7EBF-49\\tsql\\query"
  State=1
  StackTrace:
       在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
       在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
       在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
       在 System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
       在 System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
       在 System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
       在 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
       在 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
       在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
       在 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
       在 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
       在 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
       在 System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
       在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
       在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
       在 System.Data.SqlClient.SqlConnection.Open()
       在 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       在 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
       在 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
       在 WebShop.Default.Bind_DisCount_Goods() 位置 D:\My Documents\Visual Studio 2008\Projects\nick\WebShop\Default.aspx.cs:行号 32
       在 WebShop.Default.Page_Load(Object sender, EventArgs e) 位置 D:\My Documents\Visual Studio 2008\Projects\nick\WebShop\Default.aspx.cs:行号 19
       在 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       在 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       在 System.Web.UI.Control.OnLoad(EventArgs e)
       在 System.Web.UI.Control.LoadRecursive()
       在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 
--------------------编程问答-------------------- 他说你的连接字符串有问题 --------------------编程问答-------------------- 数据库连接字符串有问题。
http://www.connectionstrings.com/ --------------------编程问答-------------------- 本地的数据库这样写连接呀 是用window登陆的 我忘了数据库的用户与密码    --------------------编程问答-------------------- 这样写  <add name="MyCon" connectionString="Data Source=.\sqlexpress;Initial Catalog=ShoppingManagerSystem;Integrated Security=True;" providerName="System.Data.SqlClient"/> 还是报错 --------------------编程问答-------------------- <add name="ConnectionString" connectionString="Password=密码;Persist Security Info=True;User ID=sa;Initial Catalog=数据库;Data Source=服务器" providerName="System.Data.SqlClient" />

用sa和密码登陆下试试呢? --------------------编程问答-------------------- <add key="ConnStr" value="Data Source=(local);Database=StudentInfo;Uid=sa;Pwd=密码5354"></add> --------------------编程问答-------------------- <add name="MyCon" connectionString="Data Source=.;Initial Catalog=ShoppingManagerSystem;Integrated Security=True;" providerName="System.Data.SqlClient"/>试试看 --------------------编程问答--------------------
引用 3 楼 nick1yong 的回复:
本地的数据库这样写连接呀 是用window登陆的 我忘了数据库的用户与密码

用window登录不用密码和用户名的。。如果你忘记了密码一般先通过window身份登录进去后,再将原来设置的密码清空就行了。 --------------------编程问答-------------------- 数据库连接字符串有问题。调整下。你也可以把连接字符串改成window登陆验证的 --------------------编程问答-------------------- 1,Windows验证
            
Common.ConnString = @"Initial Catalog=数据库名;Data Source=电脑名或IP地址;Integrated Security=SSPI";

2, Sql混合验证

Common.ConnString = @"server=电脑名或IP地址;database=数据库名;uid=用户名;password=密码";
--------------------编程问答--------------------
引用 10 楼 keenweiwei 的回复:
1,Windows验证
   
Common.ConnString = @"Initial Catalog=数据库名;Data Source=电脑名或IP地址;Integrated Security=SSPI";

2, Sql混合验证

Common.ConnString = @"server=电脑名或IP地址;database=数据库名;uid=用户名;password=密码";
……



我这样写 :<add name="MyCon" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=ShoppingManagerSystem;User ID=sa;password=nick"></add>

--------------------编程问答--------------------
引用 10 楼 keenweiwei 的回复:
1,Windows验证
   
Common.ConnString = @"Initial Catalog=数据库名;Data Source=电脑名或IP地址;Integrated Security=SSPI";

2, Sql混合验证

Common.ConnString = @"server=电脑名或IP地址;database=数据库名;uid=用户名;password=密码";
……

+1 --------------------编程问答-------------------- 很是头疼的问题 。。。。 --------------------编程问答-------------------- 在VS里面直接把连接字符串拷过去~ --------------------编程问答-------------------- sqlconnection conn = new  sqlconnection("tserver=.;database=tablename;uid=sa;pwd=123;")

<connectionStrings>
<add name="connstr" connectionString="server=.;uid=sa;pwd=123;database=crm"/>
</connectionStrings> --------------------编程问答--------------------
引用 14 楼 forevernome 的回复:
在VS里面直接把连接字符串拷过去~

怎么个弄法  以前在C# Form里我用过  web的我不知道在哪  数据库那里么? 看来我得翻书找找看 --------------------编程问答-------------------- 如果你的SQLExpress是试用版,你server后面直接加点是不行的。
如果你用是mdf文件,你在编译器中打开【服务器资源管理器】
把数据库的DataSource字符串拷贝过去即可。
你先在服务器资源管理器中将数据库连接上,看看好不好用。 --------------------编程问答-------------------- 出现新问题此版本的 SQL Server 不支持用户实例登录标志。该连接将关闭


用户代码未处理 System.Data.SqlClient.SqlException
  Message="此版本的 SQL Server 不支持用户实例登录标志。该连接将关闭。"
  Source=".Net SqlClient Data Provider"
  ErrorCode=-2146232060
  Class=14
  LineNumber=65536
  Number=18493
  Procedure=""
  Server="."
  State=1
  StackTrace:
       在 DAL.CustomersDAL.GetCustomersByLoginName(String loginName) 位置 D:\My Documents\Visual Studio 2008\Projects\nick\DAL\CustomersDAL.cs:行号 162
       在 DAL.CustomersDAL.Login(String loginName, String loginPwd, Customers& cus) 位置 D:\My Documents\Visual Studio 2008\Projects\nick\DAL\CustomersDAL.cs:行号 15
       在 BLL.CustomersBLL.Login(String loginName, String loginPwd, Customers& cus) 位置 D:\My Documents\Visual Studio 2008\Projects\nick\BLL\CustomersBLL.cs:行号 34
       在 WebShop.UserLogin.imgBtnLogin_Click(Object sender, ImageClickEventArgs e) 位置 D:\My Documents\Visual Studio 2008\Projects\nick\WebShop\UserLogin.aspx.cs:行号 30
       在 System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)
       在 System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
       在 System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 
--------------------编程问答--------------------
引用 18 楼 nick1yong 的回复:
出现新问题此版本的 SQL Server 不支持用户实例登录标志。该连接将关闭


用户代码未处理 System.Data.SqlClient.SqlException
  Message="此版本的 SQL Server 不支持用户实例登录标志。该连接将关闭。"
  Source=".Net SqlClient Data Provider"
  ErrorCode=-21462320……



这个把Instance=false;貌似决绝了  

又出现个 问题
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------

用户 'sa' 登录失败。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.Data.SqlClient.SqlException: 用户 'sa' 登录失败。

源错误: 

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。  

堆栈跟踪: 


[SqlException (0x80131904): 用户 'sa' 登录失败。]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +821651
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +186
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1932
   System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +33
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject) +172
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart) +381
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +181
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +173
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +357
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +30
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +424
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +66
   System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +494
   System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +82
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +105
   System.Data.SqlClient.SqlConnection.Open() +111
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +121
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +137
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +83
   System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1770
   System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +92
   System.Web.UI.WebControls.ListControl.PerformSelect() +31
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e) +26
   System.Web.UI.Control.PreRenderRecursiveInternal() +77
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360

 


--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.1891; ASP.NET 版本:2.0.50727.1879 

  --------------------编程问答--------------------
引用 19 楼 nick1yong 的回复:
引用 18 楼 nick1yong 的回复:
出现新问题此版本的 SQL Server 不支持用户实例登录标志。该连接将关闭


用户代码未处理 System.Data.SqlClient.SqlException
Message="此版本的 SQL Server 不支持用户实例登录标志。该连接将关闭。"
Source=".Net SqlClient Data Provider"
E……


链接字符串 如下 
 <add name="MyCon" connectionString="Data Source=.;Initial Catalog=ShoppingManagerSystem;Integrated Security=True; Connect Timeout=30;User Instance=False" providerName="System.Data.SqlClient"></add> --------------------编程问答--------------------
引用 10 楼 keenweiwei 的回复:
1,Windows验证
   
Common.ConnString = @"Initial Catalog=数据库名;Data Source=电脑名或IP地址;Integrated Security=SSPI";

2, Sql混合验证

Common.ConnString = @"server=电脑名或IP地址;database=数据库名;uid=用户名;password=密码";
……
说的很好么。。。 --------------------编程问答-------------------- 不知道怎么做?
点击视图--服务器资源管理器--数据链接--添加链接
链接好了的话,点击链接好的数据库--右键属性--(Ctrl+C)链接字符串中的内容,就是你的链接字符串
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,