I want to get max size of any column of a table using datatable in C#. Example is that if i have a table in database named dept and its attributes are NAME and ADDRESS and i have set size of NAME as varchar(50) and Address as varchar(30).
Now i want to get 50 and 30.....
i have written this code
DataSet ds1 = new DataSet();
DataSet ds = new DataSet();
string query = Form1.query1Pass;
SqlCommand cmd = new SqlCommand(query.ToString(), con);
this.tablename1 = query.Substring(query.LastIndexOf("from")+4); ;
cmd.ExecuteNonQuery();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(this.ds);
string query2 = Form1.query2Pass;
cmd = new SqlCommand(query2, con);
adp = new SqlDataAdapter(cmd);
adp.Fill(this.ds1);
StringBuilder datatype2 = new StringBuilder();
this.datatype2.Append("Data Type Conflict: ");
this.datetimeCon.Append("DateTime Format Conflict: ");
DataTable table1 = this.ds.Tables[0];
DataTable table2 = this.ds1.Tables[0]; ;
DataColumnCollection col1 = table1.Columns;
DataColumnCollection col2 = table2.Columns;
foreach (DataColumn ds in col1) {
foreach (DataColumn ds2 in col2) {
if (ds.ColumnName.Equals(ds2.ColumnName)) {
if (!ds.DataType.Equals(ds2.DataType)) {
this.datatype2.Append(ds.ColumnName + "<" + ds.DataType + ">, " + ds2.ColumnName + "<" + ds2.DataType + ">,");
}
else
if (ds.MaxLength!=ds2.MaxLength) {
maxLength.Append(ds.ColumnName+"<"+ds.MaxLength+">, "+ds2.ColumnName+"<"+ds2.MaxLength+">,");
}
}
}
}