首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请问大侠,能否优化这段代码,小弟我感觉好冗余

2012-10-21 
请教大侠,能否优化这段代码,我感觉好冗余啊是c#里执行存储过程的一个方法,由于参数比较多,特别是后面那一

请教大侠,能否优化这段代码,我感觉好冗余啊
是c#里执行存储过程的一个方法,由于参数比较多,特别是后面那一段
parameters[x].Direction =ParameterDirection.Output;感觉很冗余,但必须要写,最好能写在 new SqlParameter()实例化里,但这个重载虽然最后参数可以写Direction ,但是那个重载必须有7个参数,很多是我不需要的,想请教大侠一般是怎么写的,非常感谢。

 

C# code
public static string getMyAndRoom(int id,string guidkey,int jRoomId)        {            string p_name = "getMyAndRoom";            SqlParameter[] parameters = { new SqlParameter("@id", id),                                                          new SqlParameter("@guidkey",guidkey),                                                          new SqlParameter("@jRoomId",jRoomId),                                                          new SqlParameter("@userName",SqlDbType.NVarChar,30),                                                          new SqlParameter("@qqImg",SqlDbType.VarChar,600),                                                          new SqlParameter("@roomName",SqlDbType.NVarChar,30),                                                          new SqlParameter("@bgImg",SqlDbType.VarChar,360),                                                          new SqlParameter("@sex",SqlDbType.Bit),                                                          new SqlParameter("@jRoomName",SqlDbType.NVarChar,30),                                                          new SqlParameter("@jRoomBgImg",SqlDbType.VarChar,360),                                                          new SqlParameter("@jRoomqqImg",SqlDbType.VarChar,200)};            parameters[3].Direction = ParameterDirection.Output;            parameters[4].Direction = ParameterDirection.Output;            parameters[5].Direction = ParameterDirection.Output;            parameters[6].Direction = ParameterDirection.Output;            parameters[7].Direction = ParameterDirection.Output;            parameters[8].Direction = ParameterDirection.Output;            parameters[9].Direction = ParameterDirection.Output;            parameters[10].Direction = ParameterDirection.Output;            DataSet ds = DbHelperSQL.RunProcedure(p_name, parameters, "getMyAndRoom");            if (id != 0)


[解决办法]
SqlParameter 构造函数中的第六个重载形式……
C# code
public SqlParameter(    string parameterName,    SqlDbType dbType,    int size,    ParameterDirection direction,    bool isNullable,    byte precision,    byte scale,    string sourceColumn,    DataRowVersion sourceVersion,    Object value) 

热点排行