ExcelDNA和Mysql

时间:2011-12-03 20:09:43

标签: c# mysql excel-dna

我是C#的新手,创建了excel插件,也是ExcelDNA的新手。我得到了http://exceldna.codeplex.com/wikipage?title=Getting%20Started的例子。 UDF“MultiplyThem”按预期工作。

当我修改该站点上的示例#3以从mysql数据库中获取数据时。我不仅在我的项目中引用了ExcelDna.Integration.dll而且还引用了MySql.Data.dll。然后我用这个语句编译它:

c:\windows\microsoft.net\framework\v2.0.50727\csc.exe /target:library /reference:ExcelDna.Integration.dll /reference:MySql.Data.dll TestLib.cs

当我打开excel-add并开始输入我的UDF(在这种情况下,“= MultiplyThem()”)时,没有名为“MultiplyThem”的UDF。它为什么突然停止工作?这是我的C#代码:

using ExcelDna.Integration;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

public class MyFunctions
{
[ExcelFunction(Description = "Grabs data from database", Category = "Useful functions")]
public static string MultiplyThem(string[] args)
{
    string connString = "Server=localhost;Port=3306;Database=test;Uid=root;password=pword";
    MySqlConnection conn = new MySqlConnection(connString);
    MySqlCommand command = conn.CreateCommand();
    command.CommandText = "SELECT field_value FROM customers";
    try
    {
        conn.Open();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

    string myvariable = "bad";

    MySqlDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
        myvariable = reader["field_value"].ToString();
    }

    return myvariable;
}
}

我的Test1.dna文件(我的目标是我的项目中的.NET Framework 4):

<DnaLibrary RuntimeVersion="v4.0">
   <ExternalLibrary Path="TestLib.dll"/>
</DnaLibrary>

1 个答案:

答案 0 :(得分:2)

Excel-DNA目前不支持字符串数组作为参数。如果你将字符串[] args更改为object [] args它应该没问题。

相关问题