//Default.aspx.cs
private void bindnew()
{
linterface lif = new SqlSelect();
SqlDataReader sda = lif.SelectData();
GridView1.DataSource = sda;
GridView1.DataBind();
sda.Close();
}
//SqlSelect.cs
public class SqlSelect : linterface
{
private static readonly string SlectShitu = "select * from shitu where c_num>60 and z_name='dotnet' order by s_id";
public SqlDataReader SelectData()
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
SqlCommand cmd = new SqlCommand(SlectShitu, con);
SqlDataReader sda = null;
try
{
con.Open();
sda = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(SqlException ex)
{
throw new Exception(ex.Message,ex);
}
return sda;
}
}
//linterface.cs
public interface linterface
{
SqlDataReader SelectData();
}
------解决方案--------------------------------------------------------
也许写程序的人认为他可能要实现多个数据库访问类,比如 SqlSelect、AccessSelect…… 然后自由替换。
不过就你的代码看,更多可能是,这是一个没有必要且蹩脚的设计。
------解决方案--------------------------------------------------------