我有一个 MS Access 数据库,它有一个包含几列的表。当我单击一个按钮时,我想从数据库的一列中获取字符串数据行并将其显示在富文本框中。我创建了以下代码:
public static string DisplayListOfTeams()
{
//create a data table object to hold data retrieved from DB
DataSet ds = new DataSet();
//set SQL query to string variable
String sqlQuery = "SELECT * FROM Team";
//instantiate new OleDbDataAdapter object
OleDbDataAdapter adpTeams = new OleDbDataAdapter(sqlQuery, aConnection);
//add data in DB to the dataset object/table
adpTeams.Fill(ds, "TeamTable");
string myValue;
if (ds.Tables["TeamTable"].Rows.Count > 0)
{
//You must cast the value because it is an object
myValue = (string)ds.Tables["TeamTable"].Rows[0][0];
}
else
{
myValue = "No Data found";
}
return myValue;
}
richTextBox1.Text = TeamsDA.DisplayListOfTeams();
我在以下行收到“无法将'System.Int32'类型的对象转换为'System.String'”错误:
myValue = (string)ds.Tables["TeamTable"].Rows[0][0];
我不确定我做错了什么或如何去做我想做的事情。