• .net连接mysql


    首先在官网下载,mysql-connect-net,用于使用mysql的驱动程序,我在下载mysql-connect-net.msi. installer后,执行安装程序的时候一直无法安装成功,最简单的方法是直接下载.zip文件后解压,无须安装。

    官网地址:http://dev.mysql.com/downloads/file/?id=463757

    解压文件后,

    出现了好几个文件夹,其中有v4和v4.5两个文件夹,对应vs的不同版本

    VS2010使用V4.0下的dll文件
    VS2012/2013/2015使用v4.5下的dll文件

    其中有一个帮助手册十分有用:

    Documentation文件夹下的ConnectorNET.chm中包含了连接mysql数据库的API。

    MySqlConnection类用来连接数据库

    Constructor:

    构造函数

     

      MySqlConnection(String)
    Initializes a new instance of theMySqlConnection class when given a string containing the connection string. 
    methods:

    打开数据库

     

      Open
    Opens a database connection with the property settings specified by the ConnectionString.
    (Overrides DbConnection.Open().)

    关闭数据库

     

      Close
    Closes the connection to the database. This is the preferred method of closing any open connection.
    (Overrides DbConnection.Close().)

    结果在编译运行的时候,出现警告

    请将项目文件中的“AutoGenerateBindingRedirects”属性设置为 true 

    这个可以找到vs该工程的文件夹下的csproj文件中增加<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

    警告即可消除。

    也可以参考该网址http://www.cnblogs.com/zoro-zero/p/5867320.html解决问题

    constructor:

     

      MySqlCommand(String, MySqlConnection)
    Initializes a new instance of theMySqlCommand class with the text of the query and aMySqlConnection

     

      MySqlCommand(String, MySqlConnection, MySqlTransaction)
    Initializes a new instance of theMySqlCommand class with the text of the query, aMySqlConnection, and theMySqlTransaction.  
    methods:

    执行sql语句

     

      ExecuteNonQuery
    Executes a SQL statement against the connection and returns the number of rows affected.
    (Overrides DbCommand.ExecuteNonQuery().)
    property:

    设置或返回sql语句

    CommandText
    Gets or sets the SQL statement to execute at the data source.
    (Overrides DbCommand.CommandText.)
    通过改变CommandText内容,使MysqlCommand类能够执行多条sql语句。

     

    [csharp] view plain copy
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  
    6. //////////////////////////////////////  
    7. using MySql.Data;  
    8. using MySql.Data.MySqlClient;  
    9. //建议使用mysqlClient模式,如果连接的数据库是mysql的话  
    10. //在C#中,如果想连接数据库的话,需要使用Connection连接对象。同样,不同的连接模式下,所使用的连接对象也不同  
    11. //还有三种连接方式  
    12. //(1)System.Data.SqlClient模式,使用sqlServer数据库比较好  
    13. //(2) System.Data.OleDb模式  
    14. //(3) System.Data.Odbc模式  
    15. //<1>如果使用MsqlClient模式的话,其基本连接字符串和连接对象如下:  
    16. //连接字符串:string connectString = "server=localhost;User Id=root;password=;Database=testDB";  
    17. //属性server是指数据库所在的机器(服务器)的IP地址,如果使用当前机器(本地机器)的话,也就是使用自己电脑上的数据库的话,可以使用"localhost"或者"127.0.0.1",如果使用其它机器上的数据库的话,使用那台机器的IP地址。  
    18. //database指的数据库的名字。  
    19. //Id代表连接数据库的用户名  
    20. //password代表连接数据库的密码,如果密码为空的话不需要填写,这样写"password="即可。  
    21. namespace CsharpMysql  
    22. {  
    23.     class Program  
    24.     {  
    25.         static void Main(string[] args)  
    26.         {  
    27.             string constructorString = "server=localhost;User Id=root;password=;Database=test";  
    28.             MySqlConnection myConnnect = new MySqlConnection(constructorString);  
    29.             myConnnect.Open();  
    30.             MySqlCommand myCmd = new MySqlCommand("insert into user(name,year) values('jjj',22)", myConnnect);  
    31.             Console.WriteLine(myCmd.CommandText);  
    32.             if (myCmd.ExecuteNonQuery() > 0)  
    33.             {  
    34.                 Console.WriteLine("数据插入成功!");  
    35.             }           
    36.             myCmd.CommandText = "insert into user(name,year) values('jjj4',22)";  
    37.             Console.WriteLine(myCmd.CommandText);  
    38.             if (myCmd.ExecuteNonQuery() > 0)  
    39.             {  
    40.                 Console.WriteLine("数据插入成功!");  
    41.             }  
    42.             myCmd.CommandText = "delete from user";  
    43.             Console.WriteLine(myCmd.CommandText);  
    44.             if (myCmd.ExecuteNonQuery() > 0)  
    45.             {  
    46.                 Console.WriteLine("user表类型数据全部删除成功!");  
    47.             }  
    48.             myCmd.Dispose();  
    49.             myConnnect.Close();  
    50.         }  
    51.     }  
    52. }<pre name="code" class="csharp">  

     

    参考:

     

    http://blog.csdn.net/cfl20121314/article/details/27106819
    http://blog.csdn.net/zhanghaoliangdehao/article/details/7372550

    http://blog.csdn.net/apache6/article/details/2778878

  • 相关阅读:
    Linux 设备驱动开发
    Neat Matrix Library矩阵库
    使用Linux开源组件而不使用Linux的RTOS解决方案embox
    命令行参数解析getopt
    Linux platform 设备
    编写C编译器 、 操作系统方面知识
    Embassy/Rust 和 FreeRTOS/C的速度比较
    SysAK 应用抖动诊断篇—— eBPF又立功了! | 龙蜥技术
    做ToB软件质量保障的这两年
    PolarDBX迎来开源后首个重大版本升级,2.1版本新增5大特色功能
  • 原文地址:https://www.cnblogs.com/roboot/p/9590846.html
Copyright © 2020-2023  润新知