我将如何创建一个从数据库中获取值然后将其显示到文本框的类?
public static class myMethods
{
public static void getName(string name){
ConnectionStringSettings myConnectionString = ConfigurationManager.ConnectionStrings["LibrarySystem.Properties.Settings.LibraryConnectionString"];
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand mySqlCommand = new SqlCommand("select Top 1 * from Setting Order By SettingID Desc", myDatabaseConnection))
using (SqlDataReader sqlreader = mySqlCommand.ExecuteReader())
{
if (sqlreader.Read())
{
name = sqlreader["Name"].ToString();
}
}
}
}
}
Form:
private void Button1_Click(object sender, EventArgs e)
{
string name = "";
myMethods.getName(name);
textBox1.Text = name;
}