Benutzer:MovGP0/.NET/SQL

 MovGP0   Über mich   Hilfen   Artikel   Weblinks   Literatur   Zitate   Notizen   Programmierung   MSCert   Physik 

app.config / web.config

<configuration>
   <connectionStrings>
      <add name="MyConnectionString" 
           providerName="System.Data.SqlClient" 
           connectionString="Data Source=(localdb)\v11; InitialCatalog=MyDatabaseName" />
   </connectionStrings>

   <system.data>
      <DbProviderFactories>
         <add name="SqlClient Data Provider" 
              invariant="System.Data.SqlClient" 
              description=".NET Framework Data Provider for SQL Server" 
              type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </DbProviderFactories>
   </system.data>
</configuration>

Herstellen der Verbindung

var settings = ConfigurationManager.ConnectionStrings["MyConnectionString"];
var factory = DbProviderFactories.GetFactory(settings.ProviderName);
using (var connection = factory.CreateConnection())
{
   connection.ConnectionString = settings.ConnectionString;

   // prepare Queries

   await connection.OpenAsync();
   
   // execute Queries

   connection.Close();
}

Transaction Scope

using(var transaction = new TransactionScope())
{
   using (var connection = factory.CreateConnection())
   {
      // open connection
      // execute multiple queries within scope
   }

   // when no exception was thrown, complete the transaction
   transaction.Complete();
}

Create Table

CREATE TABLE dbo.People
{
   [id] INT NOT NULL IDENTITY,
   [FirstName] VARCHAR(30) NOT NULL,
   [MiddleName] VARCHAR(30) NULL, 
   [LastName] VARCHAR(30) NOT NULL
}

Read

var command = connection.CreateCommand();
command.CommandText = "SELECT * FROM dbo.People";
command.CommandType = CommandType.Text;

var dataReader = await command.ExecuteReaderAsync(); 
while(await dataReader.ReadAsync())
{
   Console.WriteLine("Id: '{0}', FirstName: '{1}', MiddleName: '{2}', LastName: '{3}'", 
      dataReader["id"], dataReader["firstname"], dataReader["middlename"], dataReader["lastname"]);
}
dataReader.Close();

Update

var command = connection.CreateCommand();
command.CommandText = "UPDATE dbo.People WHERE Id=5 SET FirstName='John'";
command.CommandType = CommandType.Text;

await connection.OpenAsync();
int numberOfUpdatedRows = await command.ExecuteNonQueryAsync();
var command = connection.CreateCommand();
command.CommandText = "INSERT INTO dbo.People([FirstName], [MiddleName], [LastName]) VALUES(@firstName, @middleName, @lastName)";
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@firstName", "John");
command.Parameters.AddWithValue("@middleName", "Little");
command.Parameters.AddWithValue("@lastName", "Doe");

await connection.OpenAsync();
int numberOfInsertedRows = await command.ExecuteNonQueryAsync();

Query Types

Befehl Einsatz
.ExecuteReader() Query mit Liste von Rückgabewerten
.ExecuteScalar() Query mit Skalarem Rückgabewert
.ExecuteNonQuery() Command

Internetquellen

|}

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.