protected void Auto_Table()
{
Table distb=new Table();
TableRow tr=new TableRow();
TableCell tc=new TableCell();
Label[] lb=new Label[5];
SqlConnection mycon=new SqlConnection();
SqlDataReader sdr;
SqlCommand cmd=new SqlCommand();
string sql;
int i,j,m;
distb.BorderWidth=1;
distb.CellPadding = 0;
distb.CellPadding = 0;
lb[0] = new Label();
lb[1] = new Label();
lb[2] = new Label();
lb[3] = new Label();
lb[4] = new Label();
lb[0].Text = "PlanId";
lb[1].Text = "FileType";
lb[2].Text = "Description";
lb[3].Text = "顯示圖片";
lb[4].Text = "IsPhoto";
sql = "select planid,filetype,description,photopath,isphoto from tblmapphoto";
mycon.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Conn1"].ConnectionString;
mycon.Open();
cmd.Connection = mycon;
cmd.CommandText = sql;
sdr = cmd.ExecuteReader();
for(i=0;i<=4;i++)
{
tr = new TableRow();
for (j=0;j<=4;j++)
{
tc = new TableCell();
tc.Text = lb[j].Text;
tr.Cells.Add(tc);
}
distb.Rows.Add(tr);
while( sdr.Read())
{
tr = new TableRow();
for(m=0;m<=sdr.FieldCount-1;m++)
{
tc = new TableCell();
tc.Text = sdr[m].ToString();
tr.Cells.Add(tc);
}
distb.Rows.Add(tr);
}
}
} 不知什么原因,在网页上什么都没显示,也没有报错,等待救星出现呀,在线等!
{
Table distb=new Table();
TableRow tr=new TableRow();
TableCell tc=new TableCell();
Label[] lb=new Label[5];
SqlConnection mycon=new SqlConnection();
SqlDataReader sdr;
SqlCommand cmd=new SqlCommand();
string sql;
int i,j,m;
distb.BorderWidth=1;
distb.CellPadding = 0;
distb.CellPadding = 0;
lb[0] = new Label();
lb[1] = new Label();
lb[2] = new Label();
lb[3] = new Label();
lb[4] = new Label();
lb[0].Text = "PlanId";
lb[1].Text = "FileType";
lb[2].Text = "Description";
lb[3].Text = "顯示圖片";
lb[4].Text = "IsPhoto";
sql = "select planid,filetype,description,photopath,isphoto from tblmapphoto";
mycon.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Conn1"].ConnectionString;
mycon.Open();
cmd.Connection = mycon;
cmd.CommandText = sql;
sdr = cmd.ExecuteReader();
for(i=0;i<=4;i++)
{
tr = new TableRow();
for (j=0;j<=4;j++)
{
tc = new TableCell();
tc.Text = lb[j].Text;
tr.Cells.Add(tc);
}
distb.Rows.Add(tr);
while( sdr.Read())
{
tr = new TableRow();
for(m=0;m<=sdr.FieldCount-1;m++)
{
tc = new TableCell();
tc.Text = sdr[m].ToString();
tr.Cells.Add(tc);
}
distb.Rows.Add(tr);
}
}
} 不知什么原因,在网页上什么都没显示,也没有报错,等待救星出现呀,在线等!
/// <summary>
/// 将DataReader 转为 DataTable
/// </summary>
/// <param name="DataReader">DataReader</param>
public static DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
{
DataTable datatable = new DataTable();
DataTable schemaTable = dataReader.GetSchemaTable();
//动态添加列
try
{
foreach(DataRow myRow in schemaTable.Rows)
{
DataColumn myDataColumn = new DataColumn();
myDataColumn.DataType = myRow.GetType();
myDataColumn.ColumnName = myRow[0].ToString();
datatable.Columns.Add(myDataColumn);
}
//添加数据
while(dataReader.Read())
{
DataRow myDataRow = datatable.NewRow();
for(int i=0;i<schemaTable.Rows.Count;i++)
{
myDataRow[i] = dataReader[i].ToString();
}
datatable.Rows.Add(myDataRow);
myDataRow = null;
}
schemaTable = null;
dataReader.Close();
return datatable;
}
catch(Exception ex)
{
Error.Log(ex.ToString());
throw new Exception("转换出错出错!",ex);
}
}
#endregion