// ExecuteTransaction(使用事务执行指定的SQL)
public void ExecuteTransaction()
{
_dber.StartTransaction();
try
{
for (int i = 0; i < 3; i++)
{
var _parameters = new List()
{
_dber.CreateParameter("ID",i.ToString()),
_dber.CreateParameter("Name","Name123"),
_dber.CreateParameter("CreateTime",DateTime.Now),
};
var _sql = @"insert table1 (ID,Name,CreateTime) values (@ID,@Name,@CreateTime);";
var _isSucceed = _dber.ExecuteTransaction(_sql, _parameters);
}
}
catch (Exception ex)
{
_dber.RollbackTransaction();
return;
}
_dber.FinishTransaction();
}
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
ExecuteTransaction(使用指定的事务执行指定的SQL)
// ExecuteTransaction(使用指定的事务执行指定的SQL)
public void ExecuteTransaction()
{
for (int i = 0; i < 3; i++)
{
var _key = "Transaction" + i.ToString();
_dber.StartTransaction(_key);
var _id = i.ToString() + DateTime.Now.ToLongTimeString();
try
{
var _parameters = new List()
{
_dber.CreateParameter("ID",_id),
_dber.CreateParameter("Name","Name123"),
_dber.CreateParameter("CreateTime",DateTime.Now),
};
var _sql = @"insert table1 (ID,Name,CreateTime) values (@ID,@Name,@CreateTime);";
var _isSucceed = _dber.ExecuteTransaction(_key, _sql, _parameters);
}
catch (Exception ex)
{
_dber.RollbackTransaction(_key);
return;
}
if (!_dber.FinishTransaction(_key))
{
return;
}
}
}
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
RollbackTransaction(回滚指定的事务)
// RollbackTransaction(回滚指定的事务)
public void RollbackTransaction()
{
var _key = "Transaction";
_dber.StartTransaction(_key);
var _id = nameof(RollbackTransaction);
try
{
var _parameters = new List()
{
_dber.CreateParameter("ID",_id),
_dber.CreateParameter("Name","Name123"),
_dber.CreateParameter("CreateTime",DateTime.Now),
};
var _sql = @"insert table1 (ID,Name,CreateTime) values (@ID,@Name,@CreateTime);";
var _isSucceed = _dber.ExecuteTransaction(_key, _sql, _parameters);
}
catch (Exception ex)
{
_dber.RollbackTransaction(_key);
return;
}
_dber.RollbackTransaction(_key);
}
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
QueryTable(查询数据,并返回DataTable)
public DataTable QueryTable()
{
var _parameters = new List()
{
_dber.CreateParameter("ID",123),
};
var _sql = "select * from table1 where ID=@ID;";
return _dber.QueryTable(_sql, _parameters);
}
1
2
3
4
5
6
7
8
9
QueryTables(查询数据,并返回DataSet)
public DataSet QueryTables()
{
var _parameters = new List()
{
_dber.CreateParameter("ID",123),
};
var _sql = "select * from table1 where ID=@ID;";
_sql += "select * from table2 where ID=@ID;";
return _dber.QueryTables(_sql, _parameters);
}
1
2
3
4
5
6
7
8
9
10
QueryReader(查询数据,并返回DbDataReader)
public QueryReader QueryReader()
{
var _parameters = new List()
{
_dber.CreateParameter("ID",123),
};
var _sql = "select * from table1 where ID=@ID;";
return _dber.QueryReader(_sql, _parameters);
}
1
2
3
4
5
6
7
8
9
QueryDictionary (将查询结果的首两列数据转换为Dictionary)
public Dictionary QueryDictionary()
{
var _sql = "select Key,Value from table1 ;";
return _dber.QueryDictionary(_sql);
}
1
2
3
4
5
QueryValues (将查询结果的首列数据转换为List)
public List QueryNames()
{
var _sql = "select Names from table1 ;";
return _dber.QueryValues(_sql);
}
1
2
3
4
5
QueryValue (将查询值转换为long)
public List QueryValue()
{
var _sql = "select count(1) from table1 ;";
return _dber.QueryScalar(_sql);
}
1
2
3
4
5
IsExistData (查询是否存在数据)
public bool IsExistData()
{
var _sql = "select 1 from table1 where id=1;";
return _dber.IsExistData(_sql);
}
1
2
3
4
5
MySqlTexter使用
生成实例
// 用于生成MySql语句
IDbSqlTexter _sqltexter = new MySqlTexter();