DBReader提供字符串的长度而不是字符串
本文关键字:字符串 DBReader | 更新日期: 2023-09-27 18:07:00
目前我的问题是,当我运行代码时,它显示了一个数据网格与名称的length
,而不是名称本身。任何人得到任何线索,都显得很愚蠢。
List<string> NameList = new List<string>();
string connectionString = "Server = localhost; Database = TestDb; Trusted_Connection = True;";
try
{
IDbConnection dbcon;
using (dbcon = new SqlConnection(connectionString))
{
dbcon.Open();
using (IDbCommand dbcmd = dbcon.CreateCommand())
{
string sql = "Select * from people";
dbcmd.CommandText = sql;
using (IDataReader reader = dbcmd.ExecuteReader())
{
while (reader.Read())
{
string FirstName = (string) reader["ForeName"];
NameList.Add(FirstName);
}
DataGrid1.ItemsSource = NameList.ToList();
reader.Close();
dbcon.Close();
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
将List<string>
更改为如下内容
IList<string> NameList = new List<string>();
DataGrid1.DataSource = NameList.Select(s => new
{
Value = s
}).ToList();
沿着这些行试试