c#获取MySql表中数据

using MySql.Data.MySqlClient; //引入MySql.Data.dll
public static Dictionary csv_nameDic2 = new Dictionary();

private void ConnectDataBase()
        {
            try
            {
                //建立连接
                MySqlConnection mysqlConn = new MySqlConnection(string.Format(
                                "Server={0};Database={1};Uid={2};Pwd={3};port={4}",
                                "192.168.9.170", "****", "root", "*****", 3307));
                mysqlConn.Open();
                MySqlDataAdapter dataAdapter1 = new MySqlDataAdapter("select * from csv_name", mysqlConn);
                MySqlDataAdapter dataAdapter2 = new MySqlDataAdapter("select * from csv_field", mysqlConn);
                DataSet csvNameData = new DataSet();
                DataSet csvFieldData = new DataSet();
                dataAdapter1.Fill(csvNameData);
                dataAdapter2.Fill(csvFieldData);
                DataTable csvNameTable = csvNameData.Tables[0];
                DataTable  csvFieldTable = csvFieldData.Tables[0];
                foreach (DataRow item in csvNameTable.Rows)
                {
                   //获取表中表头对应的数据
                    string csvId = item["id"].ToString();
                    string csvName = item["name"].ToString();
                    string csvExplain = item["explain"].ToString();
                    if (!csv_nameDic1.ContainsKey(csvName))
                    {
                          if (!csv_nameDic2.ContainsKey(csvId))
                          {
                           csv_nameDic2.Add(csvId,csvName);
                         }
                }
            }
            catch
            {
                Console.WriteLine("无法连接数据库");
            }
        }

你可能感兴趣的:(c#)