2012-05-20 2 views
3

현재 웹 기반 응용 프로그램의 트랜잭션을 완료하려고합니다. 문자열에서 매개 변수 값을 Int32로 변환하지 못했습니다.

다음은 기능의 사본 인 INT32

에 문자열에서 매개 변수 값을 변환하지 못했습니다.

public static void completeTransaction(string storeCode, string employeeId, DateTime Date, string itemListNoId) 
{ 
    using (SqlConnection conn = new SqlConnection("Data Source = ; Initial Catalog =Business ; Integrated Security = true;")) 
    { 
     using (SqlCommand command = new SqlCommand("dbo.completeTransaction", conn)) 
     { 
      command.CommandType = CommandType.StoredProcedure; 
      command.Parameters.Add("@storeCode", SqlDbType.Int).Value = storeCode; 
      command.Parameters.Add("@employeeId", SqlDbType.Int).Value = employeeId; 
      command.Parameters.Add("@Date", SqlDbType.DateTime).Value = Date; 
      command.Parameters.Add("@itemListNoId", SqlDbType.Int).Value = itemListNoId; 
      conn.Open(); 

      command.ExecuteNonQuery(); 
      conn.Close(); 
     } 
    } 
} 

내 SQL Server 테이블은 어떤 도움을 주시면 감사하겠습니다 다음 표와 유형

storeCode INT 
employee INT 
Date DATETIME 
itemListNoId INT 

가 포함되어 있습니다. 나는 storeCode 문자열 상상

storeCode 
employeeId 
itemListNoId 

: 입력의

+0

문자열을 Int.32 데이터 형식으로 구문 분석해야합니다. –

+0

제목 앞에 "C# SQL Server 2008"등의 접두어를 붙이지 마십시오. 그것이 바로 태그가있는 것입니다. –

답변

2

나는이 문제가 첫 번째 매개 변수 (storeCode)에 있음을 알고 있습니다. int 매개 변수로 문자열을 보내려고합니다.

그 라인은 다음과 같이 읽어야

command.Parameters.Add("@storeCode", SqlDbType.Int).Value = Convert.ToInt32(storeCode); 

한 번 더 의심스러운 일이있다 : 매개 변수의 이름이 VARCHAR 컬럼을 의미 한 상점입니다. storeCode로 전달하려는 가치는 무엇입니까? 그것이 int 인 것이 확실합니까?

+0

그 ID 대신 직원 이름을 참조했기 때문에이 이름이 나타납니다. – MasterP

0

하나의 선언을 확인하는 문자열입니다. 당신은 Int로 분석하여이 문제를 해결할 수 있습니다 storeCode 더 적 parasable Int 경우

command.Parameters.Add("@storeCode", SqlDbType.Int).Value = int.Parse(storeCode); 

그러나이 문제가 발생하지 않습니다.

+1

그 ID 대신 직원 이름을 참조했기 때문에이 이름이 나타납니다. – MasterP

+1

주제가 아닐지 몰라도, int 값을 전달하기 위해 매개 변수를 사용하는 경우 매개 변수가 문자열이 아니어도됩니다. –

1

메서드의 매개 변수 유형을 변경하는 것이 좋습니다.

public static void completeTransaction(int storeCode, int employeeId, DateTime Date, string itemListNoId) 

및 방법에 값을 전달하기 전에 문자열을 변환한다.

관련 문제