-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySQLConnector.cs
81 lines (75 loc) · 3.52 KB
/
MySQLConnector.cs
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using MySql.Data.MySqlClient;
namespace Daber
{
class MySQLConnector:IConnector
{
protected string connString;
public MySQLConnector(string connString)
{
this.connString = connString;
}
public DbConnection Connect(string connString)
{
return new MySqlConnection(connString);
}
public int Insert(DbCommand cmd, string table, string cols, string vals, out long id)
{
cmd.CommandText = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", table, sbCols.ToString(), sbVals.ToString());
int rows = cmd.ExecuteNonQuery();
cmd.CommandText = "SELECT LAST_INSERT_ID()";
object o = cmd.ExecuteScalar();
id = Convert.ToInt64(o);
return rows;
}
public void AddParameter(DbCommand dbCmd, string parName, object oVal)
{
MySqlCommand cmd = (MySqlCommand)dbCmd;
if (oVal == null)
cmd.Parameters.Add(parName, MySqlDbType.VarChar).Value = DBNull.Value;
else if (oVal.GetType() == typeof(string))
cmd.Parameters.Add(parName, MySqlDbType.VarChar).Value = oVal;
else if (oVal.GetType() == typeof(int))
cmd.Parameters.Add(parName, MySqlDbType.Int32).Value = oVal;
else if (oVal.GetType() == typeof(UInt32))
cmd.Parameters.Add(parName, MySqlDbType.UInt32).Value = oVal;
else if (oVal.GetType() == typeof(ulong))
cmd.Parameters.Add(parName, MySqlDbType.Int64).Value = oVal;
else if (oVal.GetType() == typeof(float))
cmd.Parameters.Add(parName, MySqlDbType.Float).Value = oVal;
else if (oVal.GetType() == typeof(double))
cmd.Parameters.Add(parName, MySqlDbType.Decimal).Value = oVal;
else if (oVal.GetType() == typeof(DateTime))
cmd.Parameters.Add(parName, MySqlDbType.DateTime).Value = oVal;
else if (oVal.GetType() == typeof(Int64))
cmd.Parameters.Add(parName, MySqlDbType.Int64).Value = oVal;
else if (oVal is bool)
cmd.Parameters.Add(parName, MySqlDbType.Bit).Value = Convert.ToBoolean(oVal);
else if (oVal is byte)
cmd.Parameters.Add(parName, MySqlDbType.Byte).Value = Convert.ToByte(oVal);
else if (oVal is Guid)
cmd.Parameters.Add(parName, MySqlDbType.Guid).Value = oVal;
else if (oVal is short)
cmd.Parameters.Add(parName, MySqlDbType.Int16).Value = oVal;
else if (oVal.GetType().IsEnum)
{
System.Type type = oVal.GetType();
if (Enum.GetUnderlyingType(type) == typeof(Byte))
cmd.Parameters.Add(parName, MySqlDbType.Byte).Value = Convert.ToByte(oVal);
else if (Enum.GetUnderlyingType(type) == typeof(short))
cmd.Parameters.Add(parName, MySqlDbType.Int16).Value = oVal;
else if (Enum.GetUnderlyingType(type) == typeof(int))
cmd.Parameters.Add(parName, MySqlDbType.Int32).Value = oVal;
else
throw new Exception("Cannot handle " + oVal.GetType().ToString() + " in addParameter");
}
else
{
throw new Exception("Cannot handle " + oVal.GetType().ToString() + " in addParameter");
}
}
}
}