[字体="]问题
[/ FONT]
[h = 1]我在循环内写了些什么,以便在datagridview上显示类似的项目代码,并在数据库sql服务器上插入不同的项目代码[/ h][字体="]SQL Server数据库(2014)项表[/ FONT]
[字体="]ItemCode(pk)项目名称 [/ FONT]
[字体="]001鼠标[/ FONT]
[字体="]002键盘[/ FONT]
[字体="]003耳机[/ FONT]
[字体="]在Excel 2010表格上 [/ FONT]
[字体="]ItemCode ItemName [/ FONT]
[字体="]001鼠标[/ FONT]
[字体="]002键盘[/ FONT]
[字体="]004画面[/ FONT]
[字体="]005拉姆[/ FONT]
[字体="]其实我需要在导入Excel文件时插入不存在的不同项目代码[/ FONT]
[字体="] 在sql server数据库上和在Excel上找到的“数据库中的现有项目”不会插入,而是在datagridview上显示。[/ FONT]
[字体="]根据我的情况,在表格Items上插入商品代码004,005。[/ FONT]
[字体="]并在网格视图中显示001,002作为存在项目。 [/ FONT]
[字体="]我的功能如下 [/ FONT]
[字体="]我的代码(内部循环)
[/ FONT]
[h = 1] [/ h]
[/ FONT]
[h = 1]我在循环内写了些什么,以便在datagridview上显示类似的项目代码,并在数据库sql服务器上插入不同的项目代码[/ h][字体="]SQL Server数据库(2014)项表[/ FONT]
[字体="]ItemCode(pk)项目名称 [/ FONT]
[字体="]001鼠标[/ FONT]
[字体="]002键盘[/ FONT]
[字体="]003耳机[/ FONT]
[字体="]在Excel 2010表格上 [/ FONT]
[字体="]ItemCode ItemName [/ FONT]
[字体="]001鼠标[/ FONT]
[字体="]002键盘[/ FONT]
[字体="]004画面[/ FONT]
[字体="]005拉姆[/ FONT]
[字体="]其实我需要在导入Excel文件时插入不存在的不同项目代码[/ FONT]
[字体="] 在sql server数据库上和在Excel上找到的“数据库中的现有项目”不会插入,而是在datagridview上显示。[/ FONT]
[字体="]根据我的情况,在表格Items上插入商品代码004,005。[/ FONT]
[字体="]并在网格视图中显示001,002作为存在项目。 [/ FONT]
[字体="]我的功能如下 [/ FONT]
[字体="]我的代码(内部循环)
[/ FONT]
C#:
[COLOR=#000088]public[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000088]static[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000088]void[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]ImportFromExcelToDataBase[/COLOR][COLOR=#666600]()[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#666600]{[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#660066]Datatable[/COLOR][COLOR=#000000] dt [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]ShowExcelData[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#660066]DataTable[/COLOR][COLOR=#000000] dtItems [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]GetSqlItems[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000088]for[/COLOR][COLOR=#000000] [/COLOR][COLOR=#666600]([/COLOR][COLOR=#000088]int[/COLOR][COLOR=#000000] i [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#006666]0[/COLOR][COLOR=#666600];[/COLOR][COLOR=#000000] i [/COLOR][COLOR=#666600]<[/COLOR][COLOR=#000000] dt[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Rows[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Count[/COLOR][COLOR=#666600];[/COLOR][COLOR=#000000] i[/COLOR][COLOR=#666600]++)[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#666600]{[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#880000]//what i write here [/COLOR]
[COLOR=#880000] // if itemcode exist on excel exist on sql server database[/COLOR]
[COLOR=#880000] then display similar items exist on database and excel as 001,002 on datagridview[/COLOR]
[COLOR=#880000] //else [/COLOR][COLOR=#000000]
[/COLOR][COLOR=#880000]// do insert data[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000088]string[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]Insert[/COLOR][COLOR=#000000] [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#008800]"Insert Into Values ("[/COLOR][COLOR=#000000] [/COLOR][COLOR=#666600]+[/COLOR][COLOR=#000000] data [/COLOR][COLOR=#666600]+[/COLOR][COLOR=#000000] [/COLOR][COLOR=#008800]")"[/COLOR][COLOR=#666600];[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#660066]DataAccess[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]ExecuteNonQuery[/COLOR][COLOR=#666600]([/COLOR][COLOR=#660066]Insert[/COLOR][COLOR=#666600]);[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#666600]}[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#666600]}[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000088]public[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]DataTable[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]ShowExcelData[/COLOR][COLOR=#666600]()[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#666600]{[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000088]string[/COLOR][COLOR=#000000] connectionString [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000088]string[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Format[/COLOR][COLOR=#666600]([/COLOR][COLOR=#008800]"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";"[/COLOR][COLOR=#666600],[/COLOR][COLOR=#000000] txtpath[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Text[/COLOR][COLOR=#666600]);[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#660066]OleDbConnection[/COLOR][COLOR=#000000] con [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000088]new[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]OleDbConnection[/COLOR][COLOR=#666600]([/COLOR][COLOR=#000000]connectionString[/COLOR][COLOR=#666600]);[/COLOR][COLOR=#000000]
con[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Open[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#660066]DataTable[/COLOR][COLOR=#000000] dt [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000088]new[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]DataTable[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
dt [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] con[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]GetOleDbSchemaTable[/COLOR][COLOR=#666600]([/COLOR][COLOR=#660066]OleDbSchemaGuid[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Tables[/COLOR][COLOR=#666600],[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000088]null[/COLOR][COLOR=#666600]);[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000088]string[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]SheetName[/COLOR][COLOR=#000000] [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] dt[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Rows[/COLOR][COLOR=#666600][[/COLOR][COLOR=#006666]0[/COLOR][COLOR=#666600]][[/COLOR][COLOR=#008800]"TABLE_NAME"[/COLOR][COLOR=#666600]].[/COLOR][COLOR=#660066]ToString[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#660066]OleDbCommand[/COLOR][COLOR=#000000] com [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000088]new[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]OleDbCommand[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
com[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Connection[/COLOR][COLOR=#000000] [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] con[/COLOR][COLOR=#666600];[/COLOR][COLOR=#000000]
com[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]CommandText[/COLOR][COLOR=#000000] [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#666600]@[/COLOR][COLOR=#008800]"SELECT [ItemCode],[ItemsName],[ItemAddress] FROM ["[/COLOR][COLOR=#000000] [/COLOR][COLOR=#666600]+[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]SheetName[/COLOR][COLOR=#000000] [/COLOR][COLOR=#666600]+[/COLOR][COLOR=#000000] [/COLOR][COLOR=#008800]"] "[/COLOR][COLOR=#666600];[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#660066]OleDbDataAdapter[/COLOR][COLOR=#000000] oledbda [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000088]new[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]OleDbDataAdapter[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
oledbda[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]SelectCommand[/COLOR][COLOR=#000000] [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] com[/COLOR][COLOR=#666600];[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#660066]DataSet[/COLOR][COLOR=#000000] ds [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000088]new[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]DataSet[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
oledbda[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Fill[/COLOR][COLOR=#666600]([/COLOR][COLOR=#000000]ds[/COLOR][COLOR=#666600]);[/COLOR][COLOR=#000000]
dt [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] ds[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Tables[/COLOR][COLOR=#666600][[/COLOR][COLOR=#006666]0[/COLOR][COLOR=#666600]];[/COLOR][COLOR=#000000]
con[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]Close[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000088]return[/COLOR][COLOR=#000000] dt[/COLOR][COLOR=#666600];[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#666600]}[/COLOR][COLOR=#000000]
dt [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]ShowExcelData[/COLOR][COLOR=#666600]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000088]public[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]DataTable[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]GetSqlItems[/COLOR][COLOR=#666600]()[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#666600]{[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000088]string[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]GetItems[/COLOR][COLOR=#000000] [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#666600]@[/COLOR][COLOR=#008800]"select ItemCode,ItemsName,ItemAddress from Items"[/COLOR][COLOR=#666600];[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#660066]DataTable[/COLOR][COLOR=#000000] tbGetItems [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]DataAccess[/COLOR][COLOR=#666600].[/COLOR][COLOR=#660066]ExecuteDataTable[/COLOR][COLOR=#666600]([/COLOR][COLOR=#660066]GetItems[/COLOR][COLOR=#000000] [/COLOR][COLOR=#666600]);[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000088]return[/COLOR][COLOR=#000000] tbGetItems [/COLOR][COLOR=#666600];[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#666600]}[/COLOR][COLOR=#000000]
dtItems [/COLOR][COLOR=#666600]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#660066]GetSqlItems[/COLOR][COLOR=#666600]();[/COLOR]