2016-05-03 24 views
0

私はコードのビットがあります「subbut_Click」マッチデリゲートのための過負荷「System.EventHandler」

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'と一致します

私はアプローチする方法がわかりません。私はオンラインで検索していましたし、しばらくの間撮影していて、何の答えも見つけられていません。どんな提案も大歓迎です。

+0

これらのパラメータはどのようなものだと思いますか? – SLaks

答えて

0

このエラーは、ハンドラのパラメータがイベントと一致しないことを意味します。

Clickは、object sender, EventArgs eをとるSystem.EventHandlerと定義されます。

+0

次に、 "string fileName、string tableName"のオブジェクトをどのように使用できますか? – Suresh

0

.Clickイベントは、System.EventHandlerに配線される予定です。あなたが探しているstring値は、このメソッド内で必要とされている場合

この

private void subbut_Click(object sender, EventArgs e) 

public delegate void EventHandler(
    object sender, 
    EventArgs e 
) 

変更この

private void subbut_Click(string fileName, string tableName) 

EventHandlerはの署名を持っているdelegateですあなたはそれらを別の方法で取得する必要があります。これらの値は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; 

    // ... 
} 
+0

は正常に動作していますが、tableNameの定義はできません。 – Suresh

+0

まだ同じ問題に直面しています – Suresh

+0

このメソッドを呼び出すのは何ですか? –

関連する問題