Web.config entry to connect to SQL Server
Well I'm trying to change the connection of my application from Oracle to SQL Server. I have this in web.config.
<compilation debug="true">
<assemblies>
...
<add assembly="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
...
</assemblies>
</compilation>
and
<appSettings>
<add key="DatabaseProvider" value="Test.App.Oracle8i"/>
<add key="SmtpUser" value="mail@exampl开发者_如何学Pythone.com"/>
<add key="SmtpPassword" value="testpassword"/>
<add key="SmtpDefaultSender" value="mail@example.com"/>
<add key="SmtpHost" value="mail.example.com"/>
<add key="SmtpPort" value="25"/>
<add key="EnableSsl" value="False"/>
</appSettings>
and this
<connectionStrings>
<add name="CnString" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=loCALHOST)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=XE)));User Id=TESTUSER;Password=TESTPASSWORD;"/>
</connectionStrings>
When I change these values for these application will not run and mark errors.
<compilation debug="true">
<assemblies>
...
<add assembly="System.Data.SqlClient, Version=10.50.1600, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
...
</assemblies>
</compilation>
and
<appSettings>
<add key="DatabaseProvider" value="Test.App.SqlServer"/>
<add key="SmtpUser" value="mail@example.com"/>
<add key="SmtpPassword" value="testpassword"/>
<add key="SmtpDefaultSender" value="mail@example.com"/>
<add key="SmtpHost" value="mail.example.com"/>
<add key="SmtpPort" value="25"/>
<add key="EnableSsl" value="False"/>
</appSettings>
and the last
<connectionStrings>
<add name="CnString" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=MSSQLSERVER)));Initial Catalog=TESTDATABASE;User Id=TESTUSER;Password=TESTPASSWORD;"/>
</connectionStrings>
or
<connectionStrings>
<add name="CnString" providerName="System.Data.SqlClient" connectionString="Data Source=(local)\\MSSQLSERVER;Initial Catalog=TESTDATABASE;User Id=TESTUSER;Password=TESTPASSWORD;"/>
</connectionStrings>
I do not understand is supposed to make the lines.
<add assembly="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add key="DatabaseProvider" value="Test.App.Oracle8i"/>
<add name="CnString" connectionString="Data Source=**(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=loCALHOST)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=XE)))**;User Id=TESTUSER;Password=TESTPASSWORD;"/>
The only 2 lines you need to connect to SQL Server is:
<connectionStrings>
<add name="CnString" providerName="System.Data.SqlClient" connectionString="Data Source=(local)\\MSSQLSERVER;Initial Catalog=TESTDATABASE;User Id=TESTUSER;Password=TESTPASSWORD;"/>
</connectionStrings>
and
<assemblies>
<add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
(change version and publicKeyToken in case of .NET 2.0/3.x)
Then use:
SqlConnection connection = new SqlConnection("CnString");
// ...
That's it.
In .NET there is no such assembly as System.Data.SqlClient, Version=10.50.1600, ...
. SqlClient is the part of standart .NET Framework distribution.
For example, System.Data.SqlClient.SqlConnection:
Namespace: System.Data.SqlClient
Assembly: System.Data (in System.Data.dll)
精彩评论