Pass value by argument (2)

Asked 2 years ago, Updated 2 years ago, 40 views

I'm practicing argument passing and then trying to display the values I received from DB.I have created a DB connection/value acquisition class and also created a class to display, but how should I use the arguments in this case? Please give me some advice...

(DBConnection class)

public static void Main()
    {
        string words;
        string con;
        con="Server=localhost;Port=532;UserId=postgres;Password=Password;Database=voc";
        NpgsqlConnection conn = new NpgsqlConnection(con);
        conn.Open();
        varcmd = new NpgsqlCommand(@"select*fromt_voc", conn);
        try
        {

            NpgsqlDataReader dataReader=cmd.ExecuteReader();
            while(dataReader.Read())
            {
                for (inti=0;i<dataReader.FieldCount;i++)
                {
                    words=dataReader[i].ToString();
                    return words;
                }
            }

        }
        catch (Exception e)
        {
            var ArgumentException = new ArgumentException();

        }

        conn.Close();

(Display Class)

publicMainForm()
    {
        InitializeComponent();
    }

    private static void StartButtonClick (object sender, EventArgse)
    {
      // I'm still in the middle, but it's very complicated here.
      string words = MainForm.

    }

c#

2022-09-30 14:08

1 Answers

I guess the ideal format is as follows, but

// Type to be used as return value
class Row
{
    // define the required columns
    public string ColumnName1 {get;set;}
    public int ColumnName2 {get;set;}
    public bool ColumnName3 {get;set;}
}

class DBCConnection:IDisposable // This class name is named DbConnection, so we recommend that you change it.
{
    // Move from local variable
    NpgsqlConnection conn;

    // Change from local variable con to property
    public string ConnectionString {get;set;}
        // ConnectionString initial value (should be retrieved from configuration file)
        = "Server=localhost; Port=532; User Id=postgres; Password=Password; Database=voc";

    // The method is changed as follows.
    // public static void Main()
    public List<Row>GetData()
    {
        // Use a list if you are expecting multiple lines
        // string words;
        List<Row>list=new List<Row>();

        // Go to Instance
        // string con;
        // con="Server=localhost;Port=532;UserId=postgres;Password=Password;Database=voc";

        // Since it is instantiated, start connecting only for the first time.
        // NpgsqlConnection conn = new NpgsqlConnection(con);
        if(conn==null)
        {
            conn = new NpgsqlConnection (ConnectionString);

            // The exception handling will be moved here.
            try
            {
                conn.Open();
            }
            catch (Exception e)
            {
                through new InvalidOperationException("An exception occurred while initiating a connection.", e);
            }
        }
        varcmd = new NpgsqlCommand(@"select*fromt_voc", conn);

        // go to conn.Open()
        //try
        //{

        NpgsqlDataReader dataReader=cmd.ExecuteReader();
        while(dataReader.Read())
        {
            // set a line-by-line value
            // for (inti=0;i<dataReader.FieldCount;i++)
            //{
            //    words=dataReader[i].ToString();
            //    return words;
            //}
            var row = new Row();
            row.ColumnName1 = dataReader.GetString(1);
            row.ColumnName2 = dataReader.GetInt32(4);
            row.ColumnName3 = dataReader.GetBoolean(9);

            // Add a row to the list of return values.
            list.Add(row);
        }

        // go to conn.Open()
        //}
        // catch (Exception e)
        //{
        //    var ArgumentException = new ArgumentException();

        //}

        // close only once
        // conn.Close();

        // Finally, return the value.
        return list;
    }

    // common close processing
    public voiddispose()
    {
        conn?.Dispose();
        conn = null;
    }
}

// display side
private static void StartButtonClick (object sender, EventArgse)
{
    // ensure disposition with using
    using(varc=new DBConnection())
    {
        // How to Pass Data
        c. ConnectionString=...;
        vardata=c.GetData();
        textBox1.Text=data[0].ColumnName1;
    }
}

As mentioned above, there are many corrections, so I think it would be better to learn about data access through simple NpgsqlDataAdapter+DataTable.


2022-09-30 14:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.