使用SQLDataReader读取数据问题
进行单步调试leg值为7,但是判断reader.Reader()时就进不到if里面,直接跳到else中去,百思不得其解,哪个位大哥知道的指点一下,谢谢!代码如下:publ...
进行单步调试leg值为7,但是判断reader.Reader()时就进不到if里面,直接跳到else中去,百思不得其解,哪个位大哥知道的指点一下,谢谢!代码如下:
public static DepartInfo GetDepartInfoByDepartId(int departId)
{
string sql = "Select * From TB_DepartInfo where dep_Id=@DepartId";
int branchId;
try
{
SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@DepartId", departId));
int leg = reader.FieldCount;
if (reader.Read())
{
DepartInfo departInfo = new DepartInfo();
departInfo.DepartId = (int)reader["dep_Id"];
departInfo.DepartName = (string)reader["dep_Name"];
departInfo.PrincipalUser = (string)reader["pri_User"]; //FK
departInfo.ConnectTelNo = (long)reader["dep_ConnectTelNo"];
departInfo.ConnectMobileTelNo = (long)reader["dep_ConnectMobileTelNo"];
departInfo.Faxes = (long)reader["dep_Faxes"];
branchId = (int)reader["bra_Id"]; //FK
reader.Close();
reader.Dispose();
departInfo.Branch = BranchInfoService.GetBranchInfoByBranchId(branchId);
//departInfo.departInfo = UserInfoService.GetUserInfoByUserId(principalUser);
return departInfo;
}
else
{
reader.Close();
reader.Dispose();
return null;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
//throw e;
return null;
}
} 展开
public static DepartInfo GetDepartInfoByDepartId(int departId)
{
string sql = "Select * From TB_DepartInfo where dep_Id=@DepartId";
int branchId;
try
{
SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@DepartId", departId));
int leg = reader.FieldCount;
if (reader.Read())
{
DepartInfo departInfo = new DepartInfo();
departInfo.DepartId = (int)reader["dep_Id"];
departInfo.DepartName = (string)reader["dep_Name"];
departInfo.PrincipalUser = (string)reader["pri_User"]; //FK
departInfo.ConnectTelNo = (long)reader["dep_ConnectTelNo"];
departInfo.ConnectMobileTelNo = (long)reader["dep_ConnectMobileTelNo"];
departInfo.Faxes = (long)reader["dep_Faxes"];
branchId = (int)reader["bra_Id"]; //FK
reader.Close();
reader.Dispose();
departInfo.Branch = BranchInfoService.GetBranchInfoByBranchId(branchId);
//departInfo.departInfo = UserInfoService.GetUserInfoByUserId(principalUser);
return departInfo;
}
else
{
reader.Close();
reader.Dispose();
return null;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
//throw e;
return null;
}
} 展开
5个回答
展开全部
FieldCount 是列的数量,并非行,reader.Read()必须有经行,即数据。它们是一纵一横的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
定义错误
追问
定义错误?何谓定义错误啊.解释一下啊。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先将语句放数据库里面 带入相应的参数看看有没有值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SqlDataReader red = new SqlDataReader();
if (red.HasRows) //是否有数据行
{
while (red.Read())
{
}
}
if (red.HasRows) //是否有数据行
{
while (red.Read())
{
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询