#

ASP.Net, c#: Lesen der Connectionstring Settings
 
Der folgende C# Code wird verwendet um in einer Asp.net Webseite eine Verbindung zu einem Datenbank Server aufzubauen.
Hierzu muss in der Anwendung der Connectionstring zur Datenbank gelesen werden.
Der ConnectionString wird über die Settings gelesen.
In ASP.Net werden die Settings über den WebConfigurationsManager ausgelesen
Anschliessend wird mit dem ConnectionString eine Verbindung zur Datenbank aufgebaut.
Bei direkter Verwendung von SQL-Server Datenbanken verwendet man hier die SQLConnection
 
C# Code zum Lesen einer Verbindung zu einem SQL Datenbank Server

using System.Data.SqlClient;

using System.Web.Configuration;

 


 
// Read Connection STring
String connection_String = WebConfigurationManager.ConnectionStrings["database_Connection"].ConnectionString;
 
//Open Database Connection
SqlConnection con = new SqlConnection(connection_String);
con.Open();
 

 
Betrifft: Asp.Net, C#, MVC WebForms, SQL Server, IIS
 

 
Der Connectionstring einer Asp.Net Anwendung zur Datenbank hin wird definiert in der Web.config.
Der Connectionstring besteht aus einem eindeutigen Namen:name und einem String:connectionString , welcher den Datenbank-Server, die Ziel-Datenbank und den User und Passwort beinhaltet.
Mit providerName wird der Server-Typ definiert.
Die einzelne Verbindung zur Datenbank wird in dem Bereich configuration->connectionStrings mit <add name=.. connectionstring=..> eingetragen
 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
..
<connectionStrings>
<add name="database_Connection" connectionString="Data Source=.\sqlexpress;Initial Catalog=Test;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
 
<system.web>
<authentication mode="None" />
..

 

 
 
Bei den meisten Web Anwendungen wird hier der SQL-Server eingetragen, welcher ebenfalls auf dem gleichern Rechner liegt.
Der IIS-Server und der SQL Server bilden hier meistens eine direkte Kombination, bei welchem ein Web-Anwendung über den IIS-Server Daten aus einem SQL-Server abholt.
 
 
 
 
Komplettes Code Beispiel zum Lesen der Datenbank Connection Settings
Asp.net, c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using System.Data.SqlClient;
using System.Web.Configuration;
 
namespace webTest
{
public partial class dbTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String connection_String = WebConfigurationManager.ConnectionStrings["database_Connection"].ConnectionString;
SqlConnection con = new SqlConnection(connection_String);
 
con.Open();
 
}
}
}

 
 
 
Komplette asp.net->web.config des Beispiels

<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
 
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-webTest-20161021065301.mdf;Initial Catalog=aspnet-webTest-20161021065301;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="database_Connection" connectionString="Data Source=.\sqlexpress;Initial Catalog=Test;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
 
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.6" />
<httpRuntime targetFramework="4.6" />
<pages>
<namespaces>
<add namespace="System.Web.Optimization" />
<add namespace="Microsoft.AspNet.Identity" />
</namespaces>
<controls>
<add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
</controls>
</pages>
..
<!--
If you are deploying to a cloud environment that has multiple web server instances,
you should change session state mode from "InProc" to "Custom". In addition,
change the connection string named "DefaultConnection" to connect to an instance
of SQL Server (including SQL Azure and SQL Compact) instead of to SQL Server Express.
-->
<sessionState mode="InProc" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
</providers>
</sessionState>
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.OAuth" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.Cookies" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers>
</system.codedom>
</configuration>

 
 
 
 
Mobile

.

yesmovies