日期:2014-05-18 浏览次数:20495 次
// //这个应该是没有问题的吧 // public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); SqlConnection conn = new SqlConnection(connectionString); // we use a try/catch here because if the method throws an exception we want to // close the connection throw code, because no datareader will exist, hence the // commandBehaviour.CloseConnection will not work try { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return rdr; } catch { conn.Close(); throw; } }
/// <summary> /// Prepare a command for execution(数据访问层方法) /// </summary> /// <param name="cmd">SqlCommand object(SQL命令)</param> /// <param name="conn">SqlConnection object(连接对象)</param> /// <param name="trans">SqlTransaction object(事务)</param> /// <param name="cmdType">Cmd type e.g. stored procedure or text(键入cmd存储过程或文本)</param> /// <param name="cmdText">Command text, e.g. Select * from Causewares(SQL语句)</param> /// <param name="cmdParms">SqlParameters to use in the command(SQL语句参数)</param> private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) { //如果数据库没有打开的话,就打开 if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn;//SQL命令中的链接对象就是传过来的连接对象(数据服务器,账号,密码) cmd.CommandText = cmdText;//SQL命令中的SQL语句就是传过来的SQL语句 //如果没有事务的话 if (trans != null) cmd.Transaction = trans;//SQL命令的事务就等于传过来的事务 cmd.CommandType = cmdType;//SQL命令的储存过程就等于传过来的储存过程 //如果SQL语句有参数的话 //遍历所有参数添加到SQL语句对应的参数地址 if (cmdParms != null) { foreach (SqlParameter parm in cmdParms) cmd.Parameters.Add(parm); } }
<connectionStrings> <add name="SQLConnString4" connectionString="server=192.168.0.0;uid=sa;pwd=pwd;database=neware200907;" providerName="System.Data.SqlClient"/ > //难道要在这里加参数,,, <add name="SQLProfileConnString" connectionString=""/> </connectionStrings>
using (IDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransactionSAP, CommandType.Text, strSql)) { List<OSLPEntity> list = new List<OSLPEntity>(); while (dr.Read()) { list.Add(newSQLOSLPEntity(dr)); } retu