首页 > 解决方案 > 连接到 SQL 时,变量似乎无法在 c# 上接收值

问题描述

我正在编写一个算法来交付明天,它需要将 C# 与 SQL 数据库集成以进行测试。

我设法进行了集成,它顺利地连接到数据库,但是当我向数据库添加一个值时,它给出了一个异常错误,表明其中一个变量给出了一个空值。通过进一步调查,我意识到其中一个变量没有收到应有的值,所以它给出了这个异常错误,我该如何解决?

这是代码:

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

// add data function classes
using System.Data;

namespace LoginSystem
{
    public class DBConnect
    {
        string ConectionString = "";  // save connection string
        public MySqlConnection connection = null;
        public string server = "127.0.0.1:3306";// MySQL host / ip of the computer
        public string user = "root";// MySQL user
        public string password = "@Gilberto099";// MySQL password 
        DataSet ds;
        DataTable dt;
        public string Table = "teste1"; // initialize db table
        public string ConnectionType = "";
        string RecordSource = "";

        DataGridView tempdata;

        public DBConnect()
        {

        }

        // function to connect to the database
        public void Connect(string database_name)
        {
            try
            {

                ConectionString = "SERVER=" + server + ";" + "DATABASE=" + database_name + ";" + "UID=" + user + ";" + "PASSWORD=" + password + ";";

                connection = new MySqlConnection(ConectionString);
            }
            catch (Exception E)
            {
                MessageBox.Show(E.Message);
            }
        }

        // Function to execute select statements
        public void ExecuteSql(string Sql_command)
        {

            nowquiee(Sql_command);

        }

        // creates connection to MySQL before execution
        public void nowquiee(string sql_comm)
        {
            try
            {
                MySqlConnection cs = new MySqlConnection(ConectionString);
                cs.Open();
                MySqlCommand myc = new MySqlCommand(sql_comm, cs);
                myc.ExecuteNonQuery();
                cs.Close();


            }
            catch (Exception err)
            {

                MessageBox.Show(err.Message);
            }
        }

        // function to execute delete , insert and update
        public void Execute(string Sql_command)
        {
            RecordSource = Sql_command;
            Table = ConnectionType;
            dt = new DataTable(ConnectionType);
            try
            {
                string command = RecordSource.ToUpper();

                //======================if sql contains select==========================================
                MySqlDataAdapter da2 = new MySqlDataAdapter(RecordSource, connection);

                DataSet tempds = new DataSet();
                da2.Fill(tempds, ConnectionType);
                da2.Fill(tempds);

                //======================================================================================


            }
            catch (Exception err) { MessageBox.Show(err.Message); }
        }

        // function to bring selected results based on column name and row index
        public string Results(int ROW, string COLUMN_NAME)
        {
            try
            {
                return dt.Rows[ROW][COLUMN_NAME].ToString();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return "";

            }
        }

        // function to bring selected results based on column index and row index
        public string Results(int ROW, int COLUMN_NAME)
        {
            try
            {
                return dt.Rows[ROW][COLUMN_NAME].ToString();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return dt.Rows[ROW][COLUMN_NAME].ToString();

            }
        }
        public string ResultsColumn(string COLUMN_NAME)
        {
            try
            {
                return dt.Columns[COLUMN_NAME].ToString();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return "";

            }
        }
        public string ResultsColumni(int COLUMN_NAME)
        {
            try
            {
                return dt.Columns[COLUMN_NAME].ToString();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return dt.Columns[COLUMN_NAME].ToString();

            }
        }

        // Execute select statement
        public void ExecuteSelect(string Sql_command)
        {
            RecordSource = Sql_command;
            ConnectionType = Table;

            dt = new DataTable(ConnectionType);
            try
            {
                string command = RecordSource.ToUpper();
                MySqlDataAdapter da = new MySqlDataAdapter(RecordSource, connection);
                ds = new DataSet();
                da.Fill(ds, ConnectionType);
                da.Fill(dt);
                tempdata = new DataGridView();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }


        }

        // count Number of rows after select
        public int Count()
        {
            return dt.Rows.Count;
        }
    }
}

标签: c#mysqlsql

解决方案


它不应该是字符串数据类型吗?(int COLUMN_NAME)

// function to bring selected results based on column index and row index
    public string Results(int ROW, int COLUMN_NAME)
    {
        try
        {
            return dt.Rows[ROW][COLUMN_NAME].ToString();
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message);
            return dt.Rows[ROW][COLUMN_NAME].ToString();

        }
    }

推荐阅读