3

如标题所示,这是我第一次尝试 C#,所以请放轻松。(作为一个新手,我保证会向 C# 专业人士提出一些简单的问题,以获得一些简单的观点!)我正在使用 ExcelDNA 在 Excel 中创建一个 UDF,它将查询我们的 mysql 数据库。我添加了 ExcelDNA 和 mysql 连接器 dll 作为参考。我有以下代码,它会产生一些错误:

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)
    {
    }

}
}

这是错误:

错误 1 ​​无法将方法组“ToString”转换为非委托类型“double”。您是否打算调用该方法?
错误 2 方法必须具有返回类型
错误 3 无法将方法组“ToString”转换为非委托类型“字符串”。您是否打算调用该方法?
错误 4 由于 'my_test.ThisAddIn.MultiplyThem(string[])' 返回 void,return 关键字后面不能跟对象表达式

4

6 回答 6

5

错误1:ToString是一种方法。你需要reader["field_value"].ToString();

错误 2:所有方法都必须指定它们返回的对象类型。在这种情况下,您希望 MultiplyThem 返回一个字符串。public static string MultiplyThem(string[] args)

错误 3:见错误 1

错误 4:见错误 2

于 2011-11-28T03:34:22.727 回答
3

如果要使用 Excel-DNA,则需要删除对 Visual Studio Tools for Office (VSTO) 程序集的引用以及代码中的相应位 - 您不能在一个程序集中混合使用这两个框架。VSTO 部分是所谓的Microsoft.Office.Tools...所以我建议:

  1. 去除那个using Microsoft.Office.Tools.Excel;
  2. 您的加载项类不需要partial(不可能有其他“部分”)。
  3. 删除ThisAddIn_Startupand ThisAddIn_Shutdown- 也是 VSTO 框架的一部分。

Console.WriteLine不太可能去任何地方 - 而是使用ExcelDna.Logging.LogDisplay.WriteLine.

另一个提示:在引用的属性表中将 ExcelDna.Integration.dll 的引用设置为Copy Local: true。这样,您就不会在输出目录中获得该程序集的不必要副本。

如果您使用的是 Visual Studio 2010,您的库可能会面向 .NET 4.0。记得在 .dna 文件中设置运行时版本:

<DnaLibrary RuntimeVersion="v4.0" >
    <ExternalLibrary Path="MyAddIn.dll" />
</DnaLibrary>
于 2011-11-28T06:00:46.900 回答
2

干得好。你有两个基本问题:

1) 方法需要指定返回类型或 void。由于您要返回一个字符串,因此我在静态和方法名称之间添加了字符串。

2) ToString 是一种方法。使用时需要加上括号,如.ToString()。

public static string 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;
}
于 2011-11-28T03:34:04.197 回答
2

所有的错误都是不言自明的。

1)用返回类型声明方法string

public static string MultiplyThem(string[] args)
//            ^^^^^^

2)使用ToString()return myvariable.ToString();

3)最后一个错误将消失。

于 2011-11-28T03:35:18.747 回答
2

这里需要注意几点:

  1. 您的方法应该有一个返回签名(即字符串或双精度)或应该有一个无效的返回签名。如果您希望返回字符串,您的方法应为:

    public static void MultiplyThem(string[] args)
    
  2. 当您从阅读器读取一个值时,您需要按如下方式访问它:

    myvariable = reader["field_value"].ToString();
    
  3. ToString 方法是一种方法,因此需要这样调用。
于 2011-11-28T03:36:34.907 回答
2

更改myvariable.ToStringmyvariable.ToString()

将您的方法声明从 更改public static MultiplyThem(string[] args)public static string MultiplyThem(string[] args)

于 2011-11-28T03:37:20.920 回答