在sharpdevel中的app.config用于connectionstring时出错
本文关键字:connectionstring 出错 用于 config sharpdevel 中的 app | 更新日期: 2024-09-26 01:09:27
我有一个非常奇怪的问题,让我陷入了项目中。。我使用的是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代码。。在这个<====================形式1
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");
}
但是当我放入直接字符串<===============形式1
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"。但是ConfigurationManager。如果缺少"cn"连接字符串,ConnectionStrings["cs"]将返回null引用,因此我怀疑您的代码和app.config与您在问题中发布的内容不同。