程序不包含静态主方法 [英] Program does not contain a static main method

查看:90
本文介绍了程序不包含静态主方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此代码时,我得到:



严重性代码描述项目文件行抑制状态
错误CS5001程序不包含适用于入口点数据库C的静态Main方法:\ Users \Admin \Desktop \VisualStudio \database \database \ CSC 1 Active











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

;使用System.Diagnostics

;使用System.IO


;



命名空间连接
{

类DBConnect
{
私有MySqlConnection连接;
私有字符串服务器;
私有字符串数据库;
private string uid;
私人字符串密码;

//构造函数
public DBConnect()
{
Initialize();
}

//初始化值
private void Initialize()
{
server =localhost;
database =first_db;
uid =root;
password =;
string connectionString;
connectionString =SERVER =+ server +; +DATABASE =+
数据库+; +UID =+ uid +; +PASSWORD =+密码+;;

connection = new MySqlConnection(connectionString);

this.display();

}


//显示数据库

public void display()
{

// a是行数
int a = -1;

a = this.Countrows();

//Console.WriteLine(a);


// b是列数
int b = this.Countcolumns();

//Console.WriteLine(b);


//创建一个包含3列的新列表,b = 3; b是列数
List< string> [] newlist = new List< string> [b];


// this.Select()获取数据集,将其设置为等于newlist
newlist = this.Select();


Console.WriteLine(Id++username++password);

for(int j = 0; j< a; j ++)//遍历每一行; a是总行数
{


for(int i = 0; i< b; i ++)// ITERATE通过每个列; b是总列数
{
Console.Write(newlist [i] [j] +);


}
//每行obsv ..写一个新行
Console.WriteLine();
}


}


//打开数据库连接
public bool OpenConnection()
{

尝试
{
connection.Open();

Console.WriteLine(连接已打开);

返回true;
}
catch(MySqlException ex)
{
//当处理错误时,你的应用程序的响应可以基于错误编号
//。
//连接时最常见的两个错误号如下:
// 0:无法连接到服务器。
// 1045:无效的用户名和/或密码。
switch(ex.Number)
{
case 0:
Console.WriteLine(无法连接到服务器。请联系管理员);
休息;

案例1045:
Console.WriteLine(用户名/密码无效,请再试一次);
休息;
}
返回false;
}



}

//关闭连接
public bool CloseConnection()
{
尝试
{
connection.Close();

Console.WriteLine(连接关闭);
返回true;
}
catch(MySqlException ex)
{
Console.WriteLine(ex.Message);
返回false;
}



}



//插入声明
public void Insert( )
{

string query =INSERT INTO users(id,username,password)VALUES('23','Jojo','33'),('99','jen ','66');

//打开连接
if(this.OpenConnection()== true)
{
//创建命令并从构造函数$中分配查询和连接b $ b MySqlCommand cmd = new MySqlCommand(查询,连接);

Console.WriteLine(插入值);



//执行命令
cmd.ExecuteNonQuery();

//关闭连接
this.CloseConnection();
}

}





//更新声明
public void更新( )
{

string query =UPDATE users SET username ='Joe',password ='22',id ='33'WHERE username ='Joe';

//打开连接
if(OpenConnection()== true)
{
//创建mysql命令
MySqlCommand cmd = new MySqlCommand() ;
//使用CommandText
cmd.CommandText = query分配查询;
//使用Connection
cmd.Connection = connection分配连接;

//执行查询
cmd.ExecuteNonQuery();

//关闭连接
this.CloseConnection();
}



}




//删除声明
public void Delete()
{

string query =DELETE FROM users WHERE username ='Joe';

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


}




//选择对帐单
public List< string> ; []选择()
{
string query =SELECT * FROM users;

//创建一个包含3个元素的列表来存储结果
List< string> [] list = new List< string> [3];


//列表的每个元素都是一个新的字符串列表
list [0] = new List< string>();
list [1] = new List< string>();
list [2] = new List< string>();

//打开连接,运行OpenConnection()将打开连接



if(this.OpenConnection()== true)
{
//创建命令
MySqlCommand cmd = new MySqlCommand(查询,连接);
//创建数据读取器并执行命令
MySqlDataReader dataReader = cmd.ExecuteReader();

//读取数据并将它们存储在列表
//遍历每一行... dataReader.Read将获取每行的数据...
while (dataReader.Read())
{

//遍历每一列,添加id,用户名,密码列表[0]是column1,即id,list [1] IS COLUMN2这是USERNAME ... list [0] [0] element column 1,row 1

list [0] .Add(dataReader [id] +);
list [1] .Add(dataReader [username] +);
list [2] .Add(dataReader [password] +);
}

//关闭数据读取器
dataReader.Close();

//关闭连接
this.CloseConnection();

//要显示的返回列表
返回列表;
}
其他
{
返回列表;
}

}



//计数声明
public int Countrows()
{
string query =SELECT Count(*)FROM users;
int Count = -1;

//打开连接
if(this.OpenConnection()== true)
{
//创建Mysql命令
MySqlCommand cmd = new MySqlCommand (查询,连接);

// ExecuteScalar将返回一个值
Count = int.Parse(cmd.ExecuteScalar()+);

//关闭连接
this.CloseConnection();

返回计数;
}
其他
{
返回计数;
}

}




public int Countcolumns()
{

string query =SELECT Count(*)FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema ='first_db'AND table_name ='users';
int Count = -1;

//打开连接
if(this.OpenConnection()== true)
{
//创建Mysql命令
MySqlCommand cmd = new MySqlCommand (查询,连接);

// ExecuteScalar将返回一个值
Count = int.Parse(cmd.ExecuteScalar()+);

//关闭连接
this.CloseConnection();

返回计数;
}
其他
{
返回计数;
}

}


/ *
//备份
public void Backup()
{
尝试
{
DateTime Time = DateTime.Now;
int year = Time.Year;
int month = Time.Month;
int day = Time.Day;
int hour = Time.Hour;
int minute = Time.Minute;
int second = Time.Second;
int millisecond = Time.Millisecond;

//将文件保存到C:\,当前日期为文件名
string path;
path =C:\\MySqlBackup+ year + - + month + - + day +
- + hour + - + minute + - + second + - + millisecond +。sql;
StreamWriter file = new StreamWriter(path);


ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName =mysqldump;
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = string.Format(@ - u {0} -p {1} -h {2} {3},
uid,password,server,database);
psi.UseShellExecute = false;

流程流程= Process.Start(psi);

字符串输出;
output = process.StandardOutput.ReadToEnd();
file.WriteLine(output);
process.WaitForExit();
file.Close();
process.Close();
}
catch(IOException ex)
{
Console.WriteLine(错误,无法备份!);

Console.WriteLine(ex.message);
}



}



/ *
//恢复
public void Restore()
{
}
*
* /
}






}





我的尝试:



这是我没有的概念,因为我对C#全新。

解决方案

这是一个类,而不是一个应用程序 - 你不能自己执行一个类,因为系统不知道从哪里开始。



可执行程序需要一个特定的方法调用 - 它叫做Main,它必须是静态方法:

  static   void  Main( string  [] args)
{
...
}



如果不存在,则无法运行代码!

编译器无法找到Main()方法,就像错误消息所示。这个方法是你的程序的起点,是必需的。



如果你是新手,从上面的评论中得到建议可能真的是个好主意。看一下教程。在那里你将展示如何开始一个新的解决方案以及如何处理由VisualStudio预生成的Main()方法。


这个问题是C#101级别,你需要找到学习C#基础知识的教程。

C#教程(C#) [ ^ ]

C#Tutorial - YouTube [ ^ ]

C# Tutorial [ ^ ]

when i run this code, i am getting :

Severity	Code	Description	Project	File	Line	Suppression State
Error	CS5001	Program does not contain a static 'Main' method suitable for an entry point	database	C:\Users\Admin\Desktop\VisualStudio\database\database\CSC	1	Active






using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MySql.Data.MySqlClient;

using System.Diagnostics;


using System.IO;



namespace connect
{

    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 = "first_db";
            uid = "root";
            password = "";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" +
            database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

            connection = new MySqlConnection(connectionString);

            this.display();

        }


        // displays the database

        public void display()
        {

            // a is the number of rows 
            int a = -1;

            a = this.Countrows();

            //Console.WriteLine(a);


            // b is the number of columns
            int b = this.Countcolumns();

            //Console.WriteLine(b);


            //create a new list with 3 columns , b = 3; b is the number of columns 
            List<string>[] newlist = new List<string>[b];


            // this.Select() fetches the dataset, set it to equal to newlist 
            newlist = this.Select();


            Console.WriteLine("Id" + " " + "username" + " " + "password");

            for (int j = 0; j < a; j++) // iterate through each row; a is total number of rows 
            {


                for (int i = 0; i < b; i++) // ITERATE THROUGH EACH COLUMN  ; b is total number of columns
                {
                    Console.Write(newlist[i][j] + " ");


                }
                // for each  row of obsv..write a new line
                Console.WriteLine();
            }


        }


        //open connection to database
        public bool OpenConnection()
        {

            try
            {
                connection.Open();

                Console.WriteLine("connection opened");

                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;
            }



        }

        //Close connection
        public bool CloseConnection()
        {
            try
            {
                connection.Close();

                Console.WriteLine("connection closed");
                return true;
            }
            catch (MySqlException ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }



        }



        //Insert statement
        public void Insert()
        {

            string query = "INSERT INTO users (id, username, password) VALUES('23' ,'Jojo', '33')  , ('99', 'jen', '66')";

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

                Console.WriteLine("values inserted");



                //Execute command
                cmd.ExecuteNonQuery();

                //close connection
                this.CloseConnection();
            }

        }





        //Update statement
        public void Update()
        {

            string query = "UPDATE users SET username='Joe', password='22', id='33'  WHERE username='Joe'";

            //Open connection
            if (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 users WHERE username='Joe'";

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


        }




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

            //Create a list of 3 elements to store the result
            List<string>[] list = new List<string>[3];


            // each element of list is a new list of strings
            list[0] = new List<string>();
            list[1] = new List<string>();
            list[2] = new List<string>();

            //Open connection, running OpenConnection() will open the 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        
                // iterate through each row... dataReader.Read will get the data for each row...
                while (dataReader.Read())
                {

                    // iterate through each column, add the id , username, password list[0] is column1 which is id, list[1] IS COLUMN2 WHICH IS USERNAME... list[0][0]  element column 1 , row 1 

                    list[0].Add(dataReader["id"] + "");
                    list[1].Add(dataReader["username"] + "");
                    list[2].Add(dataReader["password"] + "");
                }

                //close Data Reader
                dataReader.Close();

                //close Connection
                this.CloseConnection();

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

        }



        //Count statement
        public int Countrows()
        {
            string query = "SELECT Count(*) FROM users";
            int Count = -1;

            //Open Connection
            if (this.OpenConnection() == true)
            {
                //Create Mysql Command
                MySqlCommand cmd = new MySqlCommand(query, connection);

                //ExecuteScalar will return one value
                Count = int.Parse(cmd.ExecuteScalar() + "");

                //close Connection
                this.CloseConnection();

                return Count;
            }
            else
            {
                return Count;
            }

        }




        public int Countcolumns()
        {

            string query = "SELECT Count(*) FROM INFORMATION_SCHEMA.COLUMNS  WHERE table_schema = 'first_db'  AND table_name = 'users'";
            int Count = -1;

            //Open Connection
            if (this.OpenConnection() == true)
            {
                //Create Mysql Command
                MySqlCommand cmd = new MySqlCommand(query, connection);

                //ExecuteScalar will return one value
                Count = int.Parse(cmd.ExecuteScalar() + "");

                //close Connection
                this.CloseConnection();

                return Count;
            }
            else
            {
                return Count;
            }

        }


        /*
        //Backup
        public void Backup()
        {
            try
            {
                DateTime Time = DateTime.Now;
                int year = Time.Year;
                int month = Time.Month;
                int day = Time.Day;
                int hour = Time.Hour;
                int minute = Time.Minute;
                int second = Time.Second;
                int millisecond = Time.Millisecond;

                //Save file to C:\ with the current date as a filename
                string path;
                path = "C:\\MySqlBackup" + year + "-" + month + "-" + day +
            "-" + hour + "-" + minute + "-" + second + "-" + millisecond + ".sql";
                StreamWriter file = new StreamWriter(path);


                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = "mysqldump";
                psi.RedirectStandardInput = false;
                psi.RedirectStandardOutput = true;
                psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}",
                    uid, password, server, database);
                psi.UseShellExecute = false;

                Process process = Process.Start(psi);

                string output;
                output = process.StandardOutput.ReadToEnd();
                file.WriteLine(output);
                process.WaitForExit();
                file.Close();
                process.Close();
            }
            catch (IOException ex)
            {
                Console.WriteLine("Error , unable to backup!");

                Console.WriteLine("ex.message");
            }



        }



        /*
        //Restore
        public void Restore()
        {
        }
         * 
          */
    }






}



What I have tried:

This is something , i have no concept of as i am completely new to C#.

解决方案

This is a class, not an application - and you can't execute a class on it's own because the system doesn't know where to begin.

And executable program needs a specific method to call - it's called "Main" and it must be a static method:

static void Main(string[] args)
    {
    ...
    }


If that doesn't exist, you can't run the code!


The compiler can't find the Main() method, just as the error message says. This method is the starting point of your program and is required.

If you are new, it may really be a good idea to take the advice from the comment above and look at a tutorial. There you will be shown how to start a new solution and what to do with the Main() method, which is pregenerated by VisualStudio.


This problem is C# 101 level, you need to find tutorials to learn the basics of C#.
C# Tutorials (C#)[^]
C# Tutorial - YouTube[^]
C# Tutorial[^]


这篇关于程序不包含静态主方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆