尝试从文本框中的数据库返回用户 [英] Trying to return users from a database in a textbox

查看:81
本文介绍了尝试从文本框中的数据库返回用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的主要课程

  public  MainPage()
{
.InitializeComponent();


}

private async void Button_Click( object sender,RoutedEventArgs e)
{
UsersObject myGetRequest = new UsersObject();
myGetRequest.users = await GetRequest();

// 迭代你所有用户:
foreach var item in myGetRequest。用户)
{
// 做点什么


}
ResultTextBlock.Text = myGetRequest.users.ToString();


}


public static async 任务< List< UserObject>> GetRequest()
{
Uri geturi = new Uri( URL); // 替换您的网址
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
System.Net.Http.HttpResponseMessage responseGet = await client.GetAsync(geturi);
string response = await responseGet.Content.ReadAsStringAsync();
// 返回回复;

var json = await responseGet.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject< List< UserObject>>(json);

返回结果;
}


}





我也有这个userobject class

  public   class  UsersObject { public 列表< UserObject> users =  new  List< UserObject>(); } 
public class UserObject
{
public string username { get ; set ; }
// public string password {get;组; }
// public int Id {get;组; }
// public object userscol {get;组; }
// public string __href {get;组; }
}





我正在尝试返回数据库中的用户,而是我正在运行我的代码时传递UserObject。任何人都可以在这里建议我需要更改以获取数据库中的用户列表吗?



我尝试了什么:



这是我迄今为止最好的尝试,在此之前我可以将数据作为JSON传回,但它显示了我不想要的用户密码我只是想要来自MySQL数据库的用户名

解决方案

根据您提供的内容,假设其他一切正常,此代码应足以以逗号分隔



 ResultTextBlock.Text =  string  .Join( ,myGetRequest.users.Select(p = >  p .username)); 


This is my main class

public MainPage()
       {
           this.InitializeComponent();


       }

       private async void Button_Click(object sender, RoutedEventArgs e)
       {
           UsersObject myGetRequest = new UsersObject();
           myGetRequest.users = await GetRequest();

           //to iterate all users you got do this:
           foreach (var item in myGetRequest.users)
           {
               //do something


           }
           ResultTextBlock.Text = myGetRequest.users.ToString();


       }


       public static async Task<List<UserObject>> GetRequest()
       {
           Uri geturi = new Uri("url"); //replace your url
           System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
           System.Net.Http.HttpResponseMessage responseGet = await client.GetAsync(geturi);
           string response = await responseGet.Content.ReadAsStringAsync();
           //return response;

           var json = await responseGet.Content.ReadAsStringAsync();
           var result = JsonConvert.DeserializeObject<List<UserObject>>(json);

           return result;
       }


   }



And I also have this userobject class

public class UsersObject { public List<UserObject> users = new List<UserObject>(); }
  public class UserObject
  {
      public string username { get; set; }
      //public string password { get; set; }
      //public int Id { get; set; }
      //public object userscol { get; set; }
      //public string __href { get; set; }
  }



I'm trying to return the users in the database but instead i'm getting the UserObject being passed when I run my code. Anyone be able to suggest what I need to change here to get the lists of users from the database?

What I have tried:

This is my best attempt at it so far and before this I could get the data passed back as JSON but it shows the users passwords which I do not want I just want the usernames from a MySQL database

解决方案

Based on what you provided, assuming everything else are working, this code should be enough to retrieve the username in comma separated

ResultTextBlock.Text = string.Join(",", myGetRequest.users.Select(p => p.username));


这篇关于尝试从文本框中的数据库返回用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆