如何读不同数据库字段类型的值
定义变量:
private const string SQL_SELECT_BODY_BYID = "SELECT * FROM Health_T_Layout WHERE LayoutID=@LayoutID";
private const string PARM_ENTITY_LayoutID = "@LayoutID";
private const string PARM_ENTITY_LayoutCols = "@LayoutCols";
private const string PARM_ENTITY_LayoutRows = "@LayoutRows";
private const string PARM_ENTITY_WidthPercent = "@WidthPercent";
private const string PARM_ENTITY_Remark = "@Remark";
private const string PARM_ENTITY_LayoutID = "@LayoutID";
private const string PARM_ENTITY_LayoutCols = "@LayoutCols";
private const string PARM_ENTITY_LayoutRows = "@LayoutRows";
private const string PARM_ENTITY_WidthPercent = "@WidthPercent";
private const string PARM_ENTITY_Remark = "@Remark";
获得实体类
#region 根据布局ID得到布局实体类
/// <summary>
/// 根据布局ID得到布局实体类
/// </summary>
/// <param name="questionID">布局ID</param>
/// <returns>布局的实体类</returns>
public static EntityLayout GetEntityLayoutBYID(int layoutID)
{
SqlConnection myConn = DB.getCon();
try
{
myConn.Open();
SqlCommand myComm = new SqlCommand(SQL_SELECT_BODY_BYID, myConn);
SqlParameter parm_LayoutID = new SqlParameter(PARM_ENTITY_LayoutID, SqlDbType.Int, 4);
parm_LayoutID.Value = layoutID;
myComm.Parameters.Add(parm_LayoutID);
SqlDataReader sdr = myComm.ExecuteReader();
while (sdr.Read())
{
EntityLayout entityLayout = new EntityLayout();
entityLayout.LayoutCols = (sdr["LayoutCols"] == DBNull.Value) ? 0 : Convert.ToInt32(sdr["LayoutCols"]);
entityLayout.LayoutID = (sdr["LayoutID"] == DBNull.Value) ? 0:Convert.ToInt32(sdr["LayoutID"]);
entityLayout.LayoutRows = (sdr["LayoutRows"] == DBNull.Value)?0:Convert.ToInt32(sdr["LayoutRows"]);
entityLayout.WidthPercent =(sdr["WidthPercent"] == DBNull.Value)?0:Convert.ToInt32(sdr["WidthPercent"]);
entityLayout.Remark = (sdr["Remark"] == DBNull.Value) ? string.Empty : Convert.ToString(sdr["Remark"]);
entityLayout.Status = (sdr["Status"] == DBNull.Value) ? false : Convert.ToBoolean(dr["Status"]);
return entityLayout;
}
sdr.Close();
sdr.Dispose();
}
catch
{
return null;
}
finally
{
DB.close(myConn);
}
return null;
}
#endregion
/// <summary>
/// 根据布局ID得到布局实体类
/// </summary>
/// <param name="questionID">布局ID</param>
/// <returns>布局的实体类</returns>
public static EntityLayout GetEntityLayoutBYID(int layoutID)
{
SqlConnection myConn = DB.getCon();
try
{
myConn.Open();
SqlCommand myComm = new SqlCommand(SQL_SELECT_BODY_BYID, myConn);
SqlParameter parm_LayoutID = new SqlParameter(PARM_ENTITY_LayoutID, SqlDbType.Int, 4);
parm_LayoutID.Value = layoutID;
myComm.Parameters.Add(parm_LayoutID);
SqlDataReader sdr = myComm.ExecuteReader();
while (sdr.Read())
{
EntityLayout entityLayout = new EntityLayout();
entityLayout.LayoutCols = (sdr["LayoutCols"] == DBNull.Value) ? 0 : Convert.ToInt32(sdr["LayoutCols"]);
entityLayout.LayoutID = (sdr["LayoutID"] == DBNull.Value) ? 0:Convert.ToInt32(sdr["LayoutID"]);
entityLayout.LayoutRows = (sdr["LayoutRows"] == DBNull.Value)?0:Convert.ToInt32(sdr["LayoutRows"]);
entityLayout.WidthPercent =(sdr["WidthPercent"] == DBNull.Value)?0:Convert.ToInt32(sdr["WidthPercent"]);
entityLayout.Remark = (sdr["Remark"] == DBNull.Value) ? string.Empty : Convert.ToString(sdr["Remark"]);
entityLayout.Status = (sdr["Status"] == DBNull.Value) ? false : Convert.ToBoolean(dr["Status"]);
return entityLayout;
}
sdr.Close();
sdr.Dispose();
}
catch
{
return null;
}
finally
{
DB.close(myConn);
}
return null;
}
#endregion