laitimes

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

author:IT technology sharing community
C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

LighthouseDB介绍

Based on Tencent Cloud's self-developed next-generation cloud-native database TDSQL-C, LighthouseDB combines the advantages of traditional databases, cloud computing, and new hardware technologies, is 100% compatible with MySQL, achieves a high throughput of over one million QPS, and 128 TB of distributed intelligent storage to ensure data security and reliability.

LighthouseDB 产品特性

● 100% compatible with MySQL: 100% compatible with the open-source database engine MySQL.

● Ultra-high performance: Ultra-high performance of one million QPS per node to meet high-concurrency scenarios

● Fast recovery: Compute nodes are stateless, support second-level failover and recovery of local and cross-device, and support second-level backup and rollback based on snapshots

● Simple management: Out of the box, the default template provides simplified operations

● High data reliability: The cluster supports security group and VPC network isolation. Automatically maintain multiple copies of data and backups to ensure data security and reliability with 99.99999999% reliability.

● Ultra-cost-effective: Fixed-specification packages are available, and lightweight databases can be purchased and used at ultra-low prices for shared resources, and the performance is much higher than that of self-built databases

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Introduction to optional configurations

Product official website:

Open the official website of the product and click the Buy Now button.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Then enter the purchase interface, you can choose the configuration that suits you according to your needs.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

After the configuration is complete, you can enter the product console, and the interface is as follows:

Here you can change the database password for the first time, which is convenient for subsequent operations.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Click the login button in the lower left corner to open the database management page, select the instance, database account, password, and click the login button.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Create a database

In this case, you will be taken to the database management page. Let's click on Library Management.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Then enter the database management interface. Click the New Database button

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

The Create Database page is displayed, set the database name, character set, and collation, and then click the OK button.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

After the database is created, you can see that the newly created DBDemo database appears in the bottom list.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Create a data table

After the database is created, we can create a new data table with the following interface:

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Then switch to the Column Information tab and add a data table field

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Create a SQL table as follows:

CREATE TABLE `T_User` (
  `Id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键自增',
  `Name` varchar(20) DEFAULT '' COMMENT '姓名',
  `Age` int DEFAULT NULL COMMENT '年龄',
  `Address` varchar(100) DEFAULT '' COMMENT '住址',
  `Hobby` varchar(100) DEFAULT '' COMMENT '兴趣爱好',
  `Birthday` date DEFAULT NULL COMMENT '出生日期',
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='用户表'
           

SQL preview page

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

SQL change preview interface, confirm that there is no problem, and we click the OK button.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

After the table is created, we can add a new row of records. The interface is as follows:

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Click the submit button to enter the execution preview interface, and click the execution data to insert it successfully. Of course, it is also possible to import data.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Create a view

This creates a V_User view of users older than 30.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Click Submit to enter the pop-up window for the confirmation of the new view, and then click the OK button to create the view.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

After a view is created, you can run SQL statements to view the data of the view.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Create a function

Create a simple summation function

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

The interface is as follows:

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Create a function statement as follows:

CREATE DEFINER=`root`@`%` FUNCTION `func_sum`(num1 INT, num2 INT) RETURNS int
    DETERMINISTIC
    COMMENT '新建求和函数'
BEGIN
    DECLARE total INT;
    SET total = num1 + num2;
    RETURN total;
END
           

Execute the function interface

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Turn on remote access

First, go back to the console interface and click Turn on Internet.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Then the local cmd ping domain name to get the public IP address

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Note: The database port is the port after the public domain name is enabled, not the default port 3306.

Note: Extranet access is only used for database development or auxiliary management, and private network access is used for business access.

It can then be accessed using navicat

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

After a successful visit.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

C# Integration Case Test

First, create a console program LighthouseDBDemo with a net framework of 4.7.2

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Then install a third-party framework, here you mainly need the mysql driver and the Dapper database orm framework.

Open the Nuget management interface, install the MySQL driver, and select the latest version 8.0.31.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

To install the Dapper framework, the latest version 2.1.28 is also selected, which is mainly used for queries and additions.

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

The code is as follows

This mainly reflects the test database connection, query, and new operations, and the specific code is as follows:

using Dapper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LighthouseDBDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionStr = string.Format("data source={0};user id={1};password={2};database={3};port={4};pooling=false;charset=utf8;", "公网IP地址", "root", "12345678","DBDemo", "27452");
            ConnectionTest(connectionStr);
            GetSelectList(connectionStr);
            InsertDemo(connectionStr);
            Console.ReadKey();
        }

        /// <summary>
        /// 判断数据库服务器是否连接成功
        /// </summary>
        /// <param name="conStr"></param>
        /// <returns></returns>
        public static void ConnectionTest(string conStr)
        {
            try
            {
                using (MySqlConnection connection = new MySqlConnection(conStr))
                {
                    connection.Open();                 
                    connection.Close();
                    Console.WriteLine("数据库连接成功!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("数据库连接失败!原因:"+ex.Message);
            }
        }

        /// <summary>
        /// 查询测试
        /// </summary>
        /// <param name="conStr"></param>
        /// <returns></returns>
        public static void GetSelectList(string conStr)
        {
            string sql = "select name from T_User ";
            try
            {
                using (MySqlConnection connection = new MySqlConnection(conStr))
                {
                    var list = connection.Query<string>(sql).ToList();
                    if (list.Any())
                    {
                        foreach (var item in list)
                        {
                            Console.WriteLine(item);
                        }
                    }
                    
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("查询失败!原因:" + ex.Message);
            }
        }

        /// <summary>
        /// 数据插入测试
        /// </summary>
        /// <param name="conStr"></param>
        public static void InsertDemo(string conStr)
        {
            string sql = "INSERT INTO `DBDemo`.`T_User` (`Name`, `Age`, `Address`, `Hobby`, `Birthday`) VALUES ('小刘', 28, '江苏徐州', '听歌', '1996-08-05') ";
            try
            {
                using (MySqlConnection connection = new MySqlConnection(conStr))
                {
                    int count = connection.Execute(sql);
                    if (count > 0)
                    {
                        Console.WriteLine("数据插入成功!");
                    }

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("查询失败!原因:" + ex.Message);
            }
        }
    }
}

           

Then run the project, and the execution effect screen is as follows:

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

Open the database client to view

C# in practice: Introduction and case practices of LighthouseDB Lightweight Database Service

The test data is OK, this is just a simple demo, you can adjust it according to actual needs.

Read on