C#连接MySQL的两个简单代码示例

所属分类: 软件编程 / C#教程 阅读数: 44
收藏 0 赞 0 分享

实现代码一、使用的是mysql自带的驱动安装一下即可

这是一个简单的例子。
在这里有个问题:dataset如果没设主键的话,可能会引起一些对数库操作的问题,比如会造成updata出现错误。

static void Main(string[] args)
    {
      string sqlstr = "select * from manavatar";
      MySQLConnection DBConn = new MySQLConnection(new MySQLConnectionString("192.168.0.13", "flashdata", "root", "root", 3306).AsString);
      DBConn.Open();
      //MySQLDataAdapter myadap = new MySQLDataAdapter(sqlstr, conn);
      MySQLCommand DBComm = new MySQLCommand(sqlstr,DBConn);
      MySQLDataReader DBReader = DBComm.ExecuteReaderEx(); //DBComm.ExecuteReaderEx();
      MySQLDataAdapter DTAdapter = new MySQLDataAdapter(sqlstr,DBConn);
      
      DataSet myDataSet = new DataSet();
      DTAdapter.Fill(myDataSet,"manavatar");
     
     
      try
      {
        while (DBReader.Read())
        {
          //Console.WriteLine("11");
          Console.WriteLine("DBReader:{0},\t\t\tddddd:{1},\t\t {2}",DBReader.GetString(0), DBReader.GetString(1),DBReader.GetString(3));
        }
        Console.WriteLine("0000");
      }
      catch (Exception e)
      { 
        Console.WriteLine("读入失败!"+e.ToString());
      }
      finally
      {
        Console.WriteLine("DBReader关闭");
        Console.WriteLine("DBConn关闭");
        DBReader.Close();
        //DBConn.Close();
      }
      
      for (int i = 0; i < myDataSet.Tables["manavatar"].Rows.Count; i++)
      {
        Console.WriteLine("{0}",myDataSet.Tables["manavatar"].Rows[2]["user"]);
      }
      
      
    }

方法二、

贴一份示例代码。非常适合于初学者使用。
C#访问mysql

using System; 
using System.Collections.Generic; 
using System.Text; 
 
using MySql.Data.MySqlClient; 
using System.Data; 
using System.Data.Common; 
 
namespace SybaseUtilTest 
{ 
  class Program 
  { 
    // http://bugs.mysql.com/47422, 有兴趣的朋友,可以看看这个bug是怎么回事 
    static void testDataAdapter() 
    { 
      try 
      { 
        MySqlClientFactory factory = MySqlClientFactory.Instance; 
        DbConnection conn = factory.CreateConnection(); 
        conn.ConnectionString = string.Format("server={0};user id={1}; password={2}; database={3}; port={4}; pooling=false", 
              "localhost", "root", "passwd", "test", 3306); 
        conn.Open(); 
 
        DbDataAdapter da = factory.CreateDataAdapter(); 
 
        da.SelectCommand = conn.CreateCommand(); 
        da.SelectCommand.CommandText = "select * from t12345"; 
 
 
        da.DeleteCommand = conn.CreateCommand(); 
        da.DeleteCommand.CommandText = "delete from t12345 where id = @id"; 
 
        DbParameter param = factory.CreateParameter(); 
        param.ParameterName = "@id"; 
        param.DbType = DbType.Int32; 
        param.SourceColumn = "id"; 
        param.SourceVersion = DataRowVersion.Current; 
 
        da.DeleteCommand.Parameters.Add(param); 
        da.DeleteCommand.UpdatedRowSource = UpdateRowSource.None; 
 
        DataTable dt = new DataTable("t12345"); 
        da.Fill(dt); 
 
        int index = 0; 
        foreach ( DataRow o in dt.Rows ) 
        { 
          if (o["id"].Equals(4)) 
          { 
            Console.WriteLine(String.Format("index={0}, to delete id = 4, col2 = {1}" , index, o["col2"])); 
            break; 
          } 
          index++; 
        } 
        dt.Rows[index].Delete(); 
        da.Update(dt); 
        dt.AcceptChanges(); 
 
        da.Dispose(); 
        conn.Close(); 
      } 
      catch (Exception ex) 
      { 
        Console.WriteLine(ex.Source + " " 
          + ex.Message + " " 
          + ex.StackTrace); 
      } 
       
    } 
     
    static void Main(string[] args) 
    { 
      testDataAdapter(); 
    } 
  } 
} 

以上就是脚本之家小编为大家整理的c#连接mysql数据库的方法,需要的朋友可以参考一下。

更多精彩内容其他人还在看

C#中Datetimepicker出现问题的解决方法

这篇文章主要给大家介绍了关于C#中Datetimepicker出现问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C# SQLite数据库入门使用说明

这篇文章主要给大家介绍了关于C#中SQLite数据库入门使用的相关资料,文中通过图文以及示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#实现批量下载图片到本地示例代码

这篇文章主要给大家介绍了关于C#如何实现批量下载图片到本地的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用c#具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

如何获取C#中方法的执行时间以及其代码注入详解

这篇文章主要给大家介绍了关于如何获取C#中方法的执行时间以及其代码注入的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧
收藏 0 赞 0 分享

C#中通过LRU实现通用高效的超时连接探测

这篇文章主要介绍了c#中通过LRU实现通用高效的超时连接探测,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

这篇文章主要给大家介绍了关于如何使用C#将Tensorflow训练的.pb文件用在生产环境的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#程序启动项的设置方法

这篇文章主要为大家详细介绍了C#程序启动项的设置方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

c#爬虫爬取京东的商品信息

这篇文章主要给大家介绍了关于利用c#爬虫爬取京东商品信息的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#随机数生成字母金字塔

这篇文章主要为大家详细介绍了C#随机数生成字母金字塔,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

WPF实现窗体中的悬浮按钮

这篇文章主要为大家详细介绍了WPF实现窗体中的悬浮按钮,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多