可以使用ADO.NET来连接数据库并获取数据,然后将数据赋给button的text属性。以下是一个简单的示例:

1.连接数据库:

string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();

2.执行查询语句并获取数据:

string query = "SELECT * FROM myTable";
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    string data = reader["columnName"].ToString();
    //将数据赋给button的text属性
    myButton.Text = data;
}

3.关闭连接:

reader.Close();
connection.Close();

请注意,在实际应用中,为了避免SQL注入攻击等安全问题,应该使用参数化查询语句。


原文地址: https://www.cveoy.top/t/topic/bIYY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录