尝试使用它,它对我来说很好用:
<add name="sConnectionString"
connectionString="Integrated Security=SSPI;Persist Security Info=False;User ID=userid;Initial Catalog=databasename;Data Source=.\SQLEXPRESS"
providerName="System.Data.SqlClient" />
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["sConnectionString"].ConnectionString);
我希望它会帮助你.. :)
您也可以使用该网站获取连接字符串:
http: //www.connectionstrings.com/sql-server/
还要确保 app.config 以:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="yourProjectName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
让我知道这是否对您有帮助。:)