天天看點

c#不能連mysql資料庫_c# 連接配接Mysql資料庫

c#不能連mysql資料庫_c# 連接配接Mysql資料庫

1 using System.Data;

2

3 using MySql.Data.MySqlClient;

4

5

6

7

8

9 private MySqlConnection conn;

10

11 private DataTable data;

12

13 private MySqlDataAdapter da;

14

15 private MySqlCommandBuilder cb;

16

17 private DataGrid dataGrid;

18

19

20

21 private void connectBtn_Click(object sender, System.EventArgs e)

22

23 {

24

25 if (conn != null)

26

27 conn.Close();

28

29

30

31 string connStr = String.Format("server={0};user id={1}; password={2}; port={3}; database=mysql; pooling=false; charset=utf8",

32

33 server.Text, userid.Text, password.Text, 3306);

34

35

36

37 try

38

39 {

40

41 conn = new MySqlConnection( connStr );

42

43 conn.Open();

44

45

46

47 GetDatabases();

48

49 MessageBox.Show("連接配接資料庫成功!");

50

51 }

52

53 catch (MySqlException ex)

54

55 {

56

57 MessageBox.Show( "Error connecting to the server: " + ex.Message );

58

59 }

60

61 }

62

63

64

65 private void GetDatabases()

66

67 {

68

69 MySqlDataReader reader = null;

70

71 MySqlCommand cmd = new MySqlCommand("SHOW DATABASES", conn);

72

73

74

75 try

76

77 {

78

79 reader = cmd.ExecuteReader();

80

81 databaseList.Items.Clear();

82

83 while (reader.Read())

84

85 {

86

87 databaseList.Items.Add( reader.GetString(0) );

88

89 }

90

91 }

92

93 catch (MySqlException ex)

94

95 {

96

97 MessageBox.Show("Failed to populate database list: " + ex.Message );

98

99 }

100

101 finally

102

103 {

104

105 if (reader != null) reader.Close();

106

107 }

108

109 }

110

111

112

113 private void databaseList_SelectedIndexChanged(object sender, System.EventArgs e)

114

115 {

116

117 MySqlDataReader reader = null;

118

119 conn.ChangeDatabase(databaseList.SelectedItem.ToString());

120

121 //http://sosoft.cnblogs.com/

122

123 MySqlCommand cmd = new MySqlCommand("SHOW TABLES", conn);

124

125 try

126

127 {

128

129 reader = cmd.ExecuteReader();

130

131 tables.Items.Clear();

132

133 while (reader.Read())

134

135 {

136

137 tables.Items.Add( reader.GetString(0) );

138

139 }

140

141 }

142

143 catch (MySqlException ex)

144

145 {

146

147 MessageBox.Show("Failed to populate table list: " + ex.Message );

148

149 }

150

151 finally

152

153 {

154

155 if (reader != null) reader.Close();

156

157 }

158

159 }

160

161

162

163 private void tables_SelectedIndexChanged(object sender, System.EventArgs e)

164

165 {

166

167 data = new DataTable();

168

169

170

171 da = new MySqlDataAdapter("SELECT * FROM " + tables.SelectedItem.ToString(), conn );

172

173 cb = new MySqlCommandBuilder( da ); // 此處必須有,否則無法更新

174

175

176

177 da.Fill( data );

178

179

180

181 dataGrid.DataSource = data;

182

183 }

184

185

186

187 private void updateBtn_Click(object sender, System.EventArgs e)

188

189 {

190

191 DataTable changes = data.GetChanges();

192

193 da.Update( changes );

194

195 data.AcceptChanges();

196

197 }

c#不能連mysql資料庫_c# 連接配接Mysql資料庫