試圖輸出所有的從MSSQL數據庫中的列的內容到一個數組,此刻我正在此爲輸出:輸出內容
ConsoleApplication3.Program+ClassName
ConsoleApplication3.Program+ClassName
如果我添加一列額外的行然後會有三個相同的輸出。下面是處理所有這些的代碼。
public class ClassName
{
public string Col1 { get; set; }
}
static void Main(string[] args)
{
try
{
SqlConnection con = new SqlConnection("Data Source=;Network Library=DBMSSOCN; Initial Catalog = Backups; User ID = BackupsU; Password = ; ");
ClassName[] allRecords = null;
string sql = @"SELECT company_Name FROM Company";
using (var command = new SqlCommand(sql, con))
{
con.Open();
using (var reader = command.ExecuteReader())
{
var list = new List<ClassName>();
while (reader.Read())
list.Add(new ClassName { Col1 = reader.GetString(0) });
allRecords = list.ToArray();
}
foreach (var item in allRecords)
{
Console.WriteLine(item.ToString());
}
Console.ReadKey();
Console.WriteLine();
}
工作完美,謝謝! – xjacksssss