2016-05-03 56 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定義爲System.EventHandler,需要object sender, EventArgs e

+0

那麼我怎麼能使用對象的「字符串文件名,字符串表名」 – Suresh

0

.Click事件預計將連線到System.EventHandler。該EventHandlerdelegate具有的簽名:

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值,你需要以另一種方式獲得他們。這些值是否在用戶界面上公開爲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

for fileName工作正常,但我不能定義爲tableName – Suresh

+0

仍然我面臨同樣的問題 – Suresh

+0

什麼是調用該方法? –

相關問題