Search code examples
c#mysqlexcel-dna

my first c# statement...what is the correct way to do this?


As the title suggest, this is my first C# try, so please go easy. (As a newb I promise to ask a bunch of easy questions for the C# pros out there to get you some easy points!) I'm using ExcelDNA to create a UDF in Excel, which will query our mysql database. I've added the ExcelDNA and mysql connector dll's as references. I have the following code, which produces a few errors:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;  
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using ExcelDna.Integration;
using MySql.Data.MySqlClient;

namespace my_test
{
public partial class ThisAddIn
{
    [ExcelFunction(Description = "Multiplies two numbers", Category = "Useful functions")]
    public static MultiplyThem(string[] args)
    {

        string connString = "Server=localhost;Port=3306;Database=test;Uid=root;password=p-word";
        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.ToString;
    }




    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

}
}

Here's the errors:

Error 1 Cannot convert method group 'ToString' to non-delegate type 'double'. Did you intend to invoke the method?
Error 2 Method must have a return type
Error 3 Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method?
Error 4 Since 'my_test.ThisAddIn.MultiplyThem(string[])' returns void, a return keyword must not be followed by an object expression


Solution

  • If you're going to use Excel-DNA, you need to take out the references to the Visual Studio Tools for Office (VSTO) assemblies, and the corresponding bits in your code - you can't mix the two frameworks in one assembly. The VSTO parts are the ones called Microsoft.Office.Tools... So I suggest:

    1. Remove the using Microsoft.Office.Tools.Excel;
    2. Your add-in class need not be partial (there isn't likely to be other 'parts').
    3. Remove the ThisAddIn_Startup and ThisAddIn_Shutdown - also part of the VSTO framework.

    Your Console.WriteLine is unlikely to go anywhere - rather use ExcelDna.Logging.LogDisplay.WriteLine.

    Another hint: Set the reference to ExcelDna.Integration.dll to be Copy Local: true in the property sheet for the reference. That way you don't get an unnecessary copy of this assembly in your output directory.

    If you are using Visual Studio 2010 your library will probably target .NET 4.0. Remember to set the runtime version in the .dna file:

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