.net的基于mysql的DBhelper实现_MySQL
网上很少dbhelper的基于mysql数据库的,所以实现了下mysql的dbhelper,封装好了各种方法调用数据库函数,可以应用于小型的c#程序中。使用时记得修改password和端口号,mysql默认是3306,我自己定义了新端口号,所以是3307。
using System;using System.Collections.Generic;using System.Text;using System.Data;using MySql.Data.MySqlClient;namespace studentdb{ public class DBHelper { //引导数据库连接数据库调用Web.Config文件 private static MySqlConnection connection; //创建连接 public static MySqlConnection Connection { get { MySqlConnection myConn = new MySqlConnection("Database=student;Data Source=localhost;User Id=root;Password=pw;pooling=false;CharSet=gb2312;port=3307"); string connectionString = myConn.ConnectionString; if (connection == null) { connection = new MySqlConnection(connectionString); //打开连接 connection.Open(); } else if (connection.State == System.Data.ConnectionState.Closed) { connection.Open(); } else if (connection.State == System.Data.ConnectionState.Broken) { connection.Close(); connection.Open(); } return connection; } } //(无参)返回执行的行数(删除修改更新) public static int ExecuteCommand(string safeSql) { MySqlCommand cmd = new MySqlCommand(safeSql, Connection); int result = cmd.ExecuteNonQuery(); return result; } //(有参) public static int ExecuteCommand(string sql, params MySqlParameter[] values) { MySqlCommand cmd = new MySqlCommand(sql, Connection); cmd.Parameters.AddRange(values); return cmd.ExecuteNonQuery(); } //(无参)返回第一行第一列(删除修改更新) public static int GetScalar(string safeSql) { MySqlCommand cmd = new MySqlCommand(safeSql, Connection); int result = Convert.ToInt32(cmd.ExecuteScalar()); return result; } //(有参) public static int GetScalar(string sql, params MySqlParameter[] values) { MySqlCommand cmd = new MySqlCommand(sql, Connection); cmd.Parameters.AddRange(values); int result = Convert.ToInt32(cmd.ExecuteScalar()); return result; } //返回一个DataReader(查询) public static MySqlDataReader GetReader(string safeSql) { MySqlCommand cmd = new MySqlCommand(safeSql, Connection); MySqlDataReader reader = cmd.ExecuteReader(); return reader; } public static MySqlDataReader GetReader(string sql, params MySqlParameter[] values) { MySqlCommand cmd = new MySqlCommand(sql, Connection); cmd.Parameters.AddRange(values); MySqlDataReader reader = cmd.ExecuteReader(); return reader; } //返回一个DataTable public static DataTable GetDataSet(string safeSql) { DataSet ds = new DataSet(); MySqlCommand cmd = new MySqlCommand(safeSql, Connection); MySqlDataAdapter da = new MySqlDataAdapter(cmd); da.Fill(ds); return ds.Tables[0]; } public static MySqlDataAdapter GetAdapter(string safeSql) { DataSet ds = new DataSet(); MySqlCommand cmd = new MySqlCommand(safeSql, Connection); //cmd.Parameters.AddRange(values); MySqlDataAdapter da = new MySqlDataAdapter(cmd); return da; } public static DataTable GetDataSet(string sql, params MySqlParameter[] values) { DataSet ds = new DataSet(); MySqlCommand cmd = new MySqlCommand(sql, Connection); cmd.Parameters.AddRange(values); MySqlDataAdapter da = new MySqlDataAdapter(cmd); da.Fill(ds); return ds.Tables[0]; } }}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)
