zoukankan      html  css  js  c++  java
  • 第一个Net+Mysql的例子,比想象的简单很多

    1.window下安装mysql,比较简单,完全的图形化界面,不用看文档一路点击下来也ok,注意中间几个configtype选项就可以。

    2.安装MySql Net的驱动程序程序,安装完后就是几个dll,添加到vs项目的引用中就行,MySql.Data。dll,MySql.Data.Entity.Dll。

    3.在mysql.data命名空间下,可以对mysql数据库进行数据操作,原理和语法和sqlserver的,sqlclient完全一样,只是命名空间和类不一样

    这是我写mysqlhelper的第一个例子类

     public static class MySqlHelper
        {
            public static int ExecuteNonQuery(string connectionString, CommandType commandtype, string commandText)
            {
                return ExecuteNonQuery(connectionString, commandtype, commandText, null);
            }
    
            public static int ExecuteNonQuery(string connectionString, CommandType commandtype, string commandText, params MySqlParameter[] commandParameters)
            {
                if (string.IsNullOrEmpty(connectionString))
                {
                    throw new Exception("connectionString exception");
                }
                int result = 0;
                MySqlConnection con = null;
                try
                {
                    using (con = new MySqlConnection(connectionString))
                    {
                        con.Open();
                        MySqlCommand command = new MySqlCommand(commandText, con);
                        command.CommandType = commandtype;
                        result = command.ExecuteNonQuery();
                    }
                    return result;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (con.State == ConnectionState.Open)
                    {
                        con.Close();
                    }
                }
                
            }
        }

    4.操作方法,给表name添加一个新纪录,很简单,查询字符串的表达方式和sqlserver完全一致。

               string connectionString = "server=localhost;uid=root;pwd=111111;database=test;";

                string sql = "insert name(name) values('hello')";
                MySqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sql);
                Console.Read();

    另外大家都推荐的mysql图形管理工具室navicat For Mysql,我感觉这个挺简单,操作很容易上手

    http://www.connectionstrings.com/mysql

  • 相关阅读:
    Markdown学习笔记
    Go 学习笔记(一)
    case中定义变量
    <转>MySql 与Oracle区别
    Java 时间转换问题总结
    线程之间共享
    并发编程快速入门
    redis主从复制
    jedis操作redis
    redis持久化方案
  • 原文地址:https://www.cnblogs.com/zjypp/p/3327170.html
Copyright © 2011-2022 走看看