TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
ahmed elbarbary
NA
1.6k
278.3k
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)
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();
Reply
Answers (
1
)
How to Connect my peripheral device with Silverlight
downloadin books