如何将值存储到另一个表中? [英] How to store value into another table?
问题描述
您好我有以下代码。如何将变量中的值存储到临时表中?
Hi i have the following codes. How can i store the values that are inside the variable into a temporary table?
foreach (DataRow row in dt.Rows)
{
var EncryptedString = row["Encrypted"].ToString();
usernameDB = stringDecrypt(EncryptedString.Substring(4, 10), "HRM");
passwordDB = stringDecrypt(EncryptedString.Substring(84, 10), "HRM");
}
而不是变量,我想将它们存储到一个表中。例如,我有4行记录。如果我使用上面的代码,前一个代码将替换为新代码。如何在表格中逐行维护所有这些? Thks
instead of variable, i want to store them into a table. For example, i have 4 rows of records. If i use the above codes, the previous would be replaced by the new one. How can i maintain all of them row by row in a table? Thks
推荐答案
添加到数据表代码:
add to datatable code:
DataTable dt = new DataTable();
dt.Columns.Add("Name", System.Type.GetType("System.String"));
dt.Columns.Add("Password", System.Type.GetType("System.String"));
foreach (DataRow row in dt.Rows)
{
var EncryptedString = row["Encrypted"].ToString();
usernameDB = stringDecrypt(EncryptedString.Substring(4, 10), "HRM");
passwordDB = stringDecrypt(EncryptedString.Substring(84, 10), "HRM");
dt.Rows.Add(usernameDB, passwordDB);
}
为什么选择桌子?你最好使用下面的课程
Why Table? you better use class like below
public class User
{
public string Name { get; set; }
public string Password { get; set; }
}
然后你可以创建如下用户列表并添加用户
then you can create list of users like below and add users to it
List<User> users = new List<User>();
foreach (DataRow row in dt.Rows)
{
var EncryptedString = row["Encrypted"].ToString();
usernameDB = stringDecrypt(EncryptedString.Substring(4, 10), "HRM");
passwordDB = stringDecrypt(EncryptedString.Substring(84, 10), "HRM");
users.Add(new User() { Name = usernameDB, Password = passwordDB });
}
如果您需要检查此用户列表中是否存在给定的用户名和密码,请执行以下操作
if you need check for given user name and password exist in this user list or not, do as below
if(users.Any(u=> u.Name =inputUserName && u.Password == inputPassword))
{
//user exist
}
{
// incorrect user name or password
}
这篇关于如何将值存储到另一个表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!