Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
forked from 盘古软件/SharpSCADA
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (2)
master
develop
master
Branches (2)
master
develop
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (2)
master
develop
SharpSCADA
/
src
/
Program
/
DataHelper
/
DataHelper.cs
SharpSCADA
/
src
/
Program
/
DataHelper
/
DataHelper.cs
DataHelper.cs 8.81 KB
Copy Edit Raw Blame History
panvsoft authored 2020年04月10日 22:26 +08:00 . 初始版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
namespace DatabaseLib
{
public static class DataHelper
{
static string m_ConnStr = @"Data Source=.\SQLEXPRESS;Initial Catalog=SharpSCADA;Integrated Security=True";
static string m_Path = @"D:\HDA";
static string m_host = Environment.MachineName;
static string m_type = "MSSQL";
//数据库工厂接口
const string CFGPATH = @"C:\DataConfig\host.cfg";
const string INIPATH = @"C:\DataConfig\host.ini";
const string DATALOGSOURCE = "Data Operations";
const string DATALOGNAME = "Data Log";
const int STRINGMAX = 255;
static EventLog Log;
#region GetInstance
private static IDataFactory _ins;
public static IDataFactory Instance
{
get
{
return _ins;
}
}
public static string HostName
{
get { return m_host; }
}
public static string ConnectString
{
get { return m_ConnStr; }
}
public static string HdaPath
{
get { return m_Path; }
}
#endregion
/// <summary>
/// 数据库工厂构造函数
/// </summary>
/// <param name="dbtype">数据库枚举</param>
static DataHelper()
{
if (!EventLog.SourceExists(DATALOGSOURCE))
EventLog.CreateEventSource(DATALOGSOURCE, DATALOGNAME);
Log = new EventLog(DATALOGNAME);
try
{
if (File.Exists(INIPATH))
{
StringBuilder sb = new StringBuilder(STRINGMAX);
WinAPI.GetPrivateProfileString("HOST", "SERVER", m_host, sb, STRINGMAX, INIPATH);
m_host = sb.ToString();
WinAPI.GetPrivateProfileString("DATABASE", "CONNSTRING", m_ConnStr, sb, STRINGMAX, INIPATH);
m_ConnStr = sb.ToString();
WinAPI.GetPrivateProfileString("DATABASE", "ARCHIVE", m_Path, sb, STRINGMAX, INIPATH);
m_Path = sb.ToString();
WinAPI.GetPrivateProfileString("DATABASE", "TYPE", m_type, sb, STRINGMAX, INIPATH);
m_type = sb.ToString();
}
else if (File.Exists(CFGPATH))
{
using (StreamReader objReader = new StreamReader(CFGPATH))
{
m_host = objReader.ReadLine();
m_ConnStr = objReader.ReadLine();
m_Path = objReader.ReadLine();
}
}
IPAddress addr;
if (string.IsNullOrEmpty(m_host) || !IPAddress.TryParse(m_host, out addr))
{
m_host = Environment.MachineName;
}
switch (m_type.ToUpper())
{
case "MSSQL":
_ins = new MssqlFactory();
break;
case "MYSQL":
_ins = new MysqlFactory();
break;
default:
_ins = new MssqlFactory();
break;
}
}
catch (Exception e)
{
AddErrorLog(e);
}
}
public static DbParameter CreateParam(string paramName, SqlDbType dbType, object objValue, int size = 0, ParameterDirection direction = ParameterDirection.Input)
{
return _ins.CreateParam(paramName, dbType, objValue, size, direction);
}
public static string DataTableToCsv(DataTable table)
{
//以半角逗号(即,)作分隔符,列为空也要表达其存在。
//列内容如存在半角逗号(即,)则用半角引号(即"")将该字段值包含起来。
//列内容如存在半角引号(即")则应替换成半角双引号("")转义,并用半角引号(即"")将该字段值包含起来。
StringBuilder sb = new StringBuilder();
DataColumn colum;
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
colum = table.Columns[i];
if (i != 0) sb.Append(",");
var txt = row[colum] == null ? "" : row[colum].ToString();
if (colum.DataType == typeof(string) && txt.Contains(","))
{
sb.Append("\"" + txt.Replace("\"", "\"\"") + "\"");
}
else sb.Append(txt);
}
sb.AppendLine();
}
return sb.ToString();
}
public static string ReaderToCsv(IDataReader reader)
{
StringBuilder sb = new StringBuilder();
var colcount = reader.FieldCount;
while (reader.Read())
{
for (int i = 0; i < colcount; i++)
{
if (i != 0) sb.Append(",");
var txt = reader[i] == null ? "" : reader[i].ToString();
if (txt.Contains(","))
{
sb.Append("\"" + txt.Replace("\"", "\"\"") + "\"");
}
else sb.Append(txt);
}
sb.AppendLine();
}
return sb.ToString();
}
public static void AddErrorLog(Exception e)
{
string err = "";
Exception exp = e;
while (exp != null)
{
err += string.Format("\n {0}", exp.Message);
exp = exp.InnerException;
}
err += string.Format("\n {0}", e.StackTrace);
Log.Source = DATALOGSOURCE;
Log.WriteEntry(err, EventLogEntryType.Error);
}
public static string GetNullableString(this DbDataReader reader, int index)
{
SqlDataReader dataReader = reader as SqlDataReader;
if (dataReader != null)
{
var svr = dataReader.GetSqlString(index);
return svr.IsNull ? null : svr.Value;
}
else return reader.GetString(index);
}
public static DateTime? GetNullableTime(this DbDataReader reader, int index)
{
SqlDataReader dataReader = reader as SqlDataReader;
if (dataReader == null) return reader.GetDateTime(index);
var svr = dataReader.GetSqlDateTime(index);
return svr.IsNull ? default(Nullable<DateTime>) : svr.Value;
}
public static int GetTimeTick(this DbDataReader reader, int index)
{
SqlDataReader dataReader = reader as SqlDataReader;
if (dataReader != null)
{
return dataReader.GetSqlDateTime(index).TimeTicks;
}
var datetime = reader.GetDateTime(index);
var value = datetime.Subtract(new DateTime(1900, 1, 1));
long num2 = value.Ticks - value.Days * 864000000000;
if (num2 < 0)
num2 += 864000000000;
int num3 = (int)(num2 / 10000.0 * 0.3 + 0.5);
if (num3 > 300 * 60 * 60 * 24 - 1)
num3 = 0;
return num3;
}
public static object GetSqlValue(this DbDataReader reader, int index)
{
SqlDataReader dataReader = reader as SqlDataReader;
if (dataReader != null)
{
return dataReader.GetSqlValue(index);
}
var mq = reader as MySqlDataReader;
if (mq != null)
{
return mq.GetValue(index);
}
return "";
}
}
public static class WinAPI
{
//参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;filePath:INI文件的完整的路径和名称。
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
//参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。
[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

一款工业级轻量级组态软件 1.轻量级工控网关: 支持当前几种主要的工业协议如西门子的Profinet、AB的EtherNetIPs、施耐德的Modbus和OPC。采用类OPC接口网关。 2.数据采集、归档、预警及配置工具 支持实时数据采集、历史数据归档、变量触发预警,并使用TagConfig工具简单的配置实现。 3.人机界面(设计时和运行时)。
No labels
GPL-3.0
Use GPL-3.0
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/Debugman/SharpSCADA.git
git@gitee.com:Debugman/SharpSCADA.git
Debugman
SharpSCADA
SharpSCADA
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /