函数写法
写法一:
public static DataTable GetTable(string safeSqlString)
{
OleDbCommand cmd = new OleDbCommand(safeSqlString, GetConnection());
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public static DataRow GetRow(string safeSqlString)
{
DataRow dr = null;
DataTable dt = GetTable(safeSqlString);
if (dt.Rows.Count > 0)
{
dr = dt.Rows[0];
}
return dr;
}
写法二:
public static DataTable GetTable(string safeSqlString)
{
OleDbCommand cmd = new OleDbCommand(safeSqlString, GetConnection());
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public static DataRow GetRow(string safeSqlString)
{
OleDbCommand cmd = new OleDbCommand(safeSqlString, GetConnection());
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DataRow dr = null;
if (dt.Rows.Count > 0)
{
dr = dt.Rows[0];
}
return dr;
}
针对GetRow(),哪种写法好些 ,效率优先
------解决方案--------------------
应该是没有区别的 但是第一种方法的编程习惯更好 代码复用率更高
------解决方案--------------------
从代码重构的角度,第一种更好一点
但是,没必要static ,也没必要GetRow(),直接返回DataTable已经足够