从数据库中获取值的类

本文关键字:获取 数据库 | 更新日期: 2023-09-27 18:28:15

如何创建一个从数据库中获取值的类,然后将其显示到textBox?

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;
    }

从数据库中获取值的类

public static class myMethods
{
    public static string 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();
            }
        }
      }
      return name;
    }
}
Form:
private void Button1_Click(object sender, EventArgs e)
{     
    textBox1.Text = myMethods.getName();
}

或者这个:

public static class myMethods
{
    public static void getName(out string name){  
      //.....
    }
}
private void Button1_Click(object sender, EventArgs e)
{
    string name;
    myMethods.getName(out name);
    textBox1.Text = name;
}

您不需要参数:

public static string getName()
{
    string result = "";    
    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())
            {
                result = sqlreader["Name"].ToString();
            }
        }
    }
    return result;
}

private void Button1_Click(object sender, EventArgs e)
{
    textBox1.Text = myMethods.getName();
}

由于只返回单个name,请使用ExecuteScalar并将select语句缩减为仅返回name

public static class myMethods
{
    public static string 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 Name from Setting Order By SettingID Desc", myDatabaseConnection))
            var NameObj = mySqlCommand.ExecuteScalar()
            if NameObj != null then
              name = NameObj.ToString()
        }
        return name;
    }
}        

表单

private void Button1_Click(object sender, EventArgs e)
{
    string name = "";
    name = myMethods.getName();
    textBox1.Text = name;
}