请看下面的代码,从代码的可读性、减少冗余考虑,大家有没有优化的方法了。谢谢!
以下两个方法,我想来想去也没找出可以简化的地方了,请大家指教。
private void getRecordCount() { var recordCount = "0"; var cid = Session["cid"].ToString(); var sql = "SELECT COUNT(*)RCOUNT FROM (SELECT DONO FROM D1030 WHERE DOABBR = '" + cid + "' GROUP BY DONO) A"; var dataSet = SqlHelper.dataSet(sql); if (dataSet != null && dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0) { recordCount = dataSet.Tables[0].Rows[0][0].ToString(); } Response.Write(recordCount); }
private void query() { #region ---------html--------- var rowSub = @"<tr tag='{10}'> <td style='text-align:left'><img src='img/pot_line.gif' class='imgh'></td> <td style='text-align:left'>{0}</td> <td style='text-align:left'>{1}</td> <td>{2}</td> <td>{3}</td> <td>{4}</td> <td>{5}</td> <td style='text-align:right; padding-right:4%;'>{6}</td> <td style='text-align:right; padding-right:4%;'>{7}</td> <td style='text-align:right; padding-right:4%;'>{8}</td> <td>{9}</td> </tr>"; var rowHeader = @"<tr style='text-align:left; height:30px;'> <td><img alt='' src='img/pot_minus.gif' class='img' onclick='showOrHideRow(this)' id='{0}' /></td> <td colspan='10' style='font-weight:bold'><input type='radio' name='shiporder' class='rdo_shiporder' id='rdo_shiporder_{0}'/> 出货单号 {0}</td> </tr>"; var rowBlank = @"<tr> <td colspan='11'> <div style='background-image: url(img/bg_line_03.gif); background-repeat: repeat-x; height: 1px;margin-top:8px; margin-bottom:8px;'></div> </td> </tr>"; #endregion var cid = Session["cid"].ToString(); var html = new StringBuilder(); var pageSize = int.Parse(Request["pageSize"]); var pageIndex = int.Parse(Request["pageIndex"]); var startIndex = pageIndex * pageSize; try { var sql = "SELECT DONO FROM D1030 WHERE DOABBR = '" + cid + "' GROUP BY DONO ORDER BY DONO"; var groupOrderTable = SqlHelper.dataSet(sql).Tables[0]; var shipOrders = DataTableToList<D1030>(groupOrderTable).Skip(startIndex).Take(pageSize); foreach (var model in shipOrders) { html.Append(string.Format(rowHeader, model.DONO)); var d1030List = bllD1030.GetModelList(" DONO='" + model.DONO + "'"); foreach (var d1030 in d1030List) { html.Append(string.Format(rowSub, d1030.DOINDT.ToTime("yy/MM HH:mm"), d1030.DOVR01, d1030.DOSLIP, "", d1030.DOITEM, d1030.DODESC, d1030.DOSOQS, d1030.DOUPRC, d1030.DOSOQS * d1030.DOUPRC, bllMcuList.SelectMCUCN(d1030.DOMCU), model.DONO)); } html.Append(rowBlank); } Response.Write(html); } catch (Exception ex) { LogWriter.Write(LogType.Error, "[shiporder.query]页面加载错误:\n" + ex.Message); } }
```执行这种查询语句,真心没必要用DataSet啊,直接执行查询就行了
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = cmdStr;
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
conn.Open();
return cmd.ExecuteScalar();
}
}
而且你是有多喜欢DataSet啊``
下面那个东西也是,直接用DataReader查询出来一个循环就可以了写的那么复杂```
呵呵,说的对,谢谢!
private void getRecordCount() { var dataSet = SqlHelper.dataSet("SELECT COUNT(*)RCOUNT FROM (SELECT DONO FROM D1030 WHERE DOABBR = '" + Session["cid"].ToString() + "' GROUP BY DONO) A"); if (dataSet != null && dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0) { Response.Write(dataSet.Tables[0].Rows[0][0].ToString()); return; } Response.Write("0"); }
private void getRecordCount(string recordCount = "0") { var sql = string.Format("SELECT COUNT(*)RCOUNT FROM (SELECT DONO FROM D1030 WHERE DOABBR = '" + cid + "' GROUP BY DONO) A", Session["cid"].ToString()); var dataSet = SqlHelper.dataSet(sql); if (dataSet != null && dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0) { recordCount = dataSet.Tables[0].Rows[0][0].ToString(); } Response.Write(recordCount); }
private void getRecordCount()//方法命名不太对,参考:int GetRecordCount { var recordCount = "0"; var cid = Session["cid"].ToString();//未判断空,有风险 var sql = "SELECT COUNT(*)RCOUNT FROM (SELECT DONO FROM D1030 WHERE DOABBR = '" + cid + "' GROUP BY DONO) A";//有注入风险,看你的cid限制。 var dataSet = SqlHelper.dataSet(sql);//没有对应Ado.Net ExecuteScalar()的方法吗? if (dataSet != null && dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0) { recordCount = dataSet.Tables[0].Rows[0][0].ToString(); } Response.Write(recordCount);//可以定义返回值为int,直接return dbHelper.ExecuteScalar<int>("sql语句"); }
对于你下面的拼字符串,建议采用模板的思路,用Replace去做。