-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMssqlHelper.cs
More file actions
125 lines (113 loc) · 3.75 KB
/
Copy pathMssqlHelper.cs
File metadata and controls
125 lines (113 loc) · 3.75 KB
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace RADARMRM
{
public class MssqlHelper
{
private string m_sConnStr;
protected SqlConnection m_pSqlConn;
public MssqlHelper(string sConnStr)
{
m_sConnStr = sConnStr;
m_pSqlConn = new SqlConnection(m_sConnStr);
}
/// <summary>
/// 执行SQL语句,返回数据到DataSet中
/// </summary>
/// <param name="sSQL"></param>
/// <returns></returns>
public DataSet GetDataSet(string sSQL)
{
DataSet dataSet = null;
try
{
m_pSqlConn.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter(sSQL, m_pSqlConn);
dataSet = new DataSet();
sqlDA.Fill(dataSet);
}
catch
{
;
}
finally
{
m_pSqlConn.Close();
}
return dataSet;
}
/// <summary>
/// 执行SQL语句,返回 DataReader
/// </summary>
/// <param name="sSQL"></param>
/// <returns></returns>
public SqlDataReader GetDataReader(string sSQL)
{
m_pSqlConn.Open();
SqlCommand command = new SqlCommand(sSQL, m_pSqlConn);
SqlDataReader dataReader = command.ExecuteReader();
return dataReader;
}
public bool ExecuteSql(string sSQL)
{
bool bRt = false;
SqlTransaction myTrans = null;
try
{
m_pSqlConn.Open();
myTrans = m_pSqlConn.BeginTransaction();
SqlCommand Cmd = new SqlCommand(sSQL, m_pSqlConn, myTrans);
Cmd.ExecuteNonQuery();
myTrans.Commit();
bRt = true;
}
catch (SqlException ex)
{
if (myTrans != null)
myTrans.Rollback();
}
finally
{
m_pSqlConn.Close();
}
return bRt;
}
public void Replace()
{
this.m_pSqlConn.Close();
}
public void InsertRaderMessage(string overall, string radar, string vibration, string licensePlate)
{
try
{
SqlCommand command = new SqlCommand();
command.Connection = this.m_pSqlConn;
command.Parameters.Add("@time", SqlDbType.SmallDateTime);
command.Parameters.Add("@overall", SqlDbType.NChar);
command.Parameters.Add("@radar", SqlDbType.NChar);
command.Parameters.Add("@vibration", SqlDbType.NChar);
command.Parameters.Add("@licensePlate", SqlDbType.NChar);
command.CommandText = "INSERT INTO result (time, ALARM_overall, ALARM_radar, ALARM_vibration, licensePlate) Values (@time, @overall, @radar, @vibration, @licensePlate)";
command.Parameters[0].Value = DateTime.Now;
command.Parameters[1].Value = overall;
if (radar == null)
command.Parameters[2].Value = DBNull.Value;
else
command.Parameters[2].Value = radar;
if (vibration == null)
command.Parameters[3].Value = DBNull.Value;
else
command.Parameters[3].Value = vibration;
command.Parameters[4].Value = licensePlate;
command.ExecuteNonQuery();
}
catch (Exception e)
{
}
}
}
}