【问题标题】:error when app.config in sharpdevelop for connectionstring连接字符串的Sharpdevelop中的app.config时出错
【发布时间】:2013-12-15 18:24:40
【问题描述】:

我有一个非常奇怪的问题,让我陷入了我的项目中。我正在使用 C#(SharpDevelop 4.3.3 build 9663) ..

  • 当我使用 app.config 中的连接字符串时,尝试打开连接时出错:

c.Open();

app.config

    <?xml version="1.0"?>
<configuration>


<connectionStrings>

    <add name="databasecon" connectionString="Data Source=ahmed\\sqlexpress;Initial Catalog=abumanahilms;Integrated Security=True" />

</connectionStrings>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

button1 代码.. 在这个

void Button1Click(object sender, EventArgs e)
    {

        string cs=ConfigurationManager.ConnectionStrings["cs"].ToString();


        SqlConnection c = new SqlConnection(cs);


        SqlCommand sc = new SqlCommand("insert into table1 (money) VALUES ('"+textBox1.Text+"')",c);

        SqlDataReader sr;
        c.Open();

        sr = sc.ExecuteReader();


        MessageBox.Show("success");



    }

但是当我把直接字符串

string cs="Data Source=ahmed\\sqlexpress;Initial Catalog=test;Integrated Security=True";

效果很好……

我得到的错误 ------------------------------- ------------------------------

    System.InvalidOperationException: Instance failure.
   at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at teeest.MainForm.Button1Click(Object sender, EventArgs e) in c:\Users\Ahmed Albusaidi\Documents\SharpDevelop Projects\teeest\teeest\MainForm.cs:line 59
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at teeest.Program.Main(String[] args) in c:\Users\Ahmed Albusaidi\Documents\SharpDevelop Projects\teeest\teeest\Program.cs:line 27

2) 我也尝试从文本文件中获取连接字符串

string cs= File.ReadAllText("connectionstring.txt").ToString();

还有:

string cs= File.ReadAllText("connectionstring.txt");

我得到完全相同的错误:)

我希望得到帮助..提前谢谢:)

【问题讨论】:

  • 如果您发布实际错误以及导致错误的原因将帮助人们帮助您..
  • 我提到了所有可能的情况.. 仅在一种情况下出错.. 出现错误时,它指向 c.Open();代码行..如上所述..感谢您的关注..
  • 这行string cs=ConfigurationManager.ConnectionStrings["cs"].ToString();不应该是string cs=ConfigurationManager.ConnectionStrings["databasecon"].ToString();吗?
  • 是的,它应该是 .. 但我将 databasecon 更改为 cs .. 所以没关系 .. 无论如何谢谢 :)

标签: c# file configuration app-config sharpdevelop


【解决方案1】:

问题是您在 app.config 中使用了 \\。 app.config 中应该只有一个反斜杠。无需在 app.config 中对字符串进行编码。您只需在 C# 代码中执行此操作。将数据源更改为 ahmed\sqlexpress,只需一个反斜杠:

<connectionStrings>
    <add name="databasecon" connectionString="Data Source=ahmed\sqlexpress;Initial Catalog=abumanahilms;Integrated Security=True" />
</connectionStrings>

您的代码的另一个问题是 app.config 有一个名为“databasecon”的连接字符串,但您的代码使用的是“cn”。但是,如果缺少“cn”连接字符串,ConfigurationManager.ConnectionStrings["cs"] 将返回空引用,因此我怀疑您的代码和 app.config 与您在问题中发布的内容不同。

【讨论】:

  • 我会尝试并反馈给你:)
  • 完美...简单的人..就像那样..我真的很感激:)
  • Matt Ward ...你能给我一些建议吗..你知道我是如何编写程序的..可以吗?或者你对我有什么建议?
猜你喜欢
  • 1970-01-01
  • 2013-02-01
  • 2013-06-13
  • 2015-09-16
  • 2019-02-27
  • 2023-03-29
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多