简体   繁体   English

使标签静态错误:无法通过嵌套类型“windowsForm8.Form1.DBConnect”访问外部类型“windowsForm8.Form1”的非静态成员

[英]make a label static error : cannot access a non-static member of outer type 'windowsForm8.Form1' via nested Type 'windowsForm8.Form1.DBConnect'

Yo this is my first post here and i wanna know how to fix this problem in C# so im tryin to connect to database and select information from it and those information will be displayed as a label !哟,这是我在这里的第一篇文章,我想知道如何在 C# 中解决这个问题,所以我尝试连接到数据库并从中选择信息,这些信息将显示为标签! easy, so this is the error i get :很简单,所以这是我得到的错误:

cannot access a non-static member of outer type 'windowsForm8.Form1' via nested Type 'windowsForm8.Form1.DBConnect'无法通过嵌套类型“windowsForm8.Form1.DBConnect”访问外部类型“windowsForm8.Form1”的非静态成员


and this is the Code :这是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    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 = "net";
            database = "rainbow";
            uid = "ok";
            password = "passwd!";
            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;
                    default :
                        MessageBox.Show("Connected Successfuly!!");
                        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 mytable (username, password) VALUES('shryder', 'nopassword')";

            //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()
        {
        }

        //Delete statement
        public void Delete()
        {

        }

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

            //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
                if (dataReader.Read())
                {
                    /*list[0].Add(dataReader["username"] + "");
                    list[1].Add(dataReader["password"] + "");
                    list[2].Add(dataReader["email"] + "");*/
                    newscontent.Text = dataReader["username"]; //this is the error


                }

                //close Data Reader
                dataReader.Close();

                //close Connection
                this.CloseConnection();

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

        //Count statement


        //Backup
        public void Backup()
        {
        }

        //Restore
        public void Restore()
        {
        }
    }



}
}

please help me ,thanks :)请帮助我,谢谢:)

Move all your code from the class DbConnect yo your Form1 class.将 DbConnect 类中的所有代码移到 Form1 类中。 Then all of your methods have access to your form controls.然后您的所有方法都可以访问您的表单控件。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    } 

        private MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;

        //Initialize values
        private void Initialize()
        {
            server = "net";
            database = "rainbow";
            uid = "ok";
            password = "passwd!";
            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;
                    default :
                        MessageBox.Show("Connected Successfuly!!");
                        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 mytable (username, password) VALUES('shryder', 'nopassword')";

            //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()
        {
        }

        //Delete statement
        public void Delete()
        {

        }

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

            //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
                if (dataReader.Read())
                {
                    /*list[0].Add(dataReader["username"] + "");
                    list[1].Add(dataReader["password"] + "");
                    list[2].Add(dataReader["email"] + "");*/
                    newscontent.Text = dataReader["username"]; //this is the error


                }

                //close Data Reader
                dataReader.Close();

                //close Connection
                this.CloseConnection();

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

        //Count statement


        //Backup
        public void Backup()
        {
        }

        //Restore
        public void Restore()
        {
        }
    }



}
}

Like this your code ahould be able to access the labels, so it should compile.像这样你的代码应该能够访问标签,所以它应该编译。 It still doesn't do anything since none of the methods are called.它仍然没有做任何事情,因为没有调用任何方法。
Edited this on my phone, so couldn't test the code.在我的手机上编辑了这个,所以无法测试代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法通过嵌套类型X访问外部类型X的非静态成员 - Cannot access a non-static member of outer type X via nested type X 无法通过嵌套类型访问外部类型的非静态成员 - Cannot access a non-static member of outer type via nested type 无法通过嵌套类型“FormMain.ImageDelegateClass”访问外部类型“FormMain”的非静态成员 - Cannot access a non-static member of outer type 'FormMain' via nested type 'FormMain.ImageDelegateClass' 无法使用第三方提供的类通过嵌套类型访问外部类型XXX的非静态成员 - Cannot access a non-static member of outer type XXX via nested type with a third-party provided class 无法访问外部类型的非静态成员 - Cannot access a non-static member of outer type C#无法访问外部类型的非静态成员 - C# cannot access a non-static member of outer type 在C#中将一个WindowsForm标签访问到另一个表单 - Access One WindowsForm label to another form in C# 无法通过嵌套类型访问外部类型的非静态成员 - Cannot access a nonstatic member of outer type… via nested type 嵌套类:无法访问静态上下文中的非静态字段 - Nested class: Cannot access non-static field in static context 访问非静态成员所需 - Required to access non-static member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM