2015-11-20 53 views
3

我正在測試mysqlc#東西,我只使用控制檯應用程序,並將在未來將其調整爲Windows窗體。發生了一個錯誤,我似乎無法修復它。沒有任何參數對應於需要的形式參數

方法:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using MySql.Data.MySqlClient; 

namespace databaseTest 
{ 
class Program 
{ 
    private MySqlConnection connection; //connection property 
    private string server; 
    private string database; 
    private string uid; 
    private string password; 

    static void Main(string[] args) 
    { 

     while (true) 
     { 
      Console.Clear(); 
      Program myProg = new Program(); 
      myProg.Initialize(); 
      Console.WriteLine("Choose item;"); 
      Console.WriteLine("<A> Add Record"); 
      Console.WriteLine("<B> Delete Record"); 
      Console.WriteLine("<C> Update Record"); 
      Console.WriteLine("<D> Show Record"); 
      Console.WriteLine("<E> Count Records"); 
      Console.WriteLine("<F> Search Records"); 
      Console.WriteLine("<X> Exit"); 
      ConsoleKeyInfo myKey = Console.ReadKey(); 
      if (myKey.Key == ConsoleKey.A) 
      { 
       myProg.Insert(); 
      } 
     } 
    } 

    private void Initialize() 
    { 
     server = "localhost"; //local host (WAMP) 
     database = "my_db"; //database name 
     uid = "root"; //database username 
     password = ""; //database password 
     string connectionString; 
     connectionString = "SERVER=" + server + ";" + "DATABASE=" + 
     database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";"; 
     connection = new MySqlConnection(connectionString); 
    } 

    private bool OpenConnection() 
    { 
     try 
     { 
      connection.Open(); 
      return true; 
     } 
     catch (MySqlException ex) 
     { 
      //When handling errors, you can your application's response based 
      //on the error number. 
      //The two most common error numbers when connecting are as follows: 
      //0: Cannot connect to server. 
      //1045: Invalid user name and/or password. 
      switch (ex.Number) 
      { 
       case 0: 
        Console.WriteLine("Cannot connect to server. Contact administrator"); 
        break; 

       case 1045: 
        Console.WriteLine("Invalid username/password, please try again"); 
        break; 
      } 
      return false; 
     } 
    } 

    private bool CloseConnection() 
    { 
     try 
     { 
      connection.Close(); 
      return true; 
     } 
     catch (MySqlException ex) 
     { 
      Console.WriteLine(ex.Message); 
      return false; 
     } 
    } 

    public void Insert() 
    { 
     List<databaseOperations.User> user = new List<databaseOperations.User>(); 
     databaseOperations.User x = new databaseOperations.User(); 
     Console.Clear(); 
     Console.WriteLine("---> Insert Record\n"); 
     Console.WriteLine("Enter numeric ID: (***)"); 
     int ID = int.Parse(Console.ReadLine()); 
     Console.WriteLine("Enter first name:"); 
     String firstName = Console.ReadLine(); 
     Console.WriteLine("Enter last name:"); 
     String lastName = Console.ReadLine(); 
     Console.WriteLine("Enter telephone:"); 
     String telephone = Console.ReadLine(); 
     x.ID = ID; 
     x.firstName = firstName; 
     x.lastName = lastName; 
     x.telephone = telephone; 
     user.Add(x); 
     databaseOperations.InsertToDatabase(x); 
    } 
} 
} 

DLL的方法:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using MySql.Data.MySqlClient; 

namespace databaseTest 
{ 
    class databaseOperations 
    { 
     private MySqlConnection connection; //connection property 
    private string server; 
    private string database; 
    private string uid; 
    private string password; 

    public class User 
    { 
     public int ID { get; set; } 

     public string firstName { get; set; } 

     public string lastName { get; set; } 

     public string telephone { get; set; } 
    } 

    private void Initialize() 
    { 
     server = "localhost"; //local host (WAMP) 
     database = "my_db"; //database name 
     uid = "root"; //database username 
     password = ""; //database password 
     string connectionString; 
     connectionString = "SERVER=" + server + ";" + "DATABASE=" + 
     database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";"; 
     connection = new MySqlConnection(connectionString); 
    } 

    private bool OpenConnection() 
    { 
     try 
     { 
      connection.Open(); 
      return true; 
     } 
     catch (MySqlException ex) 
     { 
      //When handling errors, you can your application's response based 
      //on the error number. 
      //The two most common error numbers when connecting are as follows: 
      //0: Cannot connect to server. 
      //1045: Invalid user name and/or password. 
      switch (ex.Number) 
      { 
       case 0: 
        Console.WriteLine("Cannot connect to server. Contact administrator"); 
        break; 

       case 1045: 
        Console.WriteLine("Invalid username/password, please try again"); 
        break; 
      } 
      return false; 
     } 
    } 

    private bool CloseConnection() 
    { 
     try 
     { 
      connection.Close(); 
      return true; 
     } 
     catch (MySqlException ex) 
     { 
      Console.WriteLine(ex.Message); 
      return false; 
     } 
    } 
    public void InsertToDatabase(int ID, string firstName, string lastName, string telephone) 
    { 
     string query = "INSERT INTO tbl_user (ID,First_Name,Last_Name,Telephone) VALUES('" + ID + "', '" + firstName + "','" + lastName + "','" + telephone + "')"; 
     if (this.OpenConnection() == true) 
     { 
      MySqlCommand cmd = new MySqlCommand(query, connection); 
      cmd.ExecuteNonQuery(); 
      this.CloseConnection(); 
      Console.WriteLine("\n -->Record Added - Press enter to continue..."); 
      Console.ReadLine(); 
     } 
    } 
} 
} 

我試圖使用DLL方法InsertToDatabase

我得到的錯誤是:

錯誤CS7036沒有給出任何參數對應於如果我改變

databaseOperations.InsertToDatabase(ID, firstName, lastName, telephone); 

我得到這個錯誤,而不是所需要的形式參數databaseOperations.InsertToDatabase的」 '的firstName'(整型,字符串,字符串,字符串)

嚴重級代碼說明項目文件行 錯誤CS0120非靜態字段,方法或屬性'databaseOperations.InsertToDatabase(int,string,string,string)需要對象引用

+0

聽起來好像你需要使用的firstName(而不是給FIRST_NAME)以下行:查詢字符串=「INSERT INTO tbl_user(ID,FIRST_NAME: Last_Name,Telephone)VALUES(''+ ID +'','「+ firstName +」','「+ lastName +」','「+ telephone +」')「; – Stanton

+0

例如INSERT INTO tbl_user(ID,firstName,...) – Stanton

+0

沒有,仍在firstName或其他某列上拋出相同的錯誤 – Chromatic

回答

2

您尚未創建databaseOperations的實例。方法databaseOperations.InsertToDatabaseinstance method

而且,正如你所說,你是新來的C# - 你應該命名以大寫首字母類(Pascal大小寫),和實例較低的情況下,第一個字母(駝峯):https://msdn.microsoft.com/en-us/library/x2dbyw72.aspx

我已經寫下面假設你的例子已經更名爲您databaseOperationsDatabaseOperations

DatabaseOperations databaseOperations = new DatabaseOperations(); 
databaseOperations.InsertToDatabase(x.ID, x.firstName, x.lastName, x.telephone); 

還請注意,您DatabaseOperations類目前沒有打電話Initialize。你可能需要從你的構造函數調用此方法,如:

class DatabaseOperations 
{ 
    public DatabaseOperations() 
    { 
     Initialize(); 
    }  

    // etc. 
} 
相關問題