E
engahmedbarbary
Guest
Problem
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)
public static void ImportFromExcelToDataBase()
{
Datatable dt = ShowExcelData();
DataTable dtItems = GetSqlItems();
for (int i = 0; i < dt.Rows.Count; i++)
{
//what i write here
// if itemcode exist on excel exist on sql server database
then display similar items exist on database and excel as 001,002 on datagridview
//else
// do insert data
string Insert = "Insert Into Values (" + data + ")";
DataAccess.ExecuteNonQuery(Insert);
}
}
public DataTable ShowExcelData()
{
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);
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
DataTable dt = new DataTable();
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dt.Rows[0]["TABLE_NAME"].ToString();
OleDbCommand com = new OleDbCommand();
com.Connection = con;
com.CommandText = @"SELECT [ItemCode],[ItemsName],[ItemAddress] FROM [" + SheetName + "] ";
OleDbDataAdapter oledbda = new OleDbDataAdapter();
oledbda.SelectCommand = com;
DataSet ds = new DataSet();
oledbda.Fill(ds);
dt = ds.Tables[0];
con.Close();
return dt;
}
dt = ShowExcelData();
public DataTable GetSqlItems()
{
string GetItems = @"select ItemCode,ItemsName,ItemAddress from Items";
DataTable tbGetItems = DataAccess.ExecuteDataTable(GetItems );
return tbGetItems ;
}
dtItems = GetSqlItems();
Continue reading...
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)
public static void ImportFromExcelToDataBase()
{
Datatable dt = ShowExcelData();
DataTable dtItems = GetSqlItems();
for (int i = 0; i < dt.Rows.Count; i++)
{
//what i write here
// if itemcode exist on excel exist on sql server database
then display similar items exist on database and excel as 001,002 on datagridview
//else
// do insert data
string Insert = "Insert Into Values (" + data + ")";
DataAccess.ExecuteNonQuery(Insert);
}
}
public DataTable ShowExcelData()
{
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);
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
DataTable dt = new DataTable();
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dt.Rows[0]["TABLE_NAME"].ToString();
OleDbCommand com = new OleDbCommand();
com.Connection = con;
com.CommandText = @"SELECT [ItemCode],[ItemsName],[ItemAddress] FROM [" + SheetName + "] ";
OleDbDataAdapter oledbda = new OleDbDataAdapter();
oledbda.SelectCommand = com;
DataSet ds = new DataSet();
oledbda.Fill(ds);
dt = ds.Tables[0];
con.Close();
return dt;
}
dt = ShowExcelData();
public DataTable GetSqlItems()
{
string GetItems = @"select ItemCode,ItemsName,ItemAddress from Items";
DataTable tbGetItems = DataAccess.ExecuteDataTable(GetItems );
return tbGetItems ;
}
dtItems = GetSqlItems();
Continue reading...