查看: 349|回复: 0

[.NET开发] C#连接MySQL的两个简单代码示例

发表于 2018-2-9 08:00:00

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

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

  1. static void Main(string[] args)
  2. {
  3. string sqlstr = "select * from manavatar";
  4. MySQLConnection DBConn = new MySQLConnection(new MySQLConnectionString("192.168.0.13", "flashdata", "root", "root", 3306).AsString);
  5. DBConn.Open();
  6. //MySQLDataAdapter myadap = new MySQLDataAdapter(sqlstr, conn);
  7. MySQLCommand DBComm = new MySQLCommand(sqlstr,DBConn);
  8. MySQLDataReader DBReader = DBComm.ExecuteReaderEx(); //DBComm.ExecuteReaderEx();
  9. MySQLDataAdapter DTAdapter = new MySQLDataAdapter(sqlstr,DBConn);
  10. DataSet myDataSet = new DataSet();
  11. DTAdapter.Fill(myDataSet,"manavatar");
  12. try
  13. {
  14. while (DBReader.Read())
  15. {
  16. //Console.WriteLine("11");
  17. Console.WriteLine("DBReader:{0},\t\t\tddddd:小贝,\t\t {2}",DBReader.GetString(0), DBReader.GetString(1),DBReader.GetString(3));
  18. }
  19. Console.WriteLine("0000");
  20. }
  21. catch (Exception e)
  22. {
  23. Console.WriteLine("读入失败!"+e.ToString());
  24. }
  25. finally
  26. {
  27. Console.WriteLine("DBReader关闭");
  28. Console.WriteLine("DBConn关闭");
  29. DBReader.Close();
  30. //DBConn.Close();
  31. }
  32. for (int i = 0; i < myDataSet.Tables["manavatar"].Rows.Count; i++)
  33. {
  34. Console.WriteLine("{0}",myDataSet.Tables["manavatar"].Rows[2]["user"]);
  35. }
  36. }
复制代码

方法二、

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using MySql.Data.MySqlClient;
  5. using System.Data;
  6. using System.Data.Common;
  7. namespace SybaseUtilTest
  8. {
  9. class Program
  10. {
  11. // http://bugs.mysql.com/47422, 有兴趣的朋友,可以看看这个bug是怎么回事
  12. static void testDataAdapter()
  13. {
  14. try
  15. {
  16. MySqlClientFactory factory = MySqlClientFactory.Instance;
  17. DbConnection conn = factory.CreateConnection();
  18. conn.ConnectionString = string.Format("server={0};user id=小贝; password={2}; database={3}; port={4}; pooling=false",
  19. "localhost", "root", "passwd", "test", 3306);
  20. conn.Open();
  21. DbDataAdapter da = factory.CreateDataAdapter();
  22. da.SelectCommand = conn.CreateCommand();
  23. da.SelectCommand.CommandText = "select * from t12345";
  24. da.DeleteCommand = conn.CreateCommand();
  25. da.DeleteCommand.CommandText = "delete from t12345 where id = @id";
  26. DbParameter param = factory.CreateParameter();
  27. param.ParameterName = "@id";
  28. param.DbType = DbType.Int32;
  29. param.SourceColumn = "id";
  30. param.SourceVersion = DataRowVersion.Current;
  31. da.DeleteCommand.Parameters.Add(param);
  32. da.DeleteCommand.UpdatedRowSource = UpdateRowSource.None;
  33. DataTable dt = new DataTable("t12345");
  34. da.Fill(dt);
  35. int index = 0;
  36. foreach ( DataRow o in dt.Rows )
  37. {
  38. if (o["id"].Equals(4))
  39. {
  40. Console.WriteLine(String.Format("index={0}, to delete id = 4, col2 = 小贝" , index, o["col2"]));
  41. break;
  42. }
  43. index++;
  44. }
  45. dt.Rows[index].Delete();
  46. da.Update(dt);
  47. dt.AcceptChanges();
  48. da.Dispose();
  49. conn.Close();
  50. }
  51. catch (Exception ex)
  52. {
  53. Console.WriteLine(ex.Source + " "
  54. + ex.Message + " "
  55. + ex.StackTrace);
  56. }
  57. }
  58. static void Main(string[] args)
  59. {
  60. testDataAdapter();
  61. }
  62. }
  63. }
复制代码

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



回复

使用道具 举报