usr*_*951 3 c# mysql excel-dna
正如标题所示,这是我的第一个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)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
错误1无法将方法组"ToString"转换为非委托类型"double".你打算调用这个方法吗?
错误2方法必须具有返回类型
错误3无法将方法组"ToString"转换为非委托类型"字符串".你打算调用这个方法吗?
错误4由于'my_test.ThisAddIn.MultiplyThem(string [])'返回void,因此返回关键字后面的对象表达式不能出现
错误1:ToString是一种方法.你需要reader["field_value"].ToString();
错误2:所有方法必须指定它们返回的对象类型.在这种情况下,您希望MultiplyThem返回一个字符串.public static string MultiplyThem(string[] args)
错误3:请参阅错误1
错误4:请参阅错误2
| 归档时间: |
|
| 查看次数: |
726 次 |
| 最近记录: |