我正在测试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
我得到的错误是:



如果我改变
databaseOperations.InsertToDatabase(ID, firstName, lastName, telephone);

我收到此错误:

最佳答案

您尚未创建databaseOperations的实例。方法databaseOperations.InsertToDatabaseinstance method

另外,正如您所提到的,您是C#的新手-您应该使用大写的首字母(Pascal大小写)和使用小写的首字母(camel大小写)的实例来命名类:https://msdn.microsoft.com/en-us/library/x2dbyw72.aspx

我编写了以下示例,假设您已将databaseOperations类重命名为DatabaseOperations:

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

另请注意,您的DatabaseOperations类当前未调用Initialize。您可能需要从构造函数中调用此函数,例如:
class DatabaseOperations
{
    public DatabaseOperations()
    {
        Initialize();
    }

    // etc.
}

关于c# - 没有给出与require形式参数相对应的参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33816800/

10-13 02:10