我一直在使用Google搜索,但沒有找到任何結果。大多數人在void中聲明數組。像這樣如何從數據庫中獲取值並將其添加到C#中的數組中#
private void Something(){
ArrayList list = new ArrayList();
list.Add("awdawd");
}
但我必須聲明數組之外,因爲它會從不同的空隙中使用。這是我的代碼:
public string[] Names= { "Rick", "Morty", "John" };
,併爲DB:
private void GetNames() {
string cmdText = "SELECT * FROM Login";
// using (SqlConnection cnn = new SqlConnection("Server=.\\SQLEXPRESS;Database=TestDB"))
using (SqlCommand SelectCommand = new SqlCommand(cmdText, connectionstring))
{
SqlDataReader myReader;
connectionstring.Open();
myReader = SelectCommand.ExecuteReader();
}
}
我試圖使它Names.add( 「馬里奧」); 但它沒有工作。
有什麼建議嗎?
是。不要使用數組。改爲使用列表。 –
是否要**將這些名稱添加到預定義的名稱或**將初始數組替換爲數據庫中的值?數組沒有'Add'方法。如果你想使用'Add',使用'List <>'而不是數組。 –
你應該學習字符串數組和'ArrayList'之間的區別 – Pikoh