如何将空列替换为“ND”使用ASP.NET C#? [英] How to replace empty column as "ND" using ASP.NET C#?
问题描述
我想将数据集数据表中特定空列的值设置为数据集中的ND。
怎么做?
我尝试了什么:
I want to set value for particular empty column in data set data table as "ND" in data set.
How to do it?
What I have tried:
if (ds.Tables[s].Rows[i][Columncountvalue + 19].ToString().Trim() == "")
{
ds.Tables[s].Rows[i][Columncountvalue + 19].ToString().Trim().Replace("", "ND");
}
上面的代码是我试图在数据集数据表中将空列填充为ND但是发生以下错误
字符串长度不能为零。参数名称oldvalue
the above code is i tried to fill the empty column as "ND" in dataset data table but below error is occurs
string cannot be of zero length. parameter name oldvalue
推荐答案
您好,
怎么样:
Hi,
How about this :
private void UpdateNulls (DataTable dataTable){
List<string> dcNames = dataTable.Columns
.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToList(); //This querying of the Column Names, you could do with LINQ
foreach (DataRow row in dataTable.Rows) //This is the part where you update the cell one by one
foreach (string columnName in dcNames)
row[columnName] = string.IsNullOrEmpty(Convert.ToString(row[columnName])) ? "ND" : row[columnName];
}
我建议在服务器级别上这样做,因为它总是比在客户端更快。
所有你需要做的,就是编写类似这样的查询:
I'd suggest to do that on server level, because it is always faster than on client side.
All you need to do, is to write query similar to this:
SELECT Field1, Field2, Field3 = CASE WHEN RTRIM(LTRIM(Field3)) = '' THEN 'ND' ELSE Field3 END
FROM YourTableName
现在,你必须创建存储过程 [ ^ ](SP)并将该SP称为返回所需的结果集。怎么样?请检查:演练:仅使用存储过程(C#) [ ^ ]
只使用存储过程有很多好处,例如保护 SQL注入 [ ^ 。请参阅:如何:保护ASP.NET中的SQL注入 [ ^ ]
试试!
Now, you have to create stored procedure[^] (SP) and call that SP to return desired result set. How? Check this: Walkthrough: Using Only Stored Procedures (C#)[^]
There's lot of benefits of using only stored procedures, such as protection of SQL Injection[^]. See: How To: Protect From SQL Injection in ASP.NET[^]
Try!
这篇关于如何将空列替换为“ND”使用ASP.NET C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!