用于connectionstring的sharpdevelop中的app.config时出错

时间:2013-12-14 23:16:27

标签: c# file configuration app-config sharpdevelop

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

  • 当我使用来自app.config的连接字符串时,我在尝试打开连接时遇到错误:

c.Open();

app.config< ================ file

    <?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代码..在此&lt; ================ form1

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");



    }

但是当我把直接字符串&lt; ================ form1

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)我还尝试从文本文件

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

也:

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

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

我希望得到帮助..在此先感谢:)

1 个答案:

答案 0 :(得分:0)

问题是您在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与您在问题中发布的内容不同。