#region //执行SQL返回首行首列的值
/// <summary>
/// 执行SQL返回首行首列的值,不存在返回""
/// </summary>
public string GetSingleVal(string sql_)
{
string RetStr = null;
try
{
OpenDb();
SqlCommand cm = new SqlCommand(sql_, conn);
RetStr = cm.ExecuteScalar() == null ? "" : cm.ExecuteScalar().ToString();
cm.Dispose();
cm = null;
CloseDb();
}
catch (Exception e)
{
throw new Exception(e.ToString() + ", " + sql_);
}
return RetStr;
}
#endregion
#region //判断是否存在对应的数据
/// <summary>
/// 根据SQL判断是否存在对应的数据
/// </summary>
public bool YNExistData(string sql_)
{
bool ynExist = false;
try
{
DataTable dt = GetDataTable(sql_);
if (dt != null && dt.Rows.Count > 0)
ynExist = true;
}
catch (Exception e)
{
ynExist = false;
throw new Exception(e.ToString() + ", " + sql_);
}
return ynExist;
}
#endregion
#region //执行SQL返回DataSet数据集
/// <summary>
/// 执行SQL返回DataSet数据集
/// </summary>
/// <param name="sql_"></param>
/// <returns></returns>
public DataSet GetDataSet(string sql_)
{
if (sql_ == "")
return null;
DataSet ds = null;
try
{
OpenDb();
SqlDataAdapter myad = new SqlDataAdapter(sql_, conn);
ds = new DataSet();
myad.Fill(ds);//用数据适配器填充数据集
myad.Dispose();
myad = null;
CloseDb();
if (ds.Tables.Count <= 0)
return null;
}
catch (Exception e)
{
throw new Exception(e.ToString() + " " + sql_);
}
return ds;
}
#endregion