mysql excel dbhepler_mysql 中写dbhelper

本文档展示了如何使用C#编写一个简单的数据库访问层(DBAL),包括连接管理、SQL执行、数据获取和数据集处理。核心内容包括MySqlConnection的使用、执行命令、获取 scalar 值和读取数据。
摘要由CSDN通过智能技术生成

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using MySql.Data.MySqlClient;

namespace i_salesDAL

{

public class DBHelper

{

//引导数据库连接数据库调用Web.Config文件

private static MySqlConnection connection;

//创建连接

public static MySqlConnection Connection

{

get

{

MySqlConnection myConn = new MySqlConnection("server=localhost;user id=root;password=root;database=google;CharSet=gb2312;");

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 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];         }     } }

using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace MyOfficeDAL { public static class DBHelper { private static SqlConnection connection; public static SqlConnection Connection { get { string connectionString = ConfigurationManager.ConnectionStrings["MyOfficeConnectionString"].ConnectionString; connection = new SqlConnection(connectionString); if (connection == null) { 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) { SqlCommand cmd = new SqlCommand(safeSql, Connection); int result = cmd.ExecuteNonQuery(); return result; } public static int ExecuteCommand(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); return cmd.ExecuteNonQuery(); } public static string ReturnStringScalar(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); try { string result = cmd.ExecuteScalar().ToString(); return result; } catch (Exception ex) { return "0"; } connection.Close(); } public static int GetScalar(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); try { int result = Convert.ToInt32(cmd.ExecuteScalar()); return result; } catch (Exception e) { return 0; } connection.Close(); } public static int GetScalar(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); int result = Convert.ToInt32(cmd.ExecuteScalar()); return result; connection.Close(); } public static SqlDataReader GetReader(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); SqlDataReader reader = cmd.ExecuteReader(); return reader; reader.Close(); reader.Dispose(); } public static SqlDataReader GetReader(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); SqlDataReader reader = cmd.ExecuteReader(); return reader; reader.Close(); reader.Dispose(); } public static DataTable GetDataSet(string safeSql) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(safeSql, Connection); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); connection.Close(); connection.Dispose(); return ds.Tables[0]; } public static DataTable GetDataSet(string sql, params SqlParameter[] values) { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); connection.Close(); connection.Dispose(); return ds.Tables[0]; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值