栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

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

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

实现代码一、使用的是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},tttddddd:{1},tt {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数据库的方法,需要的朋友可以参考一下。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123380.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号