首先是安装JDBC操作数据库的包,,当然自己看着办哈,可以自己下载以后导入,或者直接让软件本身下载
第一种方式
第二种 咱自己下载个低版本的
点击这个链接
点击以后呢可以直接下载下来,然后导入(大家百度怎么导入.dll哈)
也可以直接让软件自己弄
安装好了
namespace MqttDemo { class DataBase { public static MySqlConnection getMySqlCon(string username, string password, string ip, string port, string iotdatebase, string charset) { String connetStr = "server= " + ip + ";port=" + port + ";user =" + username + ";password=" + password + ";database =" + iotdatebase + ";charset =" + charset; // String mySqlCon = ConfigurationManager.ConnectionStrings["MySqlCon"].ConnectionString; MySqlConnection mysql = new MySqlConnection(connetStr); return mysql; } public static void getInsert(MySqlCommand mySqlCommand) { try { mySqlCommand.ExecuteNonQuery(); } catch (Exception ex) { String message = ex.Message; Console.WriteLine("插入数据失败了!" + message); } } //插入SQL组合 public static String groupInsert(String from, params String[] arg) { String Value = arg[0]; bool bl = false; foreach (String str in arg) { if (bl == true) Value = Value + "," + str; else bl = true; } String sql = "insert into " + from + " values(NULL," + Value + ")"; return sql; } public static String groupInsert(String from, String[] option, String[] value) { String Option = option[0]; String Value = "'" + value[0]; bool bl = false; foreach (String str in option) { if (bl == true) Option = Option + "," + str; else bl = true; } bl = false; foreach (String str in value) { if (bl == true) Value = Value + "'" + "," + "'" + str; else bl = true; } Value = Value + "'"; String sql = "insert into " + from + "(" + Option + ")" + " values(" + Value + ")"; return sql; } } }
点击按钮连接数据库
private void button18_Click(object sender, EventArgs e) { string name = textBox21.Text.ToString(); string password = textBox22.Text.ToString(); string ip = textBox23.Text.ToString(); string port = textBox24.Text.ToString(); string database = textBox26.Text.ToString(); string charset = textBox27.Text.ToString(); if (name.Length > 0) { if (password.Length > 0) { if (ip.Length > 0) { if (port.Length > 0) { if (database.Length > 0) { if (charset.Length > 0) { if (button18.Text == "连接") { mySqlConnection = DataBase.getMySqlCon(name, password, ip, port, database, charset); mySqlConnection.Open(); //MessageBox.Show("连接了", "234324"); button18.Text = "断开"; } else { button18.Text = "连接"; try { mySqlConnection.Close(); } catch (Exception) { throw; } } } else { MessageBox.Show("编码方式不能为空", "提示!"); } } else { MessageBox.Show("数据库名字不能为空", "提示!"); } } else { MessageBox.Show("端口号不能为空", "提示!"); } } else { MessageBox.Show("密码不能为空", "提示!"); } } else { MessageBox.Show("密码不能为空", "提示!"); } } else { MessageBox.Show("用户名不能为空", "提示!"); } }
。。。不能写了,忘了一件事情。。需要先安装数据库