且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中将逗号分隔的字符串添加到数据表中?

更新时间:2023-10-31 16:46:22

尝试如下操作:

DataTable table = new DataTable();
table.Columns.Add("name", typeof(string));
table.Columns.Add("value", typeof(string));

string name = "A,B,C,D";
string value = "100,200,300,400";

string[] names = name.Split(',');
string[] values = value.Split(',');

for(int i=0; i<names.Length; i++)
    table.Rows.Add(new object[]{ names[i], values[i] });

但是您应该实现一些验证代码以使其更加实用合适的.

But you should implement some validation code to make it more appropriate.