嵌入式MySQL Server C#包装器? [英] Embedded MySQL Server C# Wrapper?

查看:78
本文介绍了嵌入式MySQL Server C#包装器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑添加嵌入式MYSQL服务器库进入我的应用程序.我的软件位于Game Server和Web管理面板之间,提供了额外的功能.我们的许多客户一直在为服务器请求MySQL数据库(因为这是他们的插件开发使用的),并且能够在我的软件中运行一个数据库,而不是手动设置它们.

I have been heavily contemplating adding the Embedded MYSQL Server Library into my application. My software is a layer between a Game Server and a Web Management Panel that provides extra functionality. Many of our customers have been requesting MySQL databases for their servers (since this is what their plugins are developed to use), and being able to run one within my software rather than manually set them up would be fantastic.

  • 我缺少该库的.NET包装吗?

  • Is there a .NET wrapper for this library I am missing?

我的应用程序正在运行的软件需要能够与其连接,但是显然该软件正在本地运行.这会工作吗?

The software being ran by my application will need to be able to connect to it, but the software is obviously being ran locally. Will this work?

推荐答案

我不知道该库有任何.NET包装器,尽管我记得有人在谈论从版本开始在.NET连接器中引入对此库的支持. 5.我现在找不到关于此的任何信息.

i'm not aware of any .NET wrapper for this library although i remember there was a talk about introducing support for this library in the .NET Connector from version 5. I can't find any information about that right now though.

网络上有一些示例代码,您可以在上面扩展代码.我问最完整的应该是 http://forums.mysql.com/read. php?38,67281,67917 ->

There is some sample code out there on the web which you can extend your code upon. I quess the most complete would be http://forums.mysql.com/read.php?38,67281,67917 ->

using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Configuration;

class EntryPoint
{
    [DllImport("libmySQL.dll")]
    static extern IntPtr mysql_init(IntPtr mysql);

    [DllImport("libmySQL.dll")]
    static extern IntPtr mysql_fetch_lengths(IntPtr result);

    [DllImport("libmySQL.dll")]
    static extern IntPtr mysql_store_result(IntPtr mysql);

    [DllImport("libmySQL.dll")]
    static extern IntPtr mysql_fetch_row(IntPtr result);

    [DllImport("libmySQL.dll")]
    static extern IntPtr mysql_real_connect(IntPtr mysql, string host, string user, string passwd, string db, uint port, string unix_socket, uint client_flag);

    [DllImport("libmySQL.dll")]
    static extern uint mysql_field_count(IntPtr mysql);

    [DllImport("libmySQL.dll")]
    static extern string mysql_error(IntPtr mysql);

    [DllImport("libmySQL.dll")]
    static extern int mysql_real_query(IntPtr mysql, string query, uint length);

    static Array MarshalArray(Type structureType, IntPtr arrayPtr, int length)
    {
        if (structureType == null)
            throw new ArgumentNullException("structureType");
        if (!structureType.IsValueType)
            throw new ArgumentException("Only struct types are supported.", "structureType");
        if (length < 0)
            throw new ArgumentOutOfRangeException("length", length, "length must be equal to or greater than zero.");
        if (arrayPtr == IntPtr.Zero)
            return null;
        int size = System.Runtime.InteropServices.Marshal.SizeOf(structureType);
        Array array = Array.CreateInstance(structureType, length);
        for (int i = 0; i < length; i++)
        {
            IntPtr offset = new IntPtr((long)arrayPtr + (size * i));
            object value = System.Runtime.InteropServices.Marshal.PtrToStructure(offset, structureType);
            array.SetValue(value, i);
        }
        return array;
    }


    static void mysql_real_query(IntPtr mysql, string query)
    {
        int result = (query != null) ? mysql_real_query(mysql, query, (uint)query.Length) : mysql_real_query(mysql, null, 0);
        if (result == 0)
            return;
        throw new ApplicationException(mysql_error(mysql));
    }

    static int Main(string[] args)
    {
        IntPtr ptr = mysql_init(IntPtr.Zero);
        Debug.Assert(ptr != IntPtr.Zero);

        IntPtr mysql = mysql_real_connect(ptr, ConfigurationSettings.AppSettings["server"], ConfigurationSettings.AppSettings["username"], ConfigurationSettings.AppSettings["password"], ConfigurationSettings.AppSettings["database"], 0, null, 0);
        Debug.Assert(ptr != IntPtr.Zero, mysql_error(ptr));

        string query = "SELECT * FROM proxy_servers";
        Console.WriteLine("Executing query: {0}", query);
        mysql_real_query(mysql, query);

        IntPtr result = mysql_store_result(mysql);
        Debug.Assert(result != IntPtr.Zero, mysql_error(mysql));

        uint fieldCount = mysql_field_count(mysql);
        Console.WriteLine("field count: {0}", fieldCount);

        for (IntPtr ptrRow = mysql_fetch_row(result); ptrRow != IntPtr.Zero; ptrRow = mysql_fetch_row(result))
        {
            IntPtr[] mysqlRow = (IntPtr[])MarshalArray(typeof(IntPtr), ptrRow, (int)fieldCount);
            IntPtr ptrLengths = mysql_fetch_lengths(result);
            uint[] lengths = (uint[])MarshalArray(typeof(uint), ptrLengths, (int)fieldCount);
            for (int i = 0; i < (int)fieldCount; i++)
            {
                string str = Marshal.PtrToStringAnsi(mysqlRow[i], (int)lengths[i]);
                Console.Write("{0}, ", str);
            }
            Console.WriteLine();
        }
        return 0;
    }
}

这篇关于嵌入式MySQL Server C#包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆