【发布时间】: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