ExcelDNA and Mysql
本文关键字:Mysql and ExcelDNA | 更新日期: 2023-09-27 18:16:47
我是c#新手,创建excel插件,也是ExcelDNA新手。我在http://exceldna.codeplex.com/wikipage?title=Getting%20Started上做了一些例子。UDF"MultiplyThem"按预期工作。
当我修改示例#3在该网站上从mysql数据库抓取数据。在我的项目中,我不仅引用了ExcelDna.Integration.dll,还引用了MySql.Data.dll。然后用下面的语句编译它:
c:'windows'microsoft.net'framework'v2.0.50727'csc.exe /target:library /reference:ExcelDna.Integration.dll /reference:MySql.Data.dll TestLib.cs
当我打开excel-add in并开始输入我的UDF(在本例中为"=MultiplyThem()")时,没有名为"MultiplyThem"的UDF。为什么它突然不工作了?下面是我的c#代码:
using ExcelDna.Integration;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
public class MyFunctions
{
[ExcelFunction(Description = "Grabs data from database", Category = "Useful functions")]
public static string MultiplyThem(string[] args)
{
string connString = "Server=localhost;Port=3306;Database=test;Uid=root;password=pword";
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;
}
}
和我的Test1。dna文件(我的目标是。net Framework 4在我的项目):
<DnaLibrary RuntimeVersion="v4.0">
<ExternalLibrary Path="TestLib.dll"/>
</DnaLibrary>
Excel-DNA目前不支持字符串数组作为参数。如果您将字符串[]args更改为object[] args,则应该没问题。