如何从 ASP.NET C# 中的 SqlDataSource 获取特定的行值?
Posted
技术标签:
【中文标题】如何从 ASP.NET C# 中的 SqlDataSource 获取特定的行值?【英文标题】:How to get specific row value from SqlDataSource in ASP.NET C#? 【发布时间】:2021-10-26 22:18:33 【问题描述】:如何通过 SQLDataSource 工具获取存在 SQL Server 数据库的特定值,用于比较两个值,一个是加密的,另一个是从密码文本框获取的,如下面的代码,在 (ASPxLabel) 上不显示任何输出? 请建议获得解决方案。
protected void ASPxButton1_Click(object sender, EventArgs e)
DataView dview (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
for (int i = 0; i < dview.Count ; i++)
string view = (String)dview.Table.Rows[i][2];
string st = Convert.ToString(view);
if ( VerifyHash("Admin", "SHA512",st))
ASPxLabel1.Text = "Correct";
else
ASPxLabel1.Text = "Incorrect";
public static bool VerifyHash(string plainText, string hashAlgorithm, string hashValue)
// Convert base64-encoded hash value into a byte array.
byte[] hashWithSaltBytes = Convert.FromBase64String(hashValue);
// We must know size of hash (without salt).
int hashSizeInBits, hashSizeInBytes;
// Make sure that hashing algorithm name is specified.
if (hashAlgorithm == null)
hashAlgorithm = "";
// Size of hash is based on the specified algorithm.
switch (hashAlgorithm.ToUpper())
case "SHA384":
hashSizeInBits = 384;
break;
case "SHA512":
hashSizeInBits = 512;
break;
default: // Must be MD5
hashSizeInBits = 128;
break;
// Convert size of hash from bits to bytes.
hashSizeInBytes = hashSizeInBits / 8;
// Make sure that the specified hash value is long enough.
if (hashWithSaltBytes.Length < hashSizeInBytes)
return false;
// Allocate array to hold original salt bytes retrieved from hash.
byte[] saltBytes = new byte[hashWithSaltBytes.Length - hashSizeInBytes];
// Copy salt from the end of the hash to the new array.
for (int i = 0; i < saltBytes.Length; i++)
saltBytes[i] = hashWithSaltBytes[hashSizeInBytes + i];
// Compute a new hash string.
string expectedHashString = ComputeHash(plainText, hashAlgorithm, saltBytes);
// If the computed hash matches the specified hash,
// the plain text value must be correct.
return (hashValue == expectedHashString);
public static string ComputeHash(string plainText, string hashAlgorithm, byte[] saltBytes)
// If salt is not specified, generate it.
if (saltBytes == null)
// Define min and max salt sizes.
int minSaltSize = 4;
int maxSaltSize = 8;
// Generate a random number for the size of the salt.
Random random = new Random();
int saltSize = random.Next(minSaltSize, maxSaltSize);
// Allocate a byte array, which will hold the salt.
saltBytes = new byte[saltSize];
// Initialize a random number generator.
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
// Fill the salt with cryptographically strong byte values.
rng.GetNonZeroBytes(saltBytes);
// Convert plain text into a byte array.
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
// Allocate array, which will hold plain text and salt.
byte[] plainTextWithSaltBytes =
new byte[plainTextBytes.Length + saltBytes.Length];
// Copy plain text bytes into resulting array.
for (int i = 0; i < plainTextBytes.Length; i++)
plainTextWithSaltBytes[i] = plainTextBytes[i];
// Append salt bytes to the resulting array.
for (int i = 0; i < saltBytes.Length; i++)
plainTextWithSaltBytes[plainTextBytes.Length + i] = saltBytes[i];
HashAlgorithm hash;
// Make sure hashing algorithm name is specified.
if (hashAlgorithm == null)
hashAlgorithm = "";
// Initialize appropriate hashing algorithm class.
switch (hashAlgorithm.ToUpper())
case "SHA384":
hash = new SHA384Managed();
break;
case "SHA512":
hash = new SHA512Managed();
break;
default:
hash = new MD5CryptoServiceProvider();
break;
// Compute hash value of our plain text with appended salt.
byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes);
// Create array which will hold hash and original salt bytes.
byte[] hashWithSaltBytes = new byte[hashBytes.Length +
saltBytes.Length];
// Copy hash bytes into resulting array.
for (int i = 0; i < hashBytes.Length; i++)
hashWithSaltBytes[i] = hashBytes[i];
// Append salt bytes to the result.
for (int i = 0; i < saltBytes.Length; i++)
hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i];
// Convert result into a base64-encoded string.
string hashValue = Convert.ToBase64String(hashWithSaltBytes);
// Return the result.
return hashValue;
【问题讨论】:
【参考方案1】:您可以使用 DataRowView 从数据中获取特定的行值
DataRowView
是来自 DataView
的 DataRow
DataRowView rowView = dview[0];
或者您可以在 linq 查询中使用索引来获取特定的行数据。
【讨论】:
感谢您的回复,感谢您的回复,但仍然没有结果。以上是关于如何从 ASP.NET C# 中的 SqlDataSource 获取特定的行值?的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 C# ASP.net 从 SQL Server 中的文本框中存储日期
如何使用 C# 从 ASP.NET 中的 SQL Server 数据库中检索和显示 GridView 中的值
如何从 ASP.NET C# 中的 SqlDataSource 获取特定的行值?
如何使用C#从asp.net MVC中的枚举绑定下拉列表[重复]