正如标题所示,这是我第一次尝试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'转换为非委托类型'string'。您是否打算调用该方法?
由于'my_test.ThisAddIn.MultiplyThem(string[])'返回void,因此return关键字不能后跟对象表达式
错误1:ToString
是一个方法。你需要reader["field_value"].ToString();
错误2:所有方法必须指定它们返回的对象类型。在本例中,您希望MultiplyThem返回一个字符串。public static string MultiplyThem(string[] args)
错误3:参见错误1
错误4:参见错误2
如果你要使用Excel-DNA,你需要拿出对Visual Studio Tools for Office (VSTO)程序集的引用,以及代码中相应的部分——你不能在一个程序集中混合使用这两个框架。VSTO部分被称为Microsoft.Office.Tools...
,所以我建议:
- 删除
using Microsoft.Office.Tools.Excel;
- 您的外接程序类不需要是
partial
(不太可能有其他"部分")。 - 删除
ThisAddIn_Startup
和ThisAddIn_Shutdown
-也是VSTO框架的一部分。
你的Console.WriteLine
不太可能去任何地方-不如使用ExcelDna.Logging.LogDisplay.WriteLine
。
如果你使用的是Visual Studio 2010,你的库可能会以。net 4.0为目标。记得在.dna文件中设置运行时版本:
<DnaLibrary RuntimeVersion="v4.0" >
<ExternalLibrary Path="MyAddIn.dll" />
</DnaLibrary>
给你。你有两个基本问题:
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;
}
所有的错误都是不言自明的
1)声明返回类型为string
的方法:
public static string MultiplyThem(string[] args)
// ^^^^^^
2)使用ToString()
: return myvariable.ToString();
有几点需要注意:
-
你的方法应该有一个返回签名(即string或double)或者应该有一个void的返回签名。如果你希望返回字符串,你的方法应该是:
public static void MultiplyThem(string[] args)
-
当你从读取器读取一个值时,你需要这样访问它:
myvariable = reader["field_value"].ToString();
- ToString方法是一个方法,因为它需要被调用。
将myvariable.ToString
更改为myvariable.ToString()
。
将方法声明从public static MultiplyThem(string[] args)
更改为public static string MultiplyThem(string[] args)
。