C#操作MySQL的工具类MySqlHelper


本文摘自PHP中文网,作者大家讲道理,侵删。

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.Linq; 

using System.Text; 

using MySql.Data.MySqlClient; 

using System.Data; 

class MySqlHelper:IDisposable 

    

        private MySqlConnection m_conn = null

        private MySqlTransaction m_trans = null

        private bool m_tran_enabled = false

    

    

        public MySqlHelper() 

        

            m_conn = new MySqlConnection(); 

            m_conn.ConnectionString = "Server=localhost;Port=3301;Uid=sa;Pwd=000"

            m_conn.Open(); 

        

    

    

        public void BeginTrans() 

        

            m_trans = m_conn.BeginTransaction(); 

            m_tran_enabled = true

        

    

    

        public void Commit() 

        

            if (m_trans != null && m_tran_enabled) 

            

                m_tran_enabled = false

                m_trans.Commit(); 

            

        

    

    

        public void Rollback() 

        

            if (m_trans != null && m_tran_enabled) 

            

                m_tran_enabled = false

                m_trans.Rollback(); 

            

        

    

    

        public object QuerySome(string sql,int fieldindex) 

        

            using (MySqlCommand cmd = new MySqlCommand(sql, m_conn)) 

            

                using (MySqlDataReader sr = cmd.ExecuteReader()) 

                

                    if (sr.Read()) 

                    

                        return sr.GetValue(fieldindex); 

                    

                

            

            return null

        

    

    

        public delegate void FillValues(MySqlDataReader sr); 

            

        public void QuerySomes(string sql, FillValues fill) 

        

            using (MySqlCommand cmd = new MySqlCommand(sql, m_conn)) 

            

                using (MySqlDataReader sr = cmd.ExecuteReader()) 

                

                    fill(sr); 

                

            

        

    

    

        public DataTable Source(string sql) 

        

            DataTable dt = null

            MySqlCommand cmd = null

            MySqlDataAdapter ad = null

            try

            

                lock (dt = new DataTable()) 

                

                    cmd = new MySqlCommand(sql, m_conn); 

                    ad = new MySqlDataAdapter((MySqlCommand)cmd); 

                    dt.Clear(); 

                    ad.Fill(dt); 

                

            

            catch (Exception e) 

            

                throw e; 

            

            return dt; 

        

    

    

        public void ExecProc(string proc, params MySqlParameter[] ps) 

        

            using (MySqlCommand cmd = new MySqlCommand(proc, m_conn)) 

            

                cmd.CommandType = System.Data.CommandType.StoredProcedure; 

                foreach (MySqlParameter p in ps) 

                

                    cmd.Parameters.Add(p); 

                

                cmd.ExecuteNonQuery(); 

            

        

    

    

        void IDisposable.Dispose() 

        

            m_conn.Close(); 

            m_conn.Dispose(); 

            if (m_trans != null

            

                m_trans.Dispose(); 

            

        

    }

相关阅读 >>

c#分别用前序遍历、中序遍历和后序遍历打印二叉树

c#实现图标锁定到windows任务栏或删除图标

c#操作mysql的工具类mysqlhelper

c#访问sqlserver的工具类sqlserverhelper

常用的c#类

c# 识别url是否是网络路径

c#读取中文文件乱码的解方法

c#自定读取配置文件类

c#中登陆账户使用的md5加密算法

c#将文件上传、下载(以二进制流保存到数据库)

更多相关阅读请进入《代码片段》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...