当前位置: 代码迷 >> C# >> 取最大值,该怎么解决
  详细解决方案

取最大值,该怎么解决

热度:105   发布时间:2016-05-05 03:36:48.0
取最大值
      public static int GetMaxID(string tableName, string field)
        {
            string s = "select Max(@field) from @tablename";
            SqlParameter[] para = { new SqlParameter("@field", field), new SqlParameter("@tablename", tableName) };
            object obj = SqlHelper.ExecuteScalar(SqlEasy.connString, CommandType.Text, s, para);
            int i = Convert.ToInt32(obj == DBNull.Value ? "0" : obj);
            return i;
        }




輸入
tableName=Table
field=a

但在執行SQL時 

SECLECT Max('a') from 'Table';

有辦法把單引號去除,或者傳入時給雙引號
------解决思路----------------------
string s = string.Format( "select Max({0}) from {1}",field,tableName);
这样就行了,哪用得着参数化
  相关解决方案