2013-07-04 2 views
-4

여기 새내기. 조언이 필요합니다. 데이터베이스에 값을 삽입하고 코드에 오류를 넣고 싶습니다. 그 다음은 데이터베이스데이터베이스에 값을 삽입하는 방법

내 코드 가 저장이 작업을 수행 : 수행대로해서는 안하려고 노력하는 모든

  string Constr = @"Data Source=(local);Integrated Security=True;Database=DDLYBank"; 
      SqlConnection Conn = new SqlConnection(Constr); 
      string MainAccInfo = "Select AccountNo, HashValue from MainAcc Where HashValue = '" + ccinfo + "'"; 
      SqlCommand cmd1 = new SqlCommand(MainAccInfo, Conn); 
      SqlDataReader read1 = cmd1.ExecuteReader(); 


      using (SqlCommand command = Conn.CreateCommand()) 
      { 
       command.CommandText = "Insert INTO Record (AccountNo, HashValue, Debit, Balance, Date_Time, FingerPrint) Values (@AccountNo, @HashValue, @Debit, @Balance, @Date_Time, @FingerPrint)"; 

       command.Parameters.AddWithValue("@AccountNo", read1["AccountNo"]); 
       command.Parameters.AddWithValue("@HashValue", read1["HashValue"]); 
       command.Parameters.AddWithValue("@Debit", rDebit); 
       command.Parameters.AddWithValue("@Balance", aBalance); 
       command.Parameters.AddWithValue("@Date_Time", rDate); 
       command.Parameters.AddWithValue("@FingerPrint", rFingerPrint); 

       Conn.Open(); 
       command.ExecuteNonQuery(); 
      } 

     } 
     catch (SqlException ex) 
     { 
      return; 
     } 
+0

당신은 SQL 명령에 매개 변수 이름 앞에 @ 기호가 누락되었습니다. –

+0

이 말씀이신가요? "@AccountNo" 나는 그것을 시도했다. 아직도 내가 그것을했을 다른 캔트. –

+0

예외가 있습니까? – Jodrell

답변

1

처음에 값을 삽입하는 데 실패하는 이유는 문제를 파악하지 못할 SQL Server의 프로 시저.

CREATE PROC CreateNewFromHashValue 
    @HashValue NVARCHAR(128), 
    @Debit INT, 
    @Balance INT, 
    @Date DATETIME, 
    @FingerPrint NVARCHAR(128) 
AS 

INSERT INTO Record (AccountNo, HashValue, Debit, Balance, Date_Time, FingerPrint) 
SELECT AccountNo, HashValue,@Debit,@Balance,@Date,@FingerPrint 
    FROM MainAcc 
    WHERE HashValue = @HashValue 

SELECT TOP 1 * 
    FROM Record 
    ORDER BY DESC 

GO 

는 우리는 .NET에서 호출 :

 // create and open a connection object 
     SqlConnection conn = new SqlConnection("Data Source=(local);Integrated Security=True;Database=DDLYBank"); 
     try { 
     conn.Open(); 

     // 1. create a command object identifying 
     // the stored procedure 
     SqlCommand cmd = new SqlCommand("CreateNewFromHashValue", conn); 

     // 2. set the command object so it knows 
     // to execute a stored procedure 
     cmd.CommandType = CommandType.StoredProcedure; 

     // 3. add parameter to command, which 
     // will be passed to the stored procedure 
     cmd.Parameters.Add(new SqlParameter("@HashValue","YOUR HashValue")); 
     cmd.Parameters.Add(new SqlParameter("@Debit",1)); // Your Debit,INT? 
     cmd.Parameters.Add(new SqlParameter("@Balance",1)); // Your Balance ,INT? 
     cmd.Parameters.Add(new SqlParameter("@Date",DateTime.Now)); // datetime :) 
     cmd.Parameters.Add(new SqlParameter("@FingerPrint","FingerPrint")); // FingerPrint 

     using (SqlCommand cmd = conn.CreateCommand()) 
     { 
      cmd.ExecuteNonQuery(); 
     } 

    } catch (Exception ex) { 
     // NOT ONLY RETURN HER !!! 
     Console.WriteLine(ex.Message.ToString()); 
    } 


    //Remember to to close conn then you are done. 
    conn.Close(); 
관련 문제