积极答复者
datatable 读取数据

问题
-
1.DAL
private static string sqlconning = ConfigurationManager.AppSettings["sqlcon"];
SqlConnection con = new SqlConnection(sqlconning);public DataTable getUserTDAL() {
con.Open();
string sql = "select userName,userPwd,userMail from user_tb";
SqlDataAdapter sd = new SqlDataAdapter(sql,con);
DataTable dt = new DataTable();
sd.Fill(dt);
con.Close();
return dt;
}2.BLL
public DataTable getUserTBLL() {
return uDAL.getUserTDAL();
}
3.UI
this.GridView2.DataSource = UB.getUserTBLL();
this.GridView2.DataBind();错误
行 55:
行 56: public DataTable getUserTDAL() {
行 57: con.Open();
行 58: string sql = "select userName,userPwd,userMail from user_tb";
行 59: SqlDataAdapter sd = new SqlDataAdapter(sql,con);
you are welcome- 已移动 Sheng Jiang 蒋晟Moderator 2011年7月31日 3:28 (发件人:Visual C#)
答案
-
你好!
上述代码建议改为下面的方式,程序会自动打开、关闭 、释放 Connection
public DataTable getUserTDAL() { string sql = "select userName,userPwd,userMail from user_tb"; using (SqlDataAdapter sd = new SqlDataAdapter(sql, sqlconning)) { DataTable dt = new DataTable(); sd.Fill(dt); return dt; } }
知识改变命运,奋斗成就人生!- 已标记为答案 威尼斯三人 2011年2月24日 4:13
全部回复
-
你好
可不可以POST 這個ERROR MESSAGE 上來給我們參考參考
或者你可以嘗試更改 一下你的CODE 變成
public DataTable getUserTDAL() {
SqlConnection con = new SqlConnection(sqlconning)
con.Open();
string sql = "select userName,userPwd,userMail from user_tb";
SqlDataAdapter sd = new SqlDataAdapter(sql,con);
DataTable dt = new DataTable();
sd.Fill(dt);
con.Close();
return dt;
}看看能不能解決這個問題Please correct me if my concept is wrong
Chi -
您好,威尼斯三人:
欢迎来到MSDN论坛。
对于SqlDataAdapter, 在编程的时候必须将Sqlconnection关闭后才能使用。 以下是MSDN资料库的原文描述:
This implementation of the SqlDataAdapter opens and closes a SqlConnection if it is not already open. This can be useful in a an application that must call the Fill method for two or more SqlDataAdapter objects. If the SqlConnection is already open, you must explicitly call Close or Dispose to close it.
以上原文,您可以在下面的链接中查看:
http://msdn.microsoft.com/zh-cn/library/aa326274(VS.71).aspx如果您还有任何问题,请随时告知我们。
感谢您的参与。
Neddy Ren [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
SqlDataAdapter sd = new SqlDataAdapter(sql,con);
没有con.Open();可以打开吗?这个在书上怎么没见过,也没遇过
you are welcome您好,SqlDataAdapter会自动监测con是否处于开或闭状态,所以无需con.Open()就能直接用。如果您显示打开 ,例如con.Open()了需要在下面使用完毕后显示 关闭 。例如:
con.Open(); //如果这里出错,应参考肖小勇的建议
string sql = "select userName,userPwd,userMail from user_tb";
SqlDataAdapter sd = new SqlDataAdapter(sql,con);con.Close();
-
SqlDataAdapter sd = new SqlDataAdapter(sql,con);
没有con.Open();可以打开吗?这个在书上怎么没见过,也没遇过
you are welcome您好,SqlDataAdapter会自动监测con是否处于开或闭状态,所以无需con.Open()就能直接用。如果您显示打开 ,例如con.Open()了需要在下面使用完毕后显示 关闭 。例如:
con.Open(); //如果这里出错,应参考肖小勇的建议
string sql = "select userName,userPwd,userMail from user_tb";
SqlDataAdapter sd = new SqlDataAdapter(sql,con);con.Close();
关闭时没用了,如果你写上con.Close();程序还是会出错误的.这个应该像你所说的SqlDataAdapter会自动监测con是否处于开或闭状态,所以无需con.Open()就能直接用吧???。
you are welcome -
SqlDataAdapter sd = new SqlDataAdapter(sql,con);
没有con.Open();可以打开吗?这个在书上怎么没见过,也没遇过
you are welcome您好,SqlDataAdapter会自动监测con是否处于开或闭状态,所以无需con.Open()就能直接用。如果您显示打开 ,例如con.Open()了需要在下面使用完毕后显示 关闭 。例如:
con.Open(); //如果这里出错,应参考肖小勇的建议
string sql = "select userName,userPwd,userMail from user_tb";
SqlDataAdapter sd = new SqlDataAdapter(sql,con);con.Close();
关闭时没用了,如果你写上con.Close();程序还是会出错误的.这个应该像你所说的SqlDataAdapter会自动监测con是否处于开或闭状态,所以无需con.Open()就能直接用吧???。
you are welcome我上面写的是为了帮助你理解使用con.Open();或不使用的区别。并不能排错。要排错我上面已注释了见肖小勇的提示。
1: con.Open();
string sql = "select userName,userPwd,userMail from user_tb";
SqlDataAdapter sd = new SqlDataAdapter(sql,con);
sd.Fill(...);
con.Close();2、string sql = "select userName,userPwd,userMail from user_tb";
SqlDataAdapter sd = new SqlDataAdapter(sql,con);
sd.Fill(...);在单表查询中,方案1和2是等效的。
您现在用的方案2,SqlDataAdapter也会在内部调用con.Open();但没有抱错对吗。因为在SqlDataAdapter的内部,con.Open();被放在try{}finally {}中,所以不会抱错。但是应该查不到任何数据。所以请检查数据库连接字符串是否正确。
一段程序能否正确运行,除了代码外还需要运行的环境。
-
你好!
上述代码建议改为下面的方式,程序会自动打开、关闭 、释放 Connection
public DataTable getUserTDAL() { string sql = "select userName,userPwd,userMail from user_tb"; using (SqlDataAdapter sd = new SqlDataAdapter(sql, sqlconning)) { DataTable dt = new DataTable(); sd.Fill(dt); return dt; } }
知识改变命运,奋斗成就人生!- 已标记为答案 威尼斯三人 2011年2月24日 4:13