在程序中,会根据查询语句得到相应的结果,形式如下:
string sql = "select * from TUser tu where tu.FUserId > 1"; // 查询实体
IList items = GetObjects(sql); // 这个方法是自定义的
经过这个查询,items中是一个个的object,这个时候,我要把object中的内容转换到一个字符串数组中,应该如何操作?
如果查询语句如下:
string sql = "select tu.FUserName, tu.FUserBirth from TUser tu where tu.FUserId > 1"; // 查询属性
IList items = GetObjects(sql);
这个时候,返回,items中是一个个的object[],可以通过下面代码输出其值内容:
object[] rows = (object[])items[0];
Console.WriteLine(rows[0] + "__" + rows[1]);
朋友们,过来看看,出出主意
string sql = "select * from TUser tu where tu.FUserId > 1"; // 查询实体
IList items = GetObjects(sql); // 这个方法是自定义的
经过这个查询,items中是一个个的object,这个时候,我要把object中的内容转换到一个字符串数组中,应该如何操作?
如果查询语句如下:
string sql = "select tu.FUserName, tu.FUserBirth from TUser tu where tu.FUserId > 1"; // 查询属性
IList items = GetObjects(sql);
这个时候,返回,items中是一个个的object[],可以通过下面代码输出其值内容:
object[] rows = (object[])items[0];
Console.WriteLine(rows[0] + "__" + rows[1]);
朋友们,过来看看,出出主意
int i;
for (i = 0 ; i < row.GetLength(0); ++i)
{
ss[i] = rows[i].ToString();
}
object[] rows = (object[])items[0];
Console.WriteLine(rows[0] + "__" + rows[1]); // all right
string sql = "select * from TUser tu where tu.FUserId > 1"; // 查询实体
IList items = GetObjects(sql); // 这个方法是自定义的for ( int i = 0; i < items.Count; i++ )
{
if ( items[i].GetType().ToString() == "System.Object[]" )
{
object[] rows = (object[])items[i];
string[] strTemp = new string[rows.GetLength(0)];
int j = 0;
while ( j < rows.GetLength(0) )
{
strTemp[j] = rows[j];
j++;
}
}
else
{
Type theType = items[i].GetType();
PropertyInfo[] infoArray = theType.GetProperties();
foreach ( PropertyInfo info in infoArray )
{
Console.WriteLin(info.GetValue(items[i], null));
}
}这段代码可以解决问题
ArrayList ary = new ArrayList();
ary.AddRange(items);//这样就转换完了
string[] s = (string[])ary.ToArray(typeof(string));