ExecuteReader: Connection 属性尚未初始化。里面有我的代码

felix61 2003-07-29 09:09:41
public int ExecuteScalar(string sSql)
{

int iRe;
OpenConnect();
command.CommandType= CommandType.Text;
command.CommandText= sSql;
iRe=(int)command.ExecuteScalar();
CloseConnect();
return iRe;
}
...全文
40 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Knight94 2003-07-29
  • 打赏
  • 举报
回复
执行查询之前增加如下:
command.Connection=connection;
felix61 2003-07-29
  • 打赏
  • 举报
回复
解决了!谢谢你
felix61 2003-07-29
  • 打赏
  • 举报
回复
public DBObject( string connectionString )
{
this.connectionString = connectionString;
connection = new SqlConnection( this.connectionString );
}
我在初始化的事时候已经给他初始化了

/// <summary>
/// 打开数据库连接
/// </summary>
public void OpenConnection()
{
if(connection.State!= ConnectionState.Closed)
connection.Close();
else
connection.Open();
}

这是我的打开方法
你们帮我看看那儿问题
brightheroes 2003-07-29
  • 打赏
  • 举报
回复
SqlCommand 没有指定SqlConnection.
对于你这个方法。
OpenConnect();
CloseConnect();
有点琐碎了。
xixigongzhu 2003-07-29
  • 打赏
  • 举报
回复
command的属性Connection没有值,你调用OpenConnect()方法肯定有个连接对象,你把这个对象赋给command的属性Connection了吗?

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧