2017-02-02 7 views
0

MS Access 데이터베이스의 Double 필드에 null 값을 삽입하려고하는데 할 수 없습니다. 나는 아래 코드를 공유하고있다. 누군가 나를 도울 수 있는가?MS Access 데이터베이스 (매개 변수가 아닌)의 이중 필드에 null을 삽입하십시오.

for (int i = 0; i <= RemovedDuplicateDt.Rows.Count - 1; i++) 
       { 


        checkBool = Convert.ToBoolean(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(2).ToString()); 

        if (checkBool) 
        { 
         k = -1; 
        } 
        else 
        { 
         k = 0; 
        } 

        if (string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3).ToString())) 
        { 
         scaling = DBNull.Value; 
        } 
        else 
        { 
         scaling = Convert.ToDouble(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3).ToString()); 
        } 

        cmd.CommandText = "INSERT INTO " + "Data VALUES ('" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(0).ToString() + "' ," 
         + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(1).ToString() + "' ," 
         + "'" + k + "' ," 
         + "'" + Convert.ToDouble(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3)) + "' ," 
         + "'" + Convert.ToDouble(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(4).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(4)) + "' ," 
         + "'" + Convert.ToDouble(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(5).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(5)) + "' ," 
         + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(6).ToString() + "' ," 
         + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(7).ToString() + "' ," 
         + "'" + Convert.ToInt16(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(8).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(8)) + "' ," 
         + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(9).ToString() + "' ," 
         + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(10).ToString() + "' ," 
         + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(11).ToString() + "' ," 
         + "'" + Convert.ToInt32(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(12).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(12)) + "' ," 
         + "'" + Convert.ToInt32(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(13).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(13)) + "' ," 
         + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(14).ToString() + "' ," 
         + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(15).ToString() + "' ," 
         + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(16).ToString() + "' ," 
         + "'" + RemovedDuplicateDt.Rows[i].ItemArray.GetValue(17).ToString() + "' ," 
         + "'" + Convert.ToInt32(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(18).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(18)) + "')"; 

        cmd.ExecuteNonQuery(); 
       } 

이 코드는 작동하지만 데이터베이스에서 "0"을보고 싶지 않습니다. null (공백)을보고 싶습니다.

Convert.ToDouble (null)하려고 할 때 오류가 발생합니다. DBNull.Value에 "DB missmatch criteria"오류가 발생했습니다.

나는이 코드를 사용하여 데이터베이스에 null을 삽입하는 방법이 필요하다.

사용하고 싶지 않습니다.

cmd.Parameters.AddWithValue("Scaling", Double.TryParse(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3).ToString(), out result) ? (object)result : DBNull.Value);, 

그 사용량이 너무 느려 때문에,

그리고 내 데이터베이스 설계가 같다;

enter image description here

나는 당신의 응답을 기다리고, 들으!

+0

'더블 셀'은'0' 또는'null'을 포함하는'RemovedDuplicateDt'에서? –

+0

RemovedDuplicateDt에서 문자열 형식의 모든 셀 및이 셀이 null입니다 – bakin

답변

2

당신이

+ (string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3).ToString()) ? "null" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3)) + " ," 

참고로이 라인

+ "'" + Convert.ToDouble(string.IsNullOrEmpty(RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3).ToString()) ? "0" : RemovedDuplicateDt.Rows[i].ItemArray.GetValue(3)) + "' ," 

을 변경하여 다음과 같이 사용할 수 있습니다 을하지만 명령 매개 변수, 합치 SQL을 사용하는 경우가 더 좋을 것이다 쿼리는 SQL injection이 될 수 있으며 이는 큰 문제입니다. 그 datatable 셀 값을 생각하십시오 '"); DROP TABLE Data ; -- "'

+0

오류 라인 코드를 공유 할 수 있습니까? 코드의 rihgt 부분을 볼 수 없었습니다 –

+0

제가 대답을 edtied,'paranthesis'를 ​​사용하여'isnullorEmpty'를 올바르게 작동 시켰습니다. 그리고 내가 보여준 줄을 바꾸고, 처음에는'' ' "'을 사용하지 마십시오. –

+0

좋아요! 감사합니다. .. – bakin