且构网

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

如何检查表的所有行在ado.net中是否都是整数?

更新时间:2023-11-29 08:20:52

",oconn); OleDbDataAdapter da = OleDbDataAdapter(cmd); oconn.Open(); da.Fill(ds); oconn.Close();
", oconn); OleDbDataAdapter da = new OleDbDataAdapter(cmd); oconn.Open(); da.Fill(ds); oconn.Close();





where ds is a datatable.


DataTable dt=  ds.Table[0];
int numberOfColumns = dt.Columns.Count;
bool IsAllInt =true;
// go through each row
foreach (DataRow dr in dt.Rows)
{
    // go through each column in the row
    for (int i = 0; i < numberOfColumns; i++)
    {
        int val;
        if(!int.TryParse(dr[i].ToString(), out val))
        {
           IsAllInt =false;
           break;
        }
    }
}
//check the value of IsAllInt


从XLS文件加载数据之前,您应先将Column对象的类型指定为int,然后将DataTable及其列创建为try-catch-finally阻止,您应该从XLS文件中的数据中加载数据,如果XLS中的数据中存在错误,则会引发某些异常.
Before to load the data from XLS file you should create your DataTable and its columns by specifying the type of the Column objects as int then, into a try-catch-finally block you should load your data from your data from XLS file and if there errors in the data from XLS some exception will be thrown.