Question

I'm new to C#, creating excel plugins and also new to ExcelDNA. I got the examples working on http://exceldna.codeplex.com/wikipage?title=Getting%20Started. The UDF "MultiplyThem" works as expected.

When I modify example #3 on that site to grab data from a mysql database. I reference not only ExcelDna.Integration.dll but also MySql.Data.dll in my project. I then compile it with this statement:

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

When I open up the excel-add in and start typing in my UDF(in this case, "=MultiplyThem()") there is no UDF called "MultiplyThem". Why did it suddenly stop working? Here's my C# code:

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;
}
}

And my Test1.dna file (I am targeting .NET Framework 4 in my project):

<DnaLibrary RuntimeVersion="v4.0">
   <ExternalLibrary Path="TestLib.dll"/>
</DnaLibrary>
Was it helpful?

Solution

Excel-DNA does not currently support string arrays as parameters. If you change the string[] args to object[] args it should be fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top