【问题标题】:No overload for 'subbut_Click' matches delegate 'System.EventHandler''subbut_Click' 没有重载匹配委托 'System.EventHandler'
【发布时间】:2016-05-03 02:44:25
【问题描述】:

我有一点代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Globalization; 

namespace DEMO
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void brwbut_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;                         
            }
        }

        private void subbut_Click(string fileName, string tableName)
        {
            string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Mode=ReadWrite;Extended Properties=\"Excel 8.0;HDR=NO\"";
            string fieldstring  = "(ID int, Field1 char(255),Field2 char(255))";

            using (OleDbConnection conn = new OleDbConnection())
            {
                conn.Open();
                using (OleDbCommand cmd = new OleDbCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = string.Format(CultureInfo.InvariantCulture, @"CREATE TABLE [{0}] {1}", tableName, fieldstring);
                    cmd.ExecuteNonQuery();

                }
                conn.Close();
            }
        }

        public void InsertRow(String fileName, String tableName, string data)
        {
            string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Mode=ReadWrite;Extended Properties=\"Excel 8.0;HDR=YES\"";
            string headers = "ID,Field1,Field2";

            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                using (OleDbCommand cmd = new OleDbCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = string.Format(CultureInfo.InvariantCulture, @"INSERT INTO [{0}$] ({1}) values({2})", tableName, headers, data);
                    //txtQuery.Text = cmd.CommandText;
                    cmd.ExecuteNonQuery();
                }
                for (int i = 0; i < 10; i++)
                {
                    InsertRow("C:\\path\\to\\file\\Test File.xls", "ListingDetails",
                        "'" + i.ToString() + "','test" + (i + 2).ToString() + "','test" + (i + 5).ToString() + "'");
                }

                conn.Close();
            }
        }
    }
}

但我总是收到一个烦人的错误

“subbut_Click”没有重载匹配委托“System.EventHandler”

我不知道如何接近。我一直在网上搜索和故障排除一段时间,但没有找到任何答案。任何建议将不胜感激。

【问题讨论】:

  • 你认为这些参数是什么?

标签: c# winforms event-handling


【解决方案1】:

此错误表示您的处理程序的参数与事件不匹配。

Click 被定义为System.EventHandler,它接受object sender, EventArgs e

【讨论】:

  • 那么我该如何使用“字符串文件名,字符串表名”的对象
【解决方案2】:

.Click 事件需要连接到System.EventHandlerEventHandlerdelegate,其签名为:

public delegate void EventHandler(
    object sender,
    EventArgs e
)

改变这个

private void subbut_Click(string fileName, string tableName)

到这里

private void subbut_Click(object sender, EventArgs e)

如果此方法中需要您要查找的 string 值,则需要以另一种方式获取它们。这些值是否在 UI 上显示为 TextBox,如果是这样,这很简单吗?

private void subbut_Click(object sender, EventArgs e)
{
    // "textBox1" contains the text value for the filename
    string fileName = textBox1.Text;

    // This is the only missing piece that you need now. 
    // You are not going to get the value for the table name from the click event.
    // Instead you need to get it from the user input, i.e.; a textbox control.
    string tableName = tableTextBox.Text;

    // ...
}

【讨论】:

  • 文件名工作正常,但我无法为 tableName 定义
  • 我仍然面临同样的问题
  • 什么是调用该方法?
  • 这里我使用了一个输入文本框和上传按钮和提交按钮。用于从用户获取输入文件的输入文本框
  • private void subbut_Click(object sender, EventArgs e) { string fileName = fileName;字符串表名 = 表名; string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Mode=ReadWrite;Extended Properties=\"Excel 8.0;HDR=NO\""; string fieldstring = "(ID int, Field1 char(255),Field2 char(255))";
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
相关资源
最近更新 更多