public List<FMDS_Water_ObserInfo> GetModelListBySql(QueryBuilder queryBuilder) { StringBuilder sbSql = new StringBuilder(); sbSql.Append(@"EXEC('select ID,Name,Location,Longitude,Latitude,IsDelete,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate,ThreeCode,Locations,OrganiseUnitID,Pic,Remarks from [FMDS_Water_Obser] '+ @Condition +' '+@SortBy)"); // 如果没有排序,就指定默认的排序 if (queryBuilder.OrderCount == 0) { queryBuilder.AddOrderASC(FMDS_Water_ObserInfo.FMDS_WATER_OBSER_TABLE, FMDS_Water_ObserInfo.ID_FIELD); } SqlParameter[] parameters = SqlDbCommandBuilder.BuildQueryCommand(queryBuilder); List<FMDS_Water_ObserInfo> models = new List<FMDS_Water_ObserInfo>(); //执行查询 using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.LocalWaterConnectionString, CommandType.Text, sbSql.ToString(), parameters)) { while (rdr.Read()) { FMDS_Water_ObserInfo model = new FMDS_Water_ObserInfo(); model.ID = (new Guid(rdr[FMDS_Water_ObserInfo.ID_FIELD].ToString())); model.Name = Convert.IsDBNull(rdr[FMDS_Water_ObserInfo.NAME_FIELD]) ? string.Empty : rdr[FMDS_Water_ObserInfo.NAME_FIELD].ToString(); model.Location = Convert.IsDBNull(rdr[FMDS_Water_ObserInfo.LOCATION_FIELD]) ? string.Empty : rdr[FMDS_Water_ObserInfo.LOCATION_FIELD].ToString(); model.Longitude = Convert.IsDBNull(rdr[FMDS_Water_ObserInfo.LONGITUDE_FIELD]) ? int.MinValue : Convert.ToDecimal(rdr[FMDS_Water_ObserInfo.LONGITUDE_FIELD]); model.Latitude = Convert.IsDBNull(rdr[FMDS_Water_ObserInfo.LATITUDE_FIELD]) ? int.MinValue : Convert.ToDecimal(rdr[FMDS_Water_ObserInfo.LATITUDE_FIELD]); model.IsDelete = Convert.IsDBNull(rdr[FMDS_Water_ObserInfo.ISDELETE_FIELD]) ? int.MinValue : Convert.ToInt32(rdr[FMDS_Water_ObserInfo.ISDELETE_FIELD]); model.CreatedBy = Convert.IsDBNull(rdr[FMDS_Water_ObserInfo.CREATEDBY_FIELD]) ? string.Empty : rdr[FMDS_Water_ObserInfo.CREATEDBY_FIELD].ToString(); model.CreatedDate = Convert.IsDBNull(rdr[FMDS_Water_ObserInfo.CREATEDDATE_FIELD]) ? Convert.ToDateTime("1900-1-1") : Convert.ToDateTime(rdr[FMDS_Water_ObserInfo.CREATEDDATE_FIELD]); model.ModifiedBy = Convert.IsDBNull(rdr[FMDS_Water_ObserInfo.MODIFIEDBY_FIELD]) ? string.Empty : rdr[FMDS_Water_ObserInfo.MODIFIEDBY_FIELD].ToString(); model.ModifiedDate = Convert.IsDBNull(rdr[FMDS_Water_ObserInfo.MODIFIEDDATE_FIELD]) ? Convert.ToDateTime("1900-1-1") : Convert.ToDateTime(rdr[FMDS_Water_ObserInfo.MODIFIEDDATE_FIELD]); model.ThreeCode = Convert.IsDBNull(rdr[FMDS_Water_ObserInfo.THREECODE_FIELD]) ? string.Empty : rdr[FMDS_Water_ObserInfo.THREECODE_FIELD].ToString(); model.Locations = Convert.IsDBNull(rdr[FMDS_Water_ObserInfo.LOCATIONS_FIELD]) ? string.Empty : rdr[FMDS_Water_ObserInfo.LOCATIONS_FIELD].ToString(); model.OrganiseUnitID = Convert.IsDBNull(rdr[FMDS_Water_ObserInfo.ORGANISEUNITID_FIELD]) ? int.MinValue : Convert.ToInt32(rdr[FMDS_Water_ObserInfo.ORGANISEUNITID_FIELD]); model.Pic = Convert.IsDBNull(rdr[FMDS_Water_ObserInfo.PIC_FIELD]) ? (new Guid()) : (new Guid(rdr[FMDS_Water_ObserInfo.PIC_FIELD].ToString())); model.Remarks = Convert.IsDBNull(rdr[FMDS_Water_ObserInfo.REMARKS_FIELD]) ? string.Empty : rdr[FMDS_Water_ObserInfo.REMARKS_FIELD].ToString(); models.Add(model); } } return models; }
请问exec的作用是什么?
上面的方法给我讲解一下
学编程的要会百度,百度一下就知道什么作用了。
一般使用exec 有两个作用:1.执行存储过程,2.执行一个动态批次;你程序中的就是执行一个动态批次,里面放的就是一个SQL字符串。
这里感觉用exec就是脱了裤子放屁,直接用就可以了。为什么要加个exec
exec一般用来执行过程,还有sql语句
如
exec up_x;
exec(@sql);
最常用的就是这两种。