-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2、新增:测试程序,方便高度 3、新增:测试数据库按钮——已通过测试
- Loading branch information
Showing
24 changed files
with
1,151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,269 @@ | ||
using MySql.Data.MySqlClient; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace SuperMap.ZS.Data | ||
{ | ||
public class DbHelperMySQL | ||
{ | ||
//数据库连接字符串 | ||
public static string Conn = Properties.Settings.Default.Connection; | ||
|
||
// 用于缓存参数的HASH表 | ||
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable()); | ||
|
||
/// <summary> | ||
/// 给定连接的数据库用假设参数执行一个sql命令(不返回数据集) | ||
/// </summary> | ||
/// <param name="connectionString">一个有效的连接字符串</param> | ||
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param> | ||
/// <param name="cmdText">存储过程名称或者sql命令语句</param> | ||
/// <param name="commandParameters">执行命令所用参数的集合</param> | ||
/// <returns>执行命令所影响的行数</returns> | ||
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) | ||
{ | ||
|
||
MySqlCommand cmd = new MySqlCommand(); | ||
|
||
using (MySqlConnection conn = new MySqlConnection(connectionString)) | ||
{ | ||
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); | ||
int val = cmd.ExecuteNonQuery(); | ||
cmd.Parameters.Clear(); | ||
return val; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 用现有的数据库连接执行一个sql命令(不返回数据集) | ||
/// </summary> | ||
/// <param name="connection">一个现有的数据库连接</param> | ||
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param> | ||
/// <param name="cmdText">存储过程名称或者sql命令语句</param> | ||
/// <param name="commandParameters">执行命令所用参数的集合</param> | ||
/// <returns>执行命令所影响的行数</returns> | ||
public static int ExecuteNonQuery(MySqlConnection connection, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) | ||
{ | ||
|
||
MySqlCommand cmd = new MySqlCommand(); | ||
|
||
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); | ||
int val = cmd.ExecuteNonQuery(); | ||
cmd.Parameters.Clear(); | ||
return val; | ||
} | ||
|
||
/// <summary> | ||
///使用现有的SQL事务执行一个sql命令(不返回数据集) | ||
/// </summary> | ||
/// <remarks> | ||
///举例: | ||
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24)); | ||
/// </remarks> | ||
/// <param name="trans">一个现有的事务</param> | ||
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param> | ||
/// <param name="cmdText">存储过程名称或者sql命令语句</param> | ||
/// <param name="commandParameters">执行命令所用参数的集合</param> | ||
/// <returns>执行命令所影响的行数</returns> | ||
public static int ExecuteNonQuery(MySqlTransaction trans, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) | ||
{ | ||
MySqlCommand cmd = new MySqlCommand(); | ||
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters); | ||
int val = cmd.ExecuteNonQuery(); | ||
cmd.Parameters.Clear(); | ||
return val; | ||
} | ||
|
||
/// <summary> | ||
/// 用执行的数据库连接执行一个返回数据集的sql命令 | ||
/// </summary> | ||
/// <remarks> | ||
/// 举例: | ||
/// MySqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24)); | ||
/// </remarks> | ||
/// <param name="connectionString">一个有效的连接字符串</param> | ||
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param> | ||
/// <param name="cmdText">存储过程名称或者sql命令语句</param> | ||
/// <param name="commandParameters">执行命令所用参数的集合</param> | ||
/// <returns>包含结果的读取器</returns> | ||
public static MySqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) | ||
{ | ||
//创建一个MySqlCommand对象 | ||
MySqlCommand cmd = new MySqlCommand(); | ||
//创建一个MySqlConnection对象 | ||
MySqlConnection conn = new MySqlConnection(connectionString); | ||
|
||
//在这里我们用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在, | ||
//因此commandBehaviour.CloseConnection 就不会执行 | ||
try | ||
{ | ||
//调用 PrepareCommand 方法,对 MySqlCommand 对象设置参数 | ||
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); | ||
//调用 MySqlCommand 的 ExecuteReader 方法 | ||
MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); | ||
//清除参数 | ||
cmd.Parameters.Clear(); | ||
return reader; | ||
} | ||
catch | ||
{ | ||
//关闭连接,抛出异常 | ||
conn.Close(); | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 返回DataSet | ||
/// </summary> | ||
/// <param name="connectionString">一个有效的连接字符串</param> | ||
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param> | ||
/// <param name="cmdText">存储过程名称或者sql命令语句</param> | ||
/// <param name="commandParameters">执行命令所用参数的集合</param> | ||
/// <returns></returns> | ||
public static DataSet GetDataSet(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) | ||
{ | ||
//创建一个MySqlCommand对象 | ||
MySqlCommand cmd = new MySqlCommand(); | ||
//创建一个MySqlConnection对象 | ||
MySqlConnection conn = new MySqlConnection(connectionString); | ||
|
||
//在这里我们用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在, | ||
|
||
try | ||
{ | ||
//调用 PrepareCommand 方法,对 MySqlCommand 对象设置参数 | ||
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); | ||
//调用 MySqlCommand 的 ExecuteReader 方法 | ||
MySqlDataAdapter adapter = new MySqlDataAdapter(); | ||
adapter.SelectCommand = cmd; | ||
DataSet ds = new DataSet(); | ||
|
||
adapter.Fill(ds); | ||
//清除参数 | ||
cmd.Parameters.Clear(); | ||
conn.Close(); | ||
return ds; | ||
} | ||
catch (Exception e) | ||
{ | ||
throw e; | ||
} | ||
} | ||
|
||
|
||
|
||
/// <summary> | ||
/// 用指定的数据库连接字符串执行一个命令并返回一个数据集的第一列 | ||
/// </summary> | ||
/// <remarks> | ||
///例如: | ||
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24)); | ||
/// </remarks> | ||
///<param name="connectionString">一个有效的连接字符串</param> | ||
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param> | ||
/// <param name="cmdText">存储过程名称或者sql命令语句</param> | ||
/// <param name="commandParameters">执行命令所用参数的集合</param> | ||
/// <returns>用 Convert.To{Type}把类型转换为想要的 </returns> | ||
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) | ||
{ | ||
MySqlCommand cmd = new MySqlCommand(); | ||
|
||
using (MySqlConnection connection = new MySqlConnection(connectionString)) | ||
{ | ||
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); | ||
object val = cmd.ExecuteScalar(); | ||
cmd.Parameters.Clear(); | ||
return val; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 用指定的数据库连接执行一个命令并返回一个数据集的第一列 | ||
/// </summary> | ||
/// <remarks> | ||
/// 例如: | ||
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24)); | ||
/// </remarks> | ||
/// <param name="connection">一个存在的数据库连接</param> | ||
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param> | ||
/// <param name="cmdText">存储过程名称或者sql命令语句</param> | ||
/// <param name="commandParameters">执行命令所用参数的集合</param> | ||
/// <returns>用 Convert.To{Type}把类型转换为想要的 </returns> | ||
public static object ExecuteScalar(MySqlConnection connection, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) | ||
{ | ||
|
||
MySqlCommand cmd = new MySqlCommand(); | ||
|
||
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); | ||
object val = cmd.ExecuteScalar(); | ||
cmd.Parameters.Clear(); | ||
return val; | ||
} | ||
|
||
/// <summary> | ||
/// 将参数集合添加到缓存 | ||
/// </summary> | ||
/// <param name="cacheKey">添加到缓存的变量</param> | ||
/// <param name="commandParameters">一个将要添加到缓存的sql参数集合</param> | ||
public static void CacheParameters(string cacheKey, params MySqlParameter[] commandParameters) | ||
{ | ||
parmCache[cacheKey] = commandParameters; | ||
} | ||
|
||
/// <summary> | ||
/// 找回缓存参数集合 | ||
/// </summary> | ||
/// <param name="cacheKey">用于找回参数的关键字</param> | ||
/// <returns>缓存的参数集合</returns> | ||
public static MySqlParameter[] GetCachedParameters(string cacheKey) | ||
{ | ||
MySqlParameter[] cachedParms = (MySqlParameter[])parmCache[cacheKey]; | ||
|
||
if (cachedParms == null) | ||
return null; | ||
|
||
MySqlParameter[] clonedParms = new MySqlParameter[cachedParms.Length]; | ||
|
||
for (int i = 0, j = cachedParms.Length; i < j; i++) | ||
clonedParms[i] = (MySqlParameter)((ICloneable)cachedParms[i]).Clone(); | ||
|
||
return clonedParms; | ||
} | ||
|
||
/// <summary> | ||
/// 准备执行一个命令 | ||
/// </summary> | ||
/// <param name="cmd">sql命令</param> | ||
/// <param name="conn">OleDb连接</param> | ||
/// <param name="trans">OleDb事务</param> | ||
/// <param name="cmdType">命令类型例如 存储过程或者文本</param> | ||
/// <param name="cmdText">命令文本,例如:Select * from Products</param> | ||
/// <param name="cmdParms">执行命令的参数</param> | ||
private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, CommandType cmdType, string cmdText, MySqlParameter[] cmdParms) | ||
{ | ||
|
||
if (conn.State != ConnectionState.Open) | ||
conn.Open(); | ||
|
||
cmd.Connection = conn; | ||
cmd.CommandText = cmdText; | ||
|
||
if (trans != null) | ||
cmd.Transaction = trans; | ||
|
||
cmd.CommandType = cmdType; | ||
|
||
if (cmdParms != null) | ||
{ | ||
foreach (MySqlParameter parm in cmdParms) | ||
cmd.Parameters.Add(parm); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// 有关程序集的一般信息由以下 | ||
// 控制。更改这些特性值可修改 | ||
// 与程序集关联的信息。 | ||
[assembly: AssemblyTitle("SuperMap.ZS.Data")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("SuperMap.ZS.Data")] | ||
[assembly: AssemblyCopyright("Copyright © 2018")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// 将 ComVisible 设置为 false 会使此程序集中的类型 | ||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 | ||
//请将此类型的 ComVisible 特性设置为 true。 | ||
[assembly: ComVisible(false)] | ||
|
||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID | ||
[assembly: Guid("97c3aa1f-5a35-401c-8fc5-09f7e11f5b3c")] | ||
|
||
// 程序集的版本信息由下列四个值组成: | ||
// | ||
// 主版本 | ||
// 次版本 | ||
// 生成号 | ||
// 修订号 | ||
// | ||
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 | ||
//通过使用 "*",如下所示: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
39 changes: 39 additions & 0 deletions
39
SuperMap.ZS/SuperMap.ZS.Data/Properties/Settings.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version='1.0' encoding='utf-8'?> | ||
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="SuperMap.ZS.Data.Properties" GeneratedClassName="Settings"> | ||
<Profiles /> | ||
<Settings> | ||
<Setting Name="Connection" Type="System.String" Scope="User"> | ||
<Value Profile="(Default)">Database='zsdatasource';Data Source='localhost';User Id='root';Password='admin';charset='utf8';pooling=true</Value> | ||
</Setting> | ||
</Settings> | ||
</SettingsFile> |
Oops, something went wrong.