2013-08-20 205 views
-5

如何創建一個從數據庫獲取值並將其顯示到文本框的類?從數據庫中獲取值的類

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

回答

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

謝謝王:) –

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(); 
} 
0

既然你只返回一個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; 
}