MySql自制服务器

自制mysql服务器的关键程序,把这段程序放到线程中运行就可以开启mysql服务了。需要的同学可以参考下。

private void runMysql()
{
    Process p=new Process();
    string constr="server=localhhost;port=3306;database=dbname;user=root;password='123456';";
    MySqlConnection con=new MySqlConnection(constr);
    p.StartInfo.FileName="cmd.exe";
    p.StartInfo.UseShellExecute=false;
    p.StartInfo.RedirectStandardInput=true;
    p.StartInfo.RedirectStandardOutput=true;
    p.StartInfo.RedirectStandardError=true;
    p.StartInfo.CreateNoWindow=true;
    p.Start();
    //mysql执行文件的位置
    string spath="c:\\users\\...\\mysql\\bin\\mysqld.exe";
    System.IO.StreamWriter sw=p.StandardInput;
    sw.WriteLine(spath);
    sw.Flush();
    p.StandardInput.WriteLine("exit");
    //线程中显示信息需要用到委托,具体参考FillComboBoxCallback
    ShowMessage("Mysql在初始化...");
    while(Process.GetProcessNyName("mysqld").Length==0)
    {
        string strres=string.Empty;
        var output = new List<string>();
        while(p.StandardOutput.Peek()>-1)
        {
            strres+=p.StandardOutput.ReadLine();
        }
        if(Process.GetProcessNyName("mysqld").Length==0)
        {
            Thread.Sleep(5);
        }
    }
    p.Close();
    System.Data.DataTable dt=new System.Data.DataTable();
    while(dt.Rows.Count==0)
    {
        string sqlstr="select table_name from information_schema.tables where table_schema='数据库名'";
        try{
            while(con.State.ToString()!="Open")
            {
                con.Open();
            }
            MySqlDataAdapter mda=new MySqlDataAdapter(sqlstr,con);
            System.Data.DataSet ds=new System.Data.DataSet();
            mda.Fill(ds);
            dt=ds.Tables[0];
            FillComboBox(comboBOx1,dt);
            if(dt.Rows.Count==0)
            {
                Thread.Sleep(5);
            }
        }
        catch(Exception ex)
        {
        }
    }
}

delegate void FillComboBoxCallback(ComboBox cb,System.Data.DataTable dt);
void FillComboBox(ComboBox cb,System.Data.DataTable dt)
{
    if(this.InvokeRequired)
    {
        this.Invoke(new FillComboBoxCallback(FillComboBox),new object[]{cb,dt});
    }else
    {
        for(int m=0;m<dt.Rows.Count;m++)
        {
            cb.Items.Add(dt.Rows[m][0].ToString());
        }
        cb.Text=cb.Items[0].ToString();
    }
}

启动后,就可以用con来获取数据库的内容了,当然也可以修改编辑。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水滴与鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值