伪代码到 C# 程序
本文关键字:程序 伪代码 | 更新日期: 2023-09-27 18:36:10
我正在尝试执行此任务。
我的数据库中有这个表。
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,我有空或空Group_ID
现在我必须为所有这些项目插入Item_ID。
对表中所有现有Item_IDs进行首次检查计数。如果任何item_ID用于少于 8 个项目,则为当前项目插入该Item_ID。如果没有Item_ID计数小于 8,则创建一个最大值应为 Item_ID + 1 的新Item_ID。
我正在尝试编写此内容,但无法弄清楚如何遍历行并计算 ID,而不是插入现有的或新的 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。
现在谁要从问题中删除反对票?
const string str = @"数据源=本地主机;初始目录=项目;集成安全性=True"; 静态空 主(字符串[] 参数) { const string connectionString = str;
DataTable DataTableAllWithoutID = new DataTable();
#if !test
string queryString = "select * from table_items_shelves;";
SqlDataAdapter adapterAllWithoutID = new SqlDataAdapter(queryString, connectionString);
adapterAllWithoutID.Fill(DataTableAllWithoutID);
adapterAllWithoutID.Dispose();
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
string insertString = "Update table_items_shelves Set Item_ID = @Item_ID where Item_Name = '@key';";
SqlCommand insertCommand = new SqlCommand(insertString, connection);
insertCommand.Parameters.Add("@Item_ID", SqlDbType.Int);
insertCommand.Parameters.Add("@key", SqlDbType.NVarChar);
#else
DataTableAllWithoutID.Columns.Add("Item_Name", typeof(string));
DataTableAllWithoutID.Columns.Add("Item_ID", typeof(object));
foreach (List<object> row in input)
{
DataRow newRow = DataTableAllWithoutID.Rows.Add();
newRow.ItemArray = row.ToArray();
}
#endif
//this code will get empty items
List<DataRow> nullOrEmpty = DataTableAllWithoutID.AsEnumerable()
.Where(x => x.Field<object>("Item_ID") == null)
.ToList();
//this creates a dictionary of valid items
Dictionary<int, List<DataRow>> dict = DataTableAllWithoutID.AsEnumerable()
.Where(x => x.Field<object>("Item_ID") != null)
.GroupBy(x => x.Field<object>("Item_ID"), x => x)
.ToDictionary(x => Convert.ToInt32(x.Key), x => (List<DataRow>)x.ToList());
//create IEnumerator for the null items
IEnumerator<DataRow> emptyRows = nullOrEmpty.GetEnumerator();
Boolean noMoreEmptyRows = false;
if (emptyRows != null)
{
foreach (int key in dict.Keys)
{
Console.WriteLine(key.ToString());
//get count of items
int count = dict[key].Count;
int itemID = (int)key;
for (int index = count; count < 8; count++)
{
if (emptyRows.MoveNext())
{
//get an item from the null list
emptyRows.Current["Item_ID"] = itemID;
insertCommand.Parameters["@Item_ID"].Value = itemID;
insertCommand.Parameters["@key"].Value = emptyRows.Current["Item_Name"];
insertCommand.ExecuteNonQuery();
Console.WriteLine("current item ID " + itemID);
Console.WriteLine("current count " + count);
//Console.ReadKey();
}//end if
else
{
noMoreEmptyRows = true;
break;
}//end else
}//end for
if (noMoreEmptyRows)
break;
}//end foreach
if (!noMoreEmptyRows)
{
//increment key to one greater than max value
int maxKey = dict.Keys.Max() + 1;
int count = 0;
while (emptyRows.MoveNext())
{
//get an item from the null list
emptyRows.Current["Item_ID"] = maxKey.ToString();
insertCommand.Parameters["@Item_ID"].Value = maxKey.ToString();
insertCommand.Parameters["@key"].Value = emptyRows.Current["Item_ID"];
insertCommand.ExecuteNonQuery();
count++;
if (count == 8)
{
maxKey++;
count = 0;
}
}
}
}//end if
#if test
foreach (DataRow row in DataTableAllWithoutID.Rows)
{
Console.WriteLine("Item_Name : {0} Item ID : {1}",
row["Item_Name"], row["Item_ID"]);
}
#endif
FIllGroupID(str);
Console.ReadKey();
}