CommandType.Text代表执行的是SQL语句
CommandType.StoreProcedure代表执行的是存储过程
CommandType代表要执行的类型
使用文件:
string username = "ZBC";
string password= Utils.MD5(password);
SqlParameter[] parms = {
new SqlParameter("@username",username),
new SqlParameter("@password",password)
};
string query= "select TOP 1 [userID],[password] from adminuser where [userLogName]=@username and [password]=@password";
SqlDataReader reader = DataAccess.ExecuteReader(CommandType.Text,query, parms);
string smtpto = " ";
while (reader.Read())
{
smtpto = reader[0].ToString();
}
命名空间中的函数:
/// <summary>
/// 执行指定数据库连接字符串的数据阅读器,指定参数.
/// </summary>
/// <remarks>
/// 示例:
/// SqlDataReader dr = ExecuteReader(CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名或SQL语句</param>
/// <param name="commandParameters">SqlParamter参数数组(new DbParameter("@prodid", 24))</param>
/// <returns>返回包含结果集的SqlDataReader</returns>
public static SqlDataReader ExecuteReader(CommandType commandType, string commandText, SqlParameter[] coll)
{
SqlDataReader dr = null;
try
{
OpenConnection();
comm.Parameters.Clear();
for (int i = 0; i < coll.Length; i++)
{
comm.Parameters.Add(coll );
}
comm.CommandText = commandText;
comm.CommandType = commandType;
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
if (dr != null && !dr.IsClosed)
dr.Close();
CloseConnection();
}
catch
{
}
}
return dr;
}
CommandType.StoreProcedure代表执行的是存储过程
CommandType代表要执行的类型
使用文件:
string username = "ZBC";
string password= Utils.MD5(password);
SqlParameter[] parms = {
new SqlParameter("@username",username),
new SqlParameter("@password",password)
};
string query= "select TOP 1 [userID],[password] from adminuser where [userLogName]=@username and [password]=@password";
SqlDataReader reader = DataAccess.ExecuteReader(CommandType.Text,query, parms);
string smtpto = " ";
while (reader.Read())
{
smtpto = reader[0].ToString();
}
命名空间中的函数:
/// <summary>
/// 执行指定数据库连接字符串的数据阅读器,指定参数.
/// </summary>
/// <remarks>
/// 示例:
/// SqlDataReader dr = ExecuteReader(CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param>
/// <param name="commandText">存储过程名或SQL语句</param>
/// <param name="commandParameters">SqlParamter参数数组(new DbParameter("@prodid", 24))</param>
/// <returns>返回包含结果集的SqlDataReader</returns>
public static SqlDataReader ExecuteReader(CommandType commandType, string commandText, SqlParameter[] coll)
{
SqlDataReader dr = null;
try
{
OpenConnection();
comm.Parameters.Clear();
for (int i = 0; i < coll.Length; i++)
{
comm.Parameters.Add(coll );
}
comm.CommandText = commandText;
comm.CommandType = commandType;
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
if (dr != null && !dr.IsClosed)
dr.Close();
CloseConnection();
}
catch
{
}
}
return dr;
}