1

我正在复制我的程序 (VS 2008) 的代码。我是初学者,我创建了一个数据库测试,其中有 1 个表 emp,其中有 3 个字段 e_name、e_id、age。调试 .cs 文件后,我得到错误 - 不包含适合入口点的静态“主”方法。请问你能帮帮我吗?

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ConnectCsharppToMySQL
{
    public  class DBConnect
    {
        private MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;

        //Constructor
        public DBConnect()
        {
            Initialize();
        }

        //Initialize values
        private void Initialize()
        {
            server = "localhost";
            database = "test";
            uid = "root";
            password = "";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" +
            database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

            connection = new MySqlConnection(connectionString);
        }

        //open connection to database
        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:
                        MessageBox.Show("Cannot connect to server.  Contact administrator");
                        break;

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

        //Close connection
        private bool CloseConnection()
        {
            try
            {
                connection.Close();
                return true;
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        //Insert statement
        public void Insert()
        {
            string query = "INSERT INTO emp (e_name, age) VALUES('Pooja R', '21')";

            //open connection
            if (this.OpenConnection() == true)
            {
                //create command and assign the query and connection from the constructor
                MySqlCommand cmd = new MySqlCommand(query, connection);

                //Execute command
                cmd.ExecuteNonQuery();

                //close connection
                this.CloseConnection();
            }
        }

        //Update statement
        public void Update()
        {
            string query = "UPDATE emp SET e_name='Peachy', age='22' WHERE name='Pooja R'";

            //Open connection
            if (this.OpenConnection() == true)
            {
                //create mysql command
                MySqlCommand cmd = new MySqlCommand();
                //Assign the query using CommandText
                cmd.CommandText = query;
                //Assign the connection using Connection
                cmd.Connection = connection;

                //Execute query
                cmd.ExecuteNonQuery();

                //close connection
                this.CloseConnection();
            }
        }

        /*     //Delete statement
             public void Delete()
             {
                 string query = "DELETE FROM tableinfo WHERE name='John Smith'";

                 if (this.OpenConnection() == true)
                 {
                     MySqlCommand cmd = new MySqlCommand(query, connection);
                     cmd.ExecuteNonQuery();
                     this.CloseConnection();
                 }
             }
     */


        //Select statement
        public List<string>[] Select()
        {
            string query = "SELECT * FROM emp";

            //Create a list to store the result
            List<string>[] list = new List<string>[3];
            list[0] = new List<string>();
            list[1] = new List<string>();
            list[2] = new List<string>();

            //Open connection
            if (this.OpenConnection() == true)
            {
                //Create Command
                MySqlCommand cmd = new MySqlCommand(query, connection);
                //Create a data reader and Execute the command
                MySqlDataReader dataReader = cmd.ExecuteReader();

                //Read the data and store them in the list
                while (dataReader.Read())
                {
                    list[0].Add(dataReader["e_id"] + "");
                    list[1].Add(dataReader["e_name"] + "");
                    list[2].Add(dataReader["age"] + "");
                }

                //close Data Reader
                dataReader.Close();

                //close Connection
                this.CloseConnection();

                //return list to be displayed
                return list;
            }
            else
            {
                return list;
            }
        }


        public static void main(String[] args)
        {
            DBConnect db1 = new DBConnect();
            Console.WriteLine("Initializing"); 
            db1.Initialize();
            Console.WriteLine("Inserting a record into table");
            db1.Insert();
            Console.WriteLine("Updating the record");
            db1.Update();
            Console.WriteLine("Displaying the table :");
            db1.Select();
        }
    }

}
4

3 回答 3

2

C# 区分大小写!

主要<---坏

主要<---很好

检查您的主要方法标识符并更正它!;)

于 2012-06-09T14:33:19.553 回答
1

改变:

public static void main(String[] args)

至:

public static void Main(String[] args)
于 2012-06-09T14:34:52.227 回答
0

将基本代码更改为

public static void Main(String[] args)

后面的部分定义了为什么您无法在屏幕上看到任何内容

你写

Console.WriteLine("Inserting a record into table");

它以DOS格式的CONSOLE格式运行,使您易于理解

我猜你正在运行GUI程序,它是一个图形程序,可以使用表单鼠标运行

谢谢你。

转换

 Console.WriteLine("Inserting a record into table");

  MessageBox.Show("Inserting a record into table");
于 2012-06-09T14:44:40.790 回答