首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SqlConnection抛出异常

SqlConnection抛出异常
EN

Stack Overflow用户
提问于 2009-10-13 10:53:09
回答 7查看 17.8K关注 0票数 4

敬业人士请帮助我解决以下问题。在我的.NET C#应用程序中,a有以下代码:

代码语言:javascript
复制
SqlConnection connection = new SqlConnection( SQLCONNECTION_STRING );

它在我的开发机器上工作得很好,但是在Windows 2003服务器上抛出了一个异常。应用程序通过CGI运行,并具有“完全信任”级别。我尝试了几个连接字符串,并且我认为字符串不会导致这个问题,因为即使是这段代码也会有异常:

代码语言:javascript
复制
SqlConnection connection = new SqlConnection();

谢谢。

我发现了两个奇怪的事实:

  1. 如果我连接调试器来处理和简单跟踪代码,那么
  2. 就能工作,如果我从命令行运行应用程序(而不是按要求通过CGI )。

所以我猜CGI和SqlConnection的交互有问题。有人知道这事吗?

谢谢大家的回应。

编辑后添加:

以下是我的连接字符串:"Provider=SQLOLEDB;Data Source=(本地);初始Catalog=Users;User Id=user;Password=password;“

我还尝试了几种可能的变体,如下所述:http://www.connectionstrings.com/sql-server

请提供关于以下例外情况的资料:

代码语言:javascript
复制
The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.
   at System.Data.SqlClient.SqlConnection..ctor()
   at Test.Database.UpdateSQLServerDatabase(IDictionary`2 fields, String regName, StringBuilder regCode)
   at Test.Program.Run()

Type: System.TypeInitializationException

InnerException: System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlConnectionFactory' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlPerformanceCounters' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.ArgumentException: Illegal characters in path.
   at System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[] str)
   at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
   at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String path)
   at System.AppDomainSetup.VerifyDir(String dir, Boolean normalize)
   at System.AppDomainSetup.get_ConfigurationFile()
   at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
   at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
   at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
   at System.Configuration.ClientConfigurationHost.GetStreamName(String configPath)
   at System.Configuration.ClientConfigurationSystem..ctor()
   at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
   at System.Diagnostics.DiagnosticsConfiguration.GetConfigSection()
   at System.Diagnostics.DiagnosticsConfiguration.Initialize()
   at System.Diagnostics.Switch.InitializeConfigSettings()
   at System.Diagnostics.Switch.InitializeWithStatus()
   at System.Diagnostics.Switch.get_SwitchSetting()
   at System.Diagnostics.TraceSwitch.get_Level()
   at System.Data.ProviderBase.DbConnectionPoolCounters..ctor(String categoryName, String categoryHelp)
   at System.Data.SqlClient.SqlPerformanceCounters..ctor()
   at System.Data.SqlClient.SqlPerformanceCounters..cctor()
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlConnectionFactory..ctor()
   at System.Data.SqlClient.SqlConnectionFactory..cctor()
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlConnection..cctor()

甚至没有参数的构造函数也会给出异常。所以我不认为问题在连接字符串中。我可以看到堆栈跟踪显示了一些关于“路径中的非法字符”的信息。所以我会试着钻下去找出答案。但也许有人已经知道解决办法了。

EN

回答 7

Stack Overflow用户

发布于 2009-10-23 18:26:25

此问题通常与配置文件中的问题有关。你可能想要重新创造这个。

谢谢,Jivtesh

票数 9
EN

Stack Overflow用户

发布于 2011-12-13 13:23:13

当我的app.config包含一个空连接字符串部分时,我遇到了这个问题,如下所示;

代码语言:javascript
复制
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
    </connectionStrings>
</configuration>

从它的声音,你有一个版本,运行作为控制台应用程序,并作为一个网络应用程序。这意味着控制台应用程序有一个app.config,asp.net站点有一个web.config

因为您有一个工作控制台应用程序,所以尝试找出等效部分之间的差异,并更新web.config以匹配您的app.config

另外,您可能会考虑到系统固有的不同权限--网站可能没有连接数据库的权限,因为它是以IUSR_<machinename>的形式运行的,但是控制台应用程序是在您自己的用户凭据下运行的,您可能是一名计算机管理员。

票数 2
EN

Stack Overflow用户

发布于 2009-10-13 12:47:21

尝试以下几点:

代码语言:javascript
复制
string str="Data Source=[YourServer];Initial Catalog=yourdb;User ID=sa;Password=sa;";
SqlConnection connection = new SqlConnection(str);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1559493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档