C# 中 一个对象的属性是另外一个对象

[复制链接]
查看11 | 回复2 | 2011-5-31 09:06:28 | 显示全部楼层 |阅读模式
在查询的时候 怎么处理,我这样写:
#region 值类型或简单引用类型

string temp = string.Empty;

temp = dt.Rows[Constants.USER_UALLEXP].ToString();

if (!string.IsNullOrEmpty(temp))

user.UAllExp = Convert.ToInt32(temp);

temp = dt.Rows[Constants.USER_UBIRTHDAY].ToString();

if (!string.IsNullOrEmpty(temp))

user.UBirthday = Convert.ToDateTime(temp);

temp = dt.Rows[Constants.USER_UCELLPHONE].ToString();

if (!string.IsNullOrEmpty(temp))

user.UCellPhone = temp.ToString();

........

#endregion

#region 对象型

temp = dt.Rows[Constants.USER_USTATE].ToString();

if (!string.IsNullOrEmpty(temp))

{

UStateDao usd = new UStateDAOImpl();

user.UState = usd.FindAllById(Convert.ToInt32(temp));

}

temp = dt.Rows[Constants.USER_UANIMAL].ToString();

if (!string.IsNullOrEmpty(temp))

{

UAnimalDAO uad = new UAnimalDAOImpl();

user.UAnimal = uad.FindAllById(Convert.ToInt32(temp));

}..........
在查询的时候 数据库连接那里发生错误,不是我的连接错了是前面的字段都查出来了,查到第N个的时候,报错(too many connections),意思是打开的连接过多 ,
我的连接类里面是这样写的:
private MySqlConnection GetConnection

{

get {

if (getConnection == null)

{

getConnection = new MySqlConnection(conString);

getConnection.Open();

}

else if (getConnection.State == ConnectionState.Closed)

{

getConnection.Open();

}

else if (getConnection.State == ConnectionState.Broken)

{

getConnection.Close();

getConnection.Open();

}

return getConnection;

}

}
调用的时候是这样的:
public DataTable GetDataTable(string sql)

{

MySqlDataAdapter msda = new MySqlDataAdapter(sql, this.GetConnection);

DataTable dt = new DataTable();

msda.Fill(dt);

return dt;

}
怎么处理呢。。。
请高手赐教。。。

回复

使用道具 举报

千问 | 2011-5-31 09:06:28 | 显示全部楼层
每次连接都没关掉吧。弄个全局的,或者不是频繁操作数据库的就没用一个关一个!...
回复

使用道具 举报

千问 | 2011-5-31 09:06:28 | 显示全部楼层
不用这样写吧,直接写个类就好了。...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行