c#uwp视图二数据库行

v9tzhpje  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(289)
string myConnectionString;
myConnectionString = "server=localhost;uid=root;pwd=root;database=medicloud;SslMode=None;charset=utf8";

MySqlConnection connection = new MySqlConnection(myConnectionString);
MySqlCommand cmd = new MySqlCommand();

cmd.CommandType = CommandType.Text;
string sqlStr = "Select arm from assign where userId=@name";
cmd.Parameters.AddWithValue("@name", txtValue.Text);

cmd.CommandText = sqlStr;
cmd.Connection = connection;

EncodingProvider ppp;
ppp = CodePagesEncodingProvider.Instance;
Encoding.RegisterProvider(ppp);

connection.Open();

MySqlDataReader reader = cmd.ExecuteReader();
reader.Read();
string databaseLine = (reader["arm"].ToString());
connection.Close();

# endregion

txtExercise.Text = databaseLine + " (Uncomplete)\n" + databaseLine;

我试图查看数据库中的两行数据。但是,我不知道该怎么看。有人能帮我吗?我试着输入“databaseline”两次,但都看到同一行

如你所见,瑞秋有两种锻炼方式。我想在txtexercise.text查看这两个练习。但是我只能看到第一个,我不能看到第二个。

wqsoz72f

wqsoz72f1#

reader.Read() 将读卡器移到下一行。只调用一次,因此只读取第一行。
你可以做一个循环。

MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()) //reader.Read returns false when there are no more rows.
{
    string databaseLine = (reader["arm"].ToString()); //read the value of the current row
    txtExercise.Text += databaseLine + " (Uncomplete)\n"; //append the value to the output        
}

相关问题