Connecting to a Mysql DB with C# - Need some with Datasets

前端 未结 1 1035
误落风尘
误落风尘 2020-12-18 12:03

Edit

I clearly do not understand how to do this right. After the examples were provided, I\'ve decided to hit the books a bit more, and try to work

相关标签:
1条回答
  • 2020-12-18 12:19

    Here's a simple example which you should follow to correct the mistakes in your approach:

    SQL stuff

    drop table if exists users;
    create table users
    (
    user_id int unsigned not null auto_increment primary key,
    username varbinary(32) unique not null
    )
    engine=innodb;
    
    insert into users (username) values ('f00'),('bar');
    

    C# DataAdapter method

    Note I dont explicitly open the db connection - the DataAdpater does that for me.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    // affffded these
    using MySql.Data;
    using MySql.Data.MySqlClient;
    using System.Data;
    
    namespace mysql
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string DB_CONN_STR = "Server=127.0.0.1;Uid=foo_dbo;Pwd=pass;Database=foo_db;";
    
                MySqlConnection cn = new MySqlConnection(DB_CONN_STR);
    
                try {
    
                    string sqlCmd = "select * from users order by user_id";
    
                    MySqlDataAdapter adr = new MySqlDataAdapter(sqlCmd, cn);
                    adr.SelectCommand.CommandType = CommandType.Text;
                    DataTable dt = new DataTable();
                    adr.Fill(dt); //opens and closes the DB connection automatically !! (fetches from pool)
    
                    foreach (DataRow dr in dt.Rows){
                        Console.WriteLine(string.Format("user_id = {0}", dr["user_id"].ToString()));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("{oops - {0}", ex.Message);
                }
                finally
                {
                    cn.Dispose(); // return connection to pool
                }
                Console.WriteLine("press any key...");
                Console.ReadKey();
            }
        }
    }
    

    C# DataReader example

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    // affffded these
    using MySql.Data;
    using MySql.Data.MySqlClient;
    using System.Data;
    
    namespace mysql
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string DB_CONN_STR = "Server=127.0.0.1;Uid=foo_dbo;Pwd=pass;Database=foo_db;";
    
                MySqlConnection cn = new MySqlConnection(DB_CONN_STR);
    
                try {
    
                    string sqlCmd = "select * from users order by user_id";
    
                    cn.Open(); // have to explicitly open connection (fetches from pool)
    
                    MySqlCommand cmd = new MySqlCommand(sqlCmd, cn);
                    cmd.CommandType = CommandType.Text;
                    MySqlDataReader rdr = cmd.ExecuteReader();
    
                    while (rdr.Read()){
                        Console.WriteLine(string.Format("user_id = {0}", rdr["user_id"].ToString()));   
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("{oops - {0}", ex.Message);
                }
                finally
                {
                    cn.Dispose(); // return connection to the pool
                }
                Console.WriteLine("press any key...");
                Console.ReadKey();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题