. NET Core Development Log--ado. NET and SQL Server

Source: Internet
Author: User

In the. NET world, if you want to operate a database, there is no need for ADO. The familiar libraries and APIs are also inseparable from the. NET core. Here's a brief description of how SQL Server is handled differently by ADO.

System.Data.SqlClient

The first step is to see if there are any System.Data.SqlClient references in project engineering, and if not, you can install them in the following three ways:

    • Visualize NuGet package management tools on Visual Studio (Manage NuGet Packages), find the appropriate class library, click the Install button
    • The same is the Package Management Console (Pack Manager console) input command on Visual Studio,Install-Package System.Data.SqlClient
    • Command line Interface (CLI) to use the command,dotnet add package System.Data.SqlClient
Inquire

The most basic ExecuteReader method can be used:

using (var conn = new SqlConnection("Server=.;Integrated Security=true")){    conn.Open();    using (var cmd = new SqlCommand("select top 10 * from [AdventureWorks2016CTP3].[Person].[Person]", conn))    {        using (var reader = cmd.ExecuteReader())        {            while (reader.Read())            {                Console.WriteLine(reader["FirstName"]);            }        }    }}

You can also use SqlDataAdapter and datasets later in. NET Core 2.0 to see that Microsoft has done a lot of work with backward compatibility:

using (var conn = new SqlConnection("Server=.;Integrated Security=true")){    var adapter = new SqlDataAdapter("select top 10 * from [AdventureWorks2016CTP3].[Person].[Person]", conn);    var dataset = new DataSet();    adapter.Fill(dataset);    var dt = dataset.Tables[0];    foreach (var item in dt.Rows)    {        var row = item as DataRow;        Console.WriteLine(row["FirstName"]);    }}
Insert
using (var conn = new SqlConnection("Server=.;Integrated Security=true")){    conn.Open();    using (var cmd = new SqlCommand("insert into [AdventureWorks2016CTP3].[Person].[AddressType] (Name) values(@Name)", conn))    {        cmd.Parameters.AddWithValue("@Name", "Test");        cmd.ExecuteNonQuery();    }}
Update
using (var conn = new SqlConnection("Server=.;Integrated Security=true")){    conn.Open();    using (var cmd = new SqlCommand("update [AdventureWorks2016CTP3].[Person].[AddressType] set name = @Name where name = @Criterion", conn))    {        cmd.Parameters.AddWithValue("@Name", "Test2");        cmd.Parameters.AddWithValue("@Criterion", "Test");        cmd.ExecuteNonQuery();    }}
Delete
using (var conn = new SqlConnection("Server=.;Integrated Security=true")){    conn.Open();    using (var cmd = new SqlCommand("delete [AdventureWorks2016CTP3].[Person].[AddressType] where name = @Name", conn))    {        cmd.Parameters.AddWithValue("@Name", "Test2");        cmd.ExecuteNonQuery();    }}
Stored Procedures
using (var conn = new SqlConnection("Server=.;Integrated Security=true")){    conn.Open();    using (var cmd = new SqlCommand("[AdventureWorks2016CTP3].[dbo].[uspGetEmployeeManagers]", conn))    {        cmd.CommandType = CommandType.StoredProcedure;        cmd.Parameters.AddWithValue("@BusinessEntityID", 10);        using (var reader = cmd.ExecuteReader())        {            while (reader.Read())            {                Console.WriteLine(reader["FirstName"]);            }        }    }}
View

There is no difference between the use and the normal data table.

using (var conn = new SqlConnection("Server=.;Integrated Security=true")){    conn.Open();    using (var cmd = new SqlCommand("select top 10 * from [AdventureWorks2016CTP3].[HumanResources].[vEmployee]", conn))    {        using (var reader = cmd.ExecuteReader())        {            while (reader.Read())            {                Console.WriteLine(reader["FirstName"]);            }        }    }}

The database in the above code example uses AdventureWorks Databases and Scripts for SQL Server CTP3

. NET Core Development Log--ado. NET and SQL Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.