在c中从mysql读取特定行#

qzwqbdag  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(308)

所以我对编程还比较陌生,但我学得很快,所以我现在坚持的是,我需要从sql表中读取数据,但我只想通过数据库中的一个键读取一行数据。
我在这里的代码做的工作,但我很肯定你可以做得更顺利很多,因为这不是很干净。
我怎么能用另一种更简单的方法来做呢?
我想要的结果是将所有行添加到它们自己的字符串中,这样我就可以更容易地将它们用于程序中的标签和其他信息。

private static MySqlConnection dbConn;

    static string MySQLConnectionString = "Server='ip';Port='port';Database='name';User='user';Password='Password';SslMode='none'";

    public static void InitializeDB()
    {
        dbConn = new MySqlConnection(MySQLConnectionString);

        string commandSuperuser = "SELECT * FROM sl WHERE User=(1)";
        string commandUserOne = "SELECT * FROM sl WHERE User=(1)";
        string commandUserTwo = "SELECT * FROM sl WHERE User=(2)";
        string commandUserThree = "SELECT * FROM sl WHERE User=(3)";
        string commandUserFour = "SELECT * FROM sl WHERE User=(4)";
        string commandUserFive = "SELECT * FROM sl WHERE User=(5)";
        string commandUserSix = "SELECT * FROM sl WHERE User=(6)";
        string commandUserSeven = "SELECT * FROM sl WHERE User=(7)";
        string commandUserEight = "SELECT * FROM sl WHERE User=(8)";
        MySqlCommand cmd1 = new MySqlCommand(commandSuperuser, dbConn);
        dbConn.Open();

        MySqlDataReader reader1 = cmd1.ExecuteReader();
        while (reader1.Read())
        {
            UserSuperuserName = reader1["Name"].ToString() + " " + reader1["Lastname"].ToString();
            UserSuperuserENumber = reader1["ENumber"].ToString();
            UserSuperuserNumber = reader1["Number"].ToString();
            UserSuperuserNickname = reader1["Nickname"].ToString();
            UserSuperuserMail = reader1["Email"].ToString();

        }
        dbConn.Close();

        MySqlCommand cmd2 = new MySqlCommand(commandUserOne, dbConn);
        dbConn.Open();

        MySqlDataReader reader2 = cmd2.ExecuteReader();
        while (reader2.Read())
        {
            UserOneName = reader2["Name"].ToString() + " " + reader2["Lastname"].ToString();
            UserOneENumber = reader2["ENumber"].ToString();
            UserOneNumber = reader2["Number"].ToString();
            UserOneNickname = reader2["Nickname"].ToString();
            UserOneMail = reader2["Email"].ToString();

        }
        dbConn.Close();

        MySqlCommand cmd3 = new MySqlCommand(commandUserTwo, dbConn);
        dbConn.Open();

        MySqlDataReader reader3 = cmd3.ExecuteReader();
        while (reader3.Read())
        {
            UserTwoName = reader3["Name"].ToString() + " " + reader3["Lastname"].ToString();
            UserTwoENumber = reader3["ENumber"].ToString();
            UserTwoNumber = reader3["Number"].ToString();
            UserTwoNickname = reader3["Nickname"].ToString();
            UserTwoMail = reader3["Email"].ToString();

        }
        dbConn.Close();

        MySqlCommand cmd4 = new MySqlCommand(commandUserThree, dbConn);
        dbConn.Open();

        MySqlDataReader reader4 = cmd4.ExecuteReader();
        while (reader4.Read())
        {
            UserThreeName = reader4["Name"].ToString() + " " + reader4["Lastname"].ToString();
            UserThreeENumber = reader4["ENumber"].ToString();
            UserThreeNumber = reader4["Number"].ToString();
            UserThreeNickname = reader4["Nickname"].ToString();
            UserThreeMail = reader4["Email"].ToString();

        }
        dbConn.Close();

        MySqlCommand cmd5 = new MySqlCommand(commandUserFour, dbConn);
        dbConn.Open();

        MySqlDataReader reader5 = cmd5.ExecuteReader();
        while (reader5.Read())
        {
            UserFourName = reader5["Name"].ToString() + " " + reader5["Lastname"].ToString();
            UserFourENumber = reader5["ENumber"].ToString();
            UserFourNumber = reader5["Number"].ToString();
            UserFourNickname = reader5["Nickname"].ToString();
            UserFourMail = reader5["Email"].ToString();

        }
        dbConn.Close();

        MySqlCommand cmd6 = new MySqlCommand(commandUserFive, dbConn);
        dbConn.Open();

        MySqlDataReader reader6 = cmd6.ExecuteReader();
        while (reader6.Read())
        {
            UserFiveName = reader6["Name"].ToString() + " " + reader6["Lastname"].ToString();
            UserFiveENumber = reader6["ENumber"].ToString();
            UserFiveNumber = reader6["Number"].ToString();
            UserFiveNickname = reader6["Nickname"].ToString();
            UserFiveMail = reader6["Email"].ToString();

        }
        dbConn.Close();

        MySqlCommand cmd7 = new MySqlCommand(commandUserSix, dbConn);
        dbConn.Open();

        MySqlDataReader reader7 = cmd7.ExecuteReader();
        while (reader7.Read())
        {
            UserSixName = reader7["Name"].ToString() + " " + reader7["Lastname"].ToString();
            UserSixENumber = reader7["ENumber"].ToString();
            UserSixNumber = reader7["Number"].ToString();
            UserSixNickname = reader7["Nickname"].ToString();
            UserSixMail = reader7["Email"].ToString();

        }
        dbConn.Close();

        MySqlCommand cmd8 = new MySqlCommand(commandUserSeven, dbConn);
        dbConn.Open();

        MySqlDataReader reader8 = cmd8.ExecuteReader();
        while (reader8.Read())
        {
            UserSevenName = reader8["Name"].ToString() + " " + reader8["Lastname"].ToString();
            UserSevenENumber = reader8["ENumber"].ToString();
            UserSevenNumber = reader8["Number"].ToString();
            UserSevenNickname = reader8["Nickname"].ToString();
            UserSevenMail = reader8["Email"].ToString();

        }
        dbConn.Close();

        MySqlCommand cmd9 = new MySqlCommand(commandUserEight, dbConn);
        dbConn.Open();

        MySqlDataReader reader9 = cmd9.ExecuteReader();
        while (reader9.Read())
        {
            UserEightName = reader9["Name"].ToString() + " " + reader9["Lastname"].ToString();
            UserEightENumber = reader9["ENumber"].ToString();
            UserEightNumber = reader9["Number"].ToString();
            UserEightNickname = reader9["Nickname"].ToString();
            UserEightMail = reader9["Email"].ToString();

        }
        dbConn.Close();

    }

如果有人有什么好的建议/例子。

krugob8w

krugob8w1#

您可以使用mysqldataadapter和datatable并将值保存到字符串数组中。

public static void InitializeDB()
    {
        dbConn = new MySqlConnection(MySQLConnectionString);

        dbConn.Open();
        try
        {
            using (MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM S1", dbConn))
            {
                DataTable dt = new DataTable();
                da.Fill(dt);
                int a = 0;
                if (dt.Rows.Count > 0)
                {
                    string[] UserName = new string[dt.Rows.Count];
                    string[] UserENumber = new string[dt.Rows.Count];
                    string[] UserNumber = new string[dt.Rows.Count];
                    string[] UserNickname = new string[dt.Rows.Count];
                    string[] UserMail = new string[dt.Rows.Count];

                    for (a = 0; a < dt.Rows.Count; a++)
                    {
                        UserName[a] = dt.Rows[a]["Name"].ToString();
                        UserENumber[a] = dt.Rows[a]["Enumber"].ToString();
                        UserNumber[a] = dt.Rows[a]["Number"].ToString();
                        UserNickname[a] = dt.Rows[a]["Nickname"].ToString();
                        UserMail[a] = dt.Rows[a]["Mail"].ToString();
                    }

                }

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            dbConn.Close();
        }
    }

相关问题