簡體   English   中英

在SharpDevelop中將app.config用於連接字符串時出錯

[英]error when app.config in sharpdevelop for connectionstring

我有一個非常奇怪的問題,使我陷於項目中..我正在使用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在此<。=============== 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");



    }

但是當我放置直接字符串<================ 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)我也試圖從文本文件中獲取連接字符串

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

也:

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

我得到完全相同的錯誤:)

我希望得到幫助..在此先感謝:)

問題是您在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與您在問題中發布的內容不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM