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

  • 相关阅读:
    [python subprocess学习篇] 调用系统命令
    linux dd命令创建一定大小的文件
    [linux time命令学习篇] time 统计命令执行的时间
    新建应用母版页的网页index.aspx,about.aspx,login.aspx
    MOSS母板页制作 学习笔记(一)
    SharePoint 2010顶部链接导航栏的详细操作
    使用SharePoint 2010 母版页
    SharePoint 2010 母版页制作的简单介绍
    在 SharePoint 2010 中访问数据
    牛刀小试、用SharePoint 实现请假管理功能
  • 原文地址:https://www.cnblogs.com/zjypp/p/3327170.html
Copyright © 2011-2022 走看看