c#按数据库中的对象名设置属性

时间:2018-01-14 18:55:04

标签: c# sql

我有一个包含100多个按钮的表单应用程序。我查询SQL数据库以获取按钮名称(与应用程序匹配)和所需的文本。它就像:

public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
void post(String url, String json) throws IOException {
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();
    okhttp3.Call call = client.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(okhttp3.Call call, IOException e) {
            Log.e("TAG", "Failed sending message!");
            Toast.makeText(MainActivity.this,"Failed sending message",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onResponse(okhttp3.Call call, Response response) throws IOException {
            Log.d("TAG", "Message sent successfully!");
            Log.d("TAG", response.body().string());
            Toast.makeText(MainActivity.this,"Message sent successfully!",Toast.LENGTH_LONG).show();

        }
    });
}

在应用程序端,我想根据查询结果设置按钮的文本。

实施例;如果查询返回;

Select ButtonName,Text from Table;

我应该在应用程序中设置属性,如

b20 Text1
b21 Text2

怎么做?

1 个答案:

答案 0 :(得分:2)

有通用的方法来查找控件。假设您使用的是Windows窗体,Find method就是您要找的东西:

string name = "..name from DB..";

Control control = ParentContainer.Controls.Find(name);
if (control != null)
  control.Text = ".. text from DB ..";