Use C # to connect SQL Server database

tags: C#  sql  C # Connect SQL Database

C # Connect to SQL Server

Connection connection class

Use the SqlConNetion class. When using the SQLConnection class, you want to reference a namespace of system.data.sqlclient.

using System.Data.SqlClient;

CommandCommand class

Using the SQLCommand database command class mainly performs operations for the database, such as inserting, deleting, modifying the database.

DataReader read class

Use SqlDataReader. Database read category is the database command class in the data type of the result returned after the query operation. Database read category is only available when the database's connection status is in an open state. When the database is closed, the database read category is no longer valid.

DataSet Class

The data set is equivalent to a virtual database, and each data set includes multiple data sheets. Even if the database is connected, it is still possible to continue access to the record from the data set, but the data is stored in the data set and is not stored in the database.

DataAdapter adaptation class

You can use SqlDataAdapter. The data adapter is often used with the data set. The data adapter can store the data in the database to the data set, and the data adapter can be said to be a bridge between the data set and the database.

Connect to the database

Enter your own server name here, there are two login methods, Windows and authentication logins. This is the same way as you create a database.

Again to choose the table you need to operate here.

There are two ways to connect the database(See how you log in)

Data Source = server name; Initial catalog = database name; user ID = username; PWD = password (no password can be omitted)

Column:

string connStr = "Data Source=.;Initial Catalog = MySchool;User ID = aa;Password=123";  
 string connStr = "Data Source=.;Initial Catalog=QQ;Integrated Security=True";

Here we can directly view the code for connecting the database.

Next, you can operate the database.

The test is successful

 public void TestConSQL()
        { 
        // Test connection database
            string connStr = "Data Source=.;Initial Catalog=QQ2021;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connStr);
            // Open the database
            conn.Open();
            Console.WriteLine("Open Database Connection");
            // Close the database
            conn.Close();
            Console.WriteLine("Close Database Connection");
        }



Intelligent Recommendation

C# connect to SQL Server database (2)

Execute SQL statement: Command object 1. Command object overview   The Command object is a data command object. Its main function is to send query, update, delete, and modify SQL statements ...

VS connect to SQL Server database (C# code)

tool: 1. Visual Studio (I use vs2013) 2.SQL Server (I use sql server2008) operating: 1. Open SQL Server, and you will see the initial link interface of the database. (As shown below) 2. Copy the "...

C# connect to Sql Server database and display it on the console

 1. Use SQL username and password authentication Data Source = server name; Initial Catalog = database name; User ID = user name; Pwd = password (no password can be omitted) E.g:public string con...

C# realizes ADO to connect sql server database

My understanding of ADO is not so thorough. So far, I feel that ADO may have the same function as JDBC and can connect to the database. At present, I only use c# to connect to sqlserver (mysql) throug...

[Learn C#] Connect to SQL Server database

Article Directory Necessary preparation Connection process Implementation code Use configuration files Necessary preparation To connect to the database, you must first have a database windows authenti...

More Recommendation

Python use pymssql to connect to sql server database

Python connects to SQL Server database - pymssql uses the basics ----Original address: The following is the instructions for using the parameters in pymssql, as follows: pymssqlCnx class (used to conn...

SQL Server installation and use SSMS to connect to the database

Article Directory 1. Installation of SQL Server and SSMS Second, the connection of SQL Server 1. Installation of SQL Server and SSMS 1. SQL installation download link:SQL Server。 Enter the download ad...

Use DataGrip to connect to SQL Server 2017 database

Microsoft's SQL Server database is a good friend for .NET development. The latest version is now 2017. The SQL Server database has its own Management Studio tool, which is basically similar to Visual ...

Use JDBC to connect to SQL Server database in IDEA

1. Preparation SQL Server database installation, IDEA and JDK environment installation and configuration. I installed SQL Server 2008, the JAVA environment is as follows, IDEA is installed automatical...

Questions: Use JDBC to connect to SQL Server database

When I first came into contact with using JDBC to connect to a database, I always encountered a lot of problems, making people confused. Therefore, I use SQL Server As an example, explain in detail ho...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top