我覺得這是基類中必需的三個方法:
1,得到DataSet的方法
public DataSet ExecuteForDS(string mySql,string myTable)
{
SqlDataAdapter myDa = new SqlDataAdapter(mySql,myConn);
DataSet ds = new DataSet();
try
{
myConn.Open();
myDa.Fill(ds,myTable);
return ds;
}
catch
{
return new DataSet();
}
finally
{
myDa.Dispose();
myConn.Close();
}
}
2,執行插入,更新,刪除的SQL語句的方法
public int ExecuteSql(string mySql)
{
SqlCommand myCmd = new SqlCommand(mySql,myConn);
try
{
myConn.Open();
myCmd.ExecuteNonQuery();
return 1;
}
catch
{
return -99;
}
finally
{
myCmd.Dispose();
myConn.Close();
}
}
3,以此判段是否在數據庫中存在
public int ExecuteCount(string sql)
{
SqlCommand myCmd = new SqlCommand(sql,myConn);
try
{
myConn.Open();
return (int)myCmd.ExecuteScalar();
}
catch
{
return -99;
}
finally
{
myCmd.Dispose();
myConn.Close();
}
}
阅读(503) | 评论(0) | 转发(0) |