2014-03-07 35 views
0

我創建了一個InsertTable方法到AigsAuthorityLayer表,但有一些事件依賴於權限範圍。如果scope is ==系統,這將是等於1,當scope is ==到共享組ID值是GroupId = aigsDB.GetDefGroup(Convert.ToString(context.Session["GroupID"])).Rows[0]["lSharingGid"].ToString();如果是scope == to group,這將是C#插入到帶有case語句的sql表中

authority = aigsDB.GetDefGroupUser(Convert.ToString(context.Session["UserId"].ToString())); 

對於editFlg,如果是等於一,它將返回true,如果沒有,假

CommonMethod.CheckLogin(); 
     string sqlWord = CommonDB.CreateSqlString("AigsAuthorityLayer", "*", "", "", ""); 
     context = HttpContext.Current; 

     CommonDB comDB = new CommonDB(connection);  

     try 

     { 
      string sql = "INSERT INTO AigsAuthorityLayer (lGid, LayerNo, lAuthority, IEditFlg) VALUES('{0}',{1}, {2},{3}')"; 
      sql = sql.Replace("{0}", GroupId); 
      sql = sql.Replace("{1}", layerName.ToString()); 
      sql = sql.Replace("{2}", authority.ToString()); 
      sql = sql.Replace("{3}", editFlg.ToString()); 
      comDB.Open(); 
      comDB.AddNewRecord(sql);    

      { 

      comDB.Close(); 
      comDB = null; 
     } 
      GroupId = "-1"; 
      authority = "-1"; 

      if (scope == "system") 
      { 
       return GroupId; 
      } 
      else if (scope == "sharing") 
      { 
       GroupId = aigsDB.GetDefGroup(Convert.ToString(context.Session["GroupID"])).Rows[0]["lSharingGid"].ToString(); 
      } 
      else if (scope == "group") 
      { 
       authority = aigsDB.GetDefGroupUser(Convert.ToString(context.Session["UserId"].ToString())); 
      } 
      return true; 
     } 
      catch (Exception e) 
     { 
      throw logger.Error("InsertAigsAuthorityLayer", e); 
     } 
     finally 
     { 

      if (editFlg == "1") 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
      comDB.Close(); 
      comDB = null; 
    } 

}

我的問題是,我不知道我這樣做是正確:(任何人都知道如果這個代碼是否正確?謝謝

+2

爲什麼不使用[參數化查詢](http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html)而不是字符串替換?順便提一下,你的問題似乎有點不清楚。 –

+0

@SonerGönül有一個重要的觀點。你完全開放注入,並應改爲參數化查詢 –

+0

我不知道該怎麼做對不起@SonerGönül這只是第一次生病與C#工作。但是我的代碼會工作嗎? – Lyn

回答

0

我沒有看到爲什麼它在技術上可能無法正常工作,但您應該始終測試自己的代碼。但是,可以進行一些改進。

首先,您應該使用參數化查詢來防止SQL注入。其次,考慮c#使用語句來幫助處理完成時自動處理對象。例如:

using(SqlConnection conn = new SqlConnection("<connection string here>") 
{ 
    string cmdString = "INSERT INTO AigsAuthorityLayer (lGid, LayerNo, lAuthority, IEditFlg) VALUES(@lGid, @layerNo, @lAuthority, @lEditFlag)"; 
    using(SqlCommand cmd = new SqlCommand(cmdString, conn) 
    { 

     cmd.CommandType = System.Data.CommandType.Text; 

     SqlParameterCollection p = cmd.Parameters; 

     // Build a parameter for each of @lGid, @layerNo, @lAuthority, @lEditFlag 
     SqlParameter p1 = p.AddWithValue("@lGid", GroupId); 
     p1.SqlDbType = System.Data.SqlDbType.Int; // Int assumed here 

     // Repeat for other parameters 
     ... 

     // Run query as needed 
     cmd.ExecuteNonQuery(); // Or appropriate method 

    } 
} 

通過利用using語句,無論成功或錯誤SqlConnectionSqlCommand對象將被自動設置完成後,並釋放所有資源。其次,通過使用SqlParameters,可以防止Sql注入。在你目前的方法中,有人可以輸入(例如)「;」然後在其中一個值中輸入「DELETE FROM」或「DROP TABLE」命令,以引發各種問題。

最後,如果你想捕捉一個錯誤,可以用try ... catch來包裝上面的內容,並用/關於它做些事情。

希望有所幫助。

+0

我建議你將參數行更新爲'p.AddWithValue(「@ lGid」,GroupId)',這樣它在讀取時就不能與列名混淆。 –

+0

垃圾,簡單的錯字。謝謝@ThorstenDittmar –

+0

謝謝你們:)我現在會更新我的代碼:)希望一切正常@JonBellamy – Lyn