2014-09-04 2 views
1

쿠키의 값에 따라 테이블에 세부 정보를 삽입하려고합니다. 여기에 코드where 절을 사용하여 문을 삽입하십시오. C#

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString); 
     conn.Open(); 
     string insertQuery = "insert into Details (Employees, Performance, TotalPerformance, Attitude, TotalAttitude) values(@employees, @performance ,@totalPerformance, attitude, totalAttitude)"; 
     SqlCommand com = new SqlCommand(insertQuery, conn); 
     com.Parameters.AddWithValue("@employees", Request.QueryString["employees"]); 
     com.Parameters.AddWithValue("@performance", totalPer.ToString()); 
     com.Parameters.AddWithValue("@totalPerformance", totalPercent.ToString()); 
     com.Parameters.AddWithValue("@attitude", totalAtt.ToString()); 
     com.Parameters.AddWithValue("@totalattitude", totalPercent.ToString()); 



     com.ExecuteNonQuery(); 

이 잘 작동하지만 열 "이름"의 값에 말했다 경우 문자열 삽입 쿼리 라인에서 내가 특정 행에 값을 삽입하는 where 절에 추가 할 수있다 행은 쿠키와 일치합니다. 적절한 구문을 모르겠습니다. 도움을 주셔서 감사합니다.

+0

도움 주셔서 감사합니다 – user3542214

답변

4

기존 레코드를 수정하려고하므로 INSERT 문이 아닌 UPDATE 문이 필요합니다. 귀하의 질문에, 한 가지, enclose your Command and Connection object inusing 문에 매개 변수를 사용하는

using (
    SqlConnection conn = 
     new SqlConnection(
      ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString)) 
{ 
    conn.Open(); 
    string updateQuery = 
     @"UPDATE Details 
     SET Employees = @employeee, 
     Performance = @performance , 
     TotalPerformance = @totalPerformance, 
     Attitude = @attitude, 
     TotalAttitude = @totalattitude 
     WHERE yourField = @yourConditionValue"; 

    using (SqlCommand com = new SqlCommand(updateQuery, conn)) 
    { 
     com.Parameters.AddWithValue("@employees", Request.QueryString["employees"]); 
     com.Parameters.AddWithValue("@performance", totalPer.ToString()); 
     com.Parameters.AddWithValue("@totalPerformance", totalPercent.ToString()); 
     com.Parameters.AddWithValue("@attitude", totalAtt.ToString()); 
     com.Parameters.AddWithValue("@totalattitude", totalPercent.ToString()); 
     com.Parameters.AddWithValue("@yourConditionValue", yourValue); 
     com.ExecuteNonQuery(); 
    } 
} 

+1

.

+0

도움 주셔서 감사합니다 – user3542214

+1

그 덕분에 완벽하게 도와 줬어, 고마워. – user3542214

0

당신이 찾고있는 것을 UPSERT이라고도합니다. 이미 존재하는 경우 UPDATE 레코드이거나 그렇지 않은 경우 INSERT입니다.

SQL Server 2008 이상에는이 기능에 대해 MERGE이라는 키워드가 있습니다. 그렇지 않으면, 올바른 논리를 사용하여 IF 문을 사용하여 저장 프로 시저를 작성합니다.

관련 문제