Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

C # how to connect to the local database

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how C# connects to the local database". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "how to connect to the local database in C#"!

Use environment

C#

VSCode

M1

1. Install MySQL

Download the MySQL software and install it foolishly. After installation, a MySQL icon will appear in the system preference setting.

During the installation, the user will be asked to enter the password. Just watch and lose, but don't forget.

It is recommended not to use the terminal command line installation, for beginners, the cost of learning is relatively high.

two。 Connect to the local MySQL

After installing MySQL, click the MySQL icon to open MySQL, as shown below

I have installed Navicate locally, so I use Navicate to connect to the local database to verify whether the connection is successful.

Open Nvicate, click Connection, and select MySQL

Fill in the database name and enter the password to connect.

After connecting, you can create a database Test

Then we create the table in the database Test, and then we can use C # to connect to the local database and manipulate the table Test.

3. C # connects to the local MySQL

Let's create a console command program Demo locally, and then the complete code is as follows

/ / defines the connection string. Here Test is the database table we created; Uid is a table field. Enter the password of the database string connectStr = "Server=127.0.0.1;Database=Test.Uid=root;Password=xxx;"; / / create the link object MySqlConnection connection = new MySqlConnection (connectStr); if (connection! = null) {Console.WriteLine ("database connection failed");} else {Console.WriteLine ("database connection failed"); return;} / / open the connection connection.Open (); / / spell SQL statement StringBuilder sqlB = new StringBuilder () SqlB.AppendLine ("SELECT"); sqlB.AppendLine ("stuID"); sqlB.AppendLine (", name"); sqlB.AppendLine ("FROM studen"); sqlB.AppendLine (";"); / / create an object that executes Sql MySqlCommand command = new MySqlCommand (sqlB.ToString (), connection); / / execute SqlMySqlDataReader reader = command.ExecuteReader () / / traversal data while (reader.Read ()) {Console.WriteLine ("{0}\ t {1}", reader ["stuID"], reader ["name"]);}

In the above code, we use StringBuilder to spell Sql statements and use classes such as MySqlConnection and MySqlCommand to operate the database.

StringBuilder is the library of System.Text. We need the library of Using.

MySqlConnection and MySqlCommand are a library of MySQL, MySql.Data. We need to introduce MySql.Data.MySqlClient into the project.

.net6 preferred that we put the library Using in and add ItemGroup to the .csproj file

Exe net6.0 enable enable

Because I created a console application, I used the following command to import the library directly from the terminal

Dotnet add package MySql.Data

➜Demo dotnet add package MySql.Data

Determining which items to restore.

Writing / var/folders/k1/96lj0hk91js5fh6lhxnl7s740000gn/T/tmpn7jejh.tmp

Info: adding PackageReference for package "MySql.Data" to project "/ Users/morris/Desktop/Demo/Demo.csproj".

Info: CACHE https://api.nuget.org/v3/registration5-gz-semver2/mysql.data/index.json

Info: restoring / Users/morris/Desktop/Demo/Demo.csproj 's package.

Info: GET https://api.nuget.org/v3-flatcontainer/mysql.data/index.json

Info: OK https://api.nuget.org/v3-flatcontainer/mysql.data/index.json 217ms

Info: GET https://api.nuget.org/v3-flatcontainer/mysql.data/8.0.28/mysql.data.8.0.28.nupkg

Info: OK https://api.nuget.org/v3-flatcontainer/mysql.data/8.0.28/mysql.data.8.0.28.nupkg 108ms

Info: GET https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4.streams/index.json

Info: OK https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4.streams/index.json 196ms

Info: GET https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4.streams/1.2.6/k4os.compression.lz4.streams.1.2.6.nupkg

Info: OK https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4.streams/1.2.6/k4os.compression.lz4.streams.1.2.6.nupkg 92 milliseconds

Info: GET https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4/index.json

Info: OK https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4/index.json 233ms

Info: GET https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4/1.2.6/k4os.compression.lz4.1.2.6.nupkg

Info: OK https://api.nuget.org/v3-flatcontainer/k4os.compression.lz4/1.2.6/k4os.compression.lz4.1.2.6.nupkg 93 milliseconds

Info: K4os.Compression.LZ4 1.2.6 has been installed from https://api.nuget.org/v3/index.json through the content hash 4EN8EE6bZG2U8dFfeqnroomOm3UNajK3cPYHvyQROCFm4jNFVLuRB7Nl5bDkjBSAjfctS6konminstalled ay3u5RafBzltDA.

Info: the content hash 5KMcNFRHeRrnJ9c8k5fZcfAJE0FndMiDiHIYa35Mx5KCMkeSNoqqqqt PEXu7YmtCoVczJagxFt7JJqqqure = install K4os.Compression.LZ4.Streams 1.2.6 from https://api.nuget.org/v3/index.json.

Info: MySql.Data 8.0.28 has been installed from https://api.nuget.org/v3/index.json through the content hash 7b699tU9ba0r7SBK7K9snGJB9ulnOdwEXG28kpVaQSN0hHenfoJ. ADOtdfhccn1KStBX8pFK5qIl8scdBL2g installed.

Info: the package "MySql.Data" is compatible with all frameworks specified in the project "/ Users/morris/Desktop/Demo/Demo.csproj".

Info: the PackageReference of the package "MySql.Data" (version 8.0.28) has been added to the file "/ Users/morris/Desktop/Demo/Demo.csproj".

Info: writes the asset file to disk. Path: / Users/morris/Desktop/Demo/obj/project.assets.json

Log: restored / Users/morris/Desktop/Demo/Demo.csproj (3.6sec in use).

The framework will be downloaded and referenced into the project.

Finally, dotnet run is executed and the database connection is successful!

Thank you for reading, the above is the content of "how to connect to the local database of C#". After the study of this article, I believe you have a deeper understanding of the problem of how to connect to the local database of C#. The specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report