我正在尝试执行此任务。将伪代码转换为C#程序
我在我的数据库中有这张表。
items_table
------------------
Item_Name | Item ID
A | 1
B | 1
C | 2
D | 2
E | Null
F |
G | 1
H |
I | Null
Select * from items_table where Item_ID is Null or Item_ID is empty
Loop(while there are items without Item_ID)
Check the count of first Item_ID
if first Item_ID count is less than 8
(update items_table values (current Item_ID) where Item_Name is current row Item_Name)
otherwise check next Item_ID
If no Item_ID count is less than 8, insert max((Item_ID)+1) where Item_Name is current row Item_Name
对于上表,这段代码应该做这样的事情。
E,F,H,I已经GROUP_ID null或空
现在我要插入的Item_ID所有这些项目。
首先检查表中所有现有Item_ID的计数。如果任何item_ID与少于8个项目一起使用,那么插入当前Item的Item_ID。如果没有Item_ID的计数小于8,则创建一个新的Item_ID应该是最大的Item_ID + 1.
我想写这个,但无法弄清楚如何循环行和计数ID比插入现有一个或新的。
private static void FIllGroupID(string connectionString)
{
string queryStringNoGroupID =
"Use Items select * from table_items_shelves where Item_ID is Null or Item_ID = '';";
SqlCommand GetAllWithoutID = new SqlCommand(queryStringNoGroupID);
DataTable DataTableAllWithoutID = new DataTable();
SqlDataAdapter adapterAllWithoutID = new SqlDataAdapter(GetAllWithoutID);
adapterAllWithoutID.Fill(DataTableAllWithoutID);
foreach (DataRow row in DataTableAllWithoutID.Rows)
{
}
}
如何循环现有的item_ids并对它们进行计数。如果count小于8,则会在当前行中插入相同的ID或者创建max(item_id)+1并插入该ID。
你觉得哪个部分很难?具体说明你遇到的问题。 – PhonicUK
我无法弄清楚如何通过group_id循环并对它们进行计数,并且为每个项目插入相同的/新的group_id。 – mubi