C# 比较数据集列类型
代码如下:C# 比较数据集列类型,c#,dataset,C#,Dataset,代码如下: try { string strReadDataLine; strReadDataLine = sr.ReadLine(); while (strReadDataLine != null) { string[] strReadDataLineSplited = strReadDataLine.Split(';');
try
{
string strReadDataLine;
strReadDataLine = sr.ReadLine();
while (strReadDataLine != null)
{
string[] strReadDataLineSplited = strReadDataLine.Split(';');
DataRow thisRow = thisDataSet.Tables["Repartition"].NewRow();
DataTable item = thisDataSet.Tables["Repartition"];
for (int i = 0; i < strDataLines.Length; i++)
{
DataColumn thisColomn =
thisDataSet.Tables["Repartition"].Columns[i];
// Here i need to know if the colomn is a string
if (thisColomn.DataType.ToString() == "System.String")
{
thisRow[strDataLines[i]] = strReadDataLineSplited[i];
}
}
thisRow["ID_USAGER"] = 1;
thisDataSet.Tables["Repartition"].Rows.Add(thisRow);
strReadDataLine = sr.ReadLine();
}
//thisDataAdapter.Update(thisDataSet, "Repartition");
}
试试看
{
字符串streaddataline;
strReadDataLine=sr.ReadLine();
while(strReadDataLine!=null)
{
字符串[]strReadDataLineSplited=strReadDataLine.Split(“;”);
DataRow thisRow=thisDataSet.Tables[“重新分区”].NewRow();
DataTable项=thisDataSet.Tables[“重新分区”];
for(int i=0;i
我需要知道列是否是字符串,以便将数据作为字符串分配给列。我得到的是argumentException,它说“输入字符串的格式不正确。无法存储在我的\u FLOAT列中。预期类型为double。”
我真正需要的是将列类型与其他类型进行比较以获得类型,然后将列分配给正确的类型
我希望这一点很清楚,因为我的英语不太好。如果我理解正确,我会构建代码片段的功能副本,并修复它以正确处理类型转换。你只错过了两件事: #1-按序号索引列,并使用该信息获取列类型。然后设置为“我的名字”列编制索引的信息。我通过在下面引入'columnName'变量来纠正这一点 #2-要将字符串输入正确转换为所需的列类型,只需使用System.convert.ChangeType(object,type)方法,如下所示
static void Main()
{
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Repartition");
DataColumn col;
col = dt.Columns.Add("ID_USAGER", typeof(int));
col = dt.Columns.Add("TestString", typeof(string));
col = dt.Columns.Add("TestInt", typeof(int));
col = dt.Columns.Add("TestDouble", typeof(double));
string testData = "TestString;TestInt;TestDouble";
testData += Environment.NewLine + "Test1;1;1.1";
testData += Environment.NewLine + "Test2;2;2.2";
Test(ds, new StringReader(testData));
}
public static void Test(DataSet thisDataSet, StringReader sr)
{
string[] strDataLines = sr.ReadLine().Split(';');
string strReadDataLine;
strReadDataLine = sr.ReadLine();
while (strReadDataLine != null)
{
string[] strReadDataLineSplited = strReadDataLine.Split(';');
DataRow thisRow = thisDataSet.Tables["Repartition"].NewRow();
DataTable item = thisDataSet.Tables["Repartition"];
for (int i = 0; i < strDataLines.Length; i++)
{
string columnName = strDataLines[i];
//#1 Don't use this as Columns[i] may not be Columns[columnName]
//DataColumn thisColomn = thisDataSet.Tables["Repartition"].Columns[i];
DataColumn thisColomn = thisDataSet.Tables["Repartition"].Columns[columnName];
//#2 Assing to the results of the string converted to the correct type:
thisRow[strDataLines[i]] = System.Convert.ChangeType(strReadDataLineSplited[i], thisColomn.DataType);
}
thisRow["ID_USAGER"] = 1;
thisDataSet.Tables["Repartition"].Rows.Add(thisRow);
strReadDataLine = sr.ReadLine();
}
}
static void Main()
{
数据集ds=新数据集();
DataTable dt=ds.Tables.Add(“重新分区”);
数据列col;
col=dt.Columns.Add(“ID_USAGER”,typeof(int));
col=dt.Columns.Add(“TestString”,typeof(string));
col=dt.Columns.Add(“TestInt”,typeof(int));
col=dt.Columns.Add(“TestDouble”,typeof(double));
string testData=“TestString;TestInt;TestDouble”;
testData+=Environment.NewLine+“Test1;1;1.1”;
testData+=Environment.NewLine+“Test2;2;2.2”;
测试(ds,新StringReader(testData));
}
公共静态无效测试(数据集thisDataSet,StringReader sr)
{
字符串[]strDataLines=sr.ReadLine().Split(“;”);
字符串streaddataline;
strReadDataLine=sr.ReadLine();
while(strReadDataLine!=null)
{
字符串[]strReadDataLineSplited=strReadDataLine.Split(“;”);
DataRow thisRow=thisDataSet.Tables[“重新分区”].NewRow();
DataTable项=thisDataSet.Tables[“重新分区”];
for(int i=0;i
thisRow[strDataLines[i]]看起来您并没有将数据存储在您认为合适的位置。您可以发布您的表结构和一些示例数据吗?