C# - 从 MySQL 读取

问题描述

我在 MysqL 中有一个连接到 c# 代码数据库。 作为代码的一部分,我想将文件从我的 db 下载到 c# 项目中的文件夹,并且我希望它适用于多种类型的文件(csv、pcap 和 lua) 我的数据库看起来像这样:

enter image description here

在我的 C# 代码中,我的函数应该获取列(这是我们所追求的文件,例如 "lua_file" 、"pcap_file"...)和 column_name,它基本上是具有该名称的列文件。 我的问题是如何将它作为通用值添加到 select 语句中?此外,我有一个名为 p_name 的参数,它保存我要检查的表中的协议(可能有几种不同的协议)

C# 代码

 public static void ReadFromDB(string column,string column_name)
    {
        try
        {
            string cmd_str;
            MysqLDataAdapter Da_Obj = new MysqLDataAdapter();
            MysqLCommand Cm_Obj = new MysqLCommand();
            DataSet FilesDB = new DataSet();

            cmd_str = "SELECT lua_name,lua_file FROM filesdb WHERE protocol_name ='" + p_name + "';";
            Da_Obj = new MysqLDataAdapter(cmd_str,connection);
            connection.open();
            Da_Obj.Fill(FilesDB,"filesdb");

            Cm_Obj = new MysqLCommand(cmd_str,connection);
            MysqLDataReader reader = Cm_Obj.ExecuteReader();
            if (reader.Read() && reader != null)
            {
                Byte[] bytes;
                bytes = Encoding.UTF8.GetBytes(String.Empty);
                bytes = (Byte[])reader["lua_file"];
                FileStream fs = new FileStream(MainWindow.output_folder + FilesDB.Tables["filesdb"].Rows[0]["lua_name"].ToString(),FileMode.OpenorCreate);
                fs.Write(bytes,bytes.Length);
                fs.Close();
                Console.WriteLine("Download Completed!");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine("Failed in ReadFromDB!");
        }
        finally
        {
            connection.Close();
        }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)