使用SqlHelper获得DataSet
string sqlconn = "server=MS-STUDYZY;database=AdventureWorks;uid=sa;pwd=p@ssw0rd";
DataSet ds = SqlHelper.ExecuteDataset(sqlconn, //数据库连接
CommandType.Text,//命令类型为T-SQL语句
"select * from Person.AddressType");//具体的T-SQL语句内容
在SqlHelper中使用参数
string sqlconn = "server=MS-STUDYZY;database=AdventureWorks;uid=sa;pwd=p@ssw0rd";
DataSet ds = SqlHelper.ExecuteDataset(sqlconn,//数据库连接
"GetDeparmentByGroupName",//要执行的存储过程的名字
"Research and Development");//传入的参数内容
使用SqlHelperParameterCache管理参数
string sqlconn = "server=MS-STUDYZY;database=AdventureWorks;uid=sa;pwd=p@ssw0rd";
string sql = "SELECT * FROM HumanResources.Employee WHERE ManagerID=@mgrID AND Title=@title";//定义SQL查询语句
//以下缓存参数
SqlParameter[] pars = new SqlParameter[2];
pars[0] = new SqlParameter("@mgrID", SqlDbType.Int);
pars[1] = new SqlParameter("@title", SqlDbType.VarChar, 50);
SqlHelperParameterCache.CacheParameterSet(sqlconn, sql, pars);
//以下获得缓存中的参数
SqlParameter[] storedParams = new SqlParameter[2];
storedParams = SqlHelperParameterCache.GetCachedParameterSet(sqlconn, sql);
storedParams[0].Value = 3;
storedParams[1].Value = "Design Engineer";
//以下是将参数传入,执行SQL查询
DataSet ds = SqlHelper.ExecuteDataset(sqlconn, CommandType.Text, sql, storedParams);