-5

データベースから値を取得して 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;
    }
4

3 に答える 3

2
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;
}
于 2013-08-20T10:15:49.810 に答える
0

単一の値のみを返すため、選択ステートメントをname使用して削減し、返すだけにしますExecuteScalarname

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;
}
于 2013-08-20T10:25:58.407 に答える
0

パラメータは必要ありません:

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();
}
于 2013-08-20T10:16:27.593 に答える