ahmed elbarbary

ahmed elbarbary

  • 954
  • 1.5k
  • 117.1k

what i write inside loop to display similar item code on dat

Sep 25 2018 7:43 PM
Problem
 
what i write inside loop to display similar item code on datagridview and insert different itemcode on database sql server
 
SQL Server Database(2014) Items Table
 
ItemCode(pk) ItemName
001                mouse
002                keyboard
003                Headphone
 
On File Excel sheet 2010
 
ItemCode ItemName
001           mouse
002           keyboard
004           screen
005           Ram
 
Actually i need when import excel file insert different items code that not exist on sql server database and Exist Items On Database and Found on Excel not insert but display on datagridview.
 
according to my case insert itemcodes 004,005 on table Items.
 
and show 001,002 in grid view as exist items .
 
my function as below
 
my code (Inside Loop)
  1. public static void ImportFromExcelToDataBase()  
  2.     {  
  3.        Datatable dt = ShowExcelData();  
  4.        DataTable dtItems = GetSqlItems();  
  5.          
  6.             for (int i = 0; i < dt.Rows.Count; i++)  
  7.             {  
  8.                 //what i write here   
  9.                 // if itemcode exist on excel exist on sql server database  
  10.                 then display similar items exist on database and excel as 001,002 on datagridview  
  11.                 //else   
  12.                  // do insert data  
  13.                 string Insert = "Insert Into Values (" + data + ")";  
  14.                 DataAccess.ExecuteNonQuery(Insert);  
  15.             }  
  16.                  
  17.     }  
  18.  public DataTable ShowExcelData()  
  19.         {  
  20.             string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", txtpath.Text);  
  21.   
  22.             OleDbConnection con = new OleDbConnection(connectionString);  
  23.   
  24.   
  25.             con.Open();  
  26.             DataTable dt = new DataTable();  
  27.   
  28.             dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);  
  29.   
  30.             string SheetName = dt.Rows[0]["TABLE_NAME"].ToString();  
  31.   
  32.   
  33.             OleDbCommand com = new OleDbCommand();  
  34.             com.Connection = con;  
  35.              
  36.             com.CommandText = @"SELECT  [ItemCode],[ItemsName],[ItemAddress] FROM  [" + SheetName + "] ";  
  37.             OleDbDataAdapter oledbda = new OleDbDataAdapter();  
  38.             oledbda.SelectCommand = com;  
  39.             DataSet ds = new DataSet();  
  40.             oledbda.Fill(ds);  
  41.             dt = ds.Tables[0];  
  42.             con.Close();  
  43.             return dt;  
  44.   
  45.   
  46.         }  
  47. dt = ShowExcelData();  
  48.   
  49.  public DataTable GetSqlItems()  
  50.         {  
  51.             string GetItems = @"select ItemCode,ItemsName,ItemAddress from Items";  
  52.   
  53.   
  54.            DataTable tbGetItems = DataAccess.ExecuteDataTable(GetItems );  
  55.             return tbGetItems ;  
  56.         }  
  57. dtItems = GetSqlItems();  

Answers (1)