2011-09-05 2 views
1

저는 C#에서 새로운 ... 배우려고 애를 씁니다. 데이터 행의 한 열 값을 기반으로 전체 행 데이터를 검색하는 방법을 알고 싶습니다.하나의 열 값을 기반으로 datarow에서 전체 행 데이터를 검색하는 방법

if(MerchantNo!="") 
then retrieve the row 
     and insert data in sql table.. 
: 나

내가 생각하고하는 것은 ..

MerchantNo TerminalNum CardType Date 
110033445 770010449 Amex 5/09/2011 

가 지금은 가맹점 수를 기반으로이 행을 검색 한 후 SQL 테이블에 삽입 할 .. 예를 제공함으로써 명확하게

사실 I 엑셀 file..Hope를 가져 오는 작업입니다 u는 내 질문에

string Mno = ""; 
      foreach (DataRow rowExcel in dtExcel.Rows) 
      { 

       foreach (DataColumn colExcel in dtExcel.Columns) 
       { 
        Mno = rowExcel[colExcel].ToString().Trim(); 
        if (Mno != "") 
        { 
         string Mno1 = Mno.Substring(16, 10); 
         Mno =Mno1.ToString(); 
         //Int32 MerchNo = Convert.ToInt32(Mno); 
        } 
        break; 
       } 
       if(Mno!="")// Mno contains the exact MerchantNo. 
       { 


       } 

       //break; 

      } 
Waqas 말한 유사
+0

곳에서 당신은 행을 검색 할 ? GridView 또는 DataRow 또는 다른 뭔가요? – Mentoliptus

+0

늦은 응답 .. 죄송합니다. 나는 DataRow에서 행을 검색하고 싶습니다 .. 나는 이런 종류의 데이터로 가득 찬 datatable을 가지고 있습니다. – Nazima

답변

0
//check if MerchantNo column is neither null nor empty 
//r is an object of DataRow 
if (Convert.IsDBNull(r["MerchantNo"]) == false && r["MerchantNo"].ToString().Trim() != String.Empty) 
{ 
     //Build an Sql Insert statement based on the data or row 
     string strSql = String.Format("INSERT INTO SOMETABLE(X,Y,Z) VALUES('{0}', '{1}', '{2}');", r["colName1"], r["colName2"], r["colName3"]); 
} 
0

을 이해하지만, 명확한 설명 루프 foreah 추가로 :

int MerchNo = 0; 
foreach (DataRow r in dtExcel.Rows) 
{ 
    if (!String.IsNullOrEmpty(r["MerchantNo"].ToString().Trim()) && 
     Int32.TryParse(r["MerchantNo"].ToString().Trim(), out MerchNo)) 
    { 
     string strSql = String.Format("INSERT INTO SomeTable(MerchantNo, TerminalNum, CardType, Date) VALUES('{0}', '{1}', '{2}', '{3}');", r["MerchantNo"], r["TerminalNum"], r["CardType"], r["Date"]); 

     SqlCommand scInsertCommand = new SqlCommand(strSql); 

     //do transacion and connection assignment, and error handling 

     scInsertCommand.ExecuteNonQuery(); 
    } 
} 
관련 문제