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