【问题标题】:my first c# statement...what is the correct way to do this?我的第一个 c# 语句......这样做的正确方法是什么?
【发布时间】:2011-11-28 03:30:26
【问题描述】:

正如标题所示,这是我第一次尝试 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 关键字后面不能跟对象表达式

【问题讨论】:

    标签: c# mysql excel-dna


    【解决方案1】:

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

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

    错误 3:请参阅错误 1

    错误 4:参见错误 2

    【讨论】:

      【解决方案2】:

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

      1. 删除using Microsoft.Office.Tools.Excel;
      2. 您的加载项类不必是 partial(不可能有其他“部分”)。
      3. 删除 ThisAddIn_Startup 和 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>
      

      【讨论】:

        【解决方案3】:

        给你。你有两个基本问题:

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

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

        【讨论】:

          【解决方案4】:

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

          1) 声明返回类型为string的方法:

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

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

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

          【讨论】:

            【解决方案5】:

            这里要注意几点:

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

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

              myvariable = reader["field_value"].ToString();
              
            3. ToString 方法是一种方法,因此需要这样调用。

            【讨论】:

              【解决方案6】:

              将myvariable.ToString 更改为myvariable.ToString()。

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-02-14
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多