大家好,好日子。我有一個關於編碼的問題,我有很長的代碼行來顯示16個項目從SQL服務器到16個文本塊,沒有錯誤,但我想保持短的代碼行。這是2周的TextBlocks(滿分16)例子代碼:將sql server數據顯示爲16個文本塊
orgDa.SelectCommand = conn.CreateCommand();
orgDa.SelectCommand.CommandText = "select OrganizationName from Organizationtbl where OrgID=1";
orgDa.SelectCommand.CommandType = CommandType.Text;
orgDa.Fill(ds, "Organizationtbl");
deptDa.SelectCommand = conn.CreateCommand();
deptDa.SelectCommand.CommandText = "select DepartmentName from Departmenttbl where DeptID=1";
deptDa.SelectCommand.CommandType = CommandType.Text;
deptDa.Fill(ds, "Departmenttbl");
if (ds.Tables["Organizationtbl"].Rows.Count == 1)
{
foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows)
{
if (orgItem.IsNull("OrganizationName"))
{
foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows)
{
textblock_EventTitle0.Text = deptItem["DepartmentName"].ToString();
}
}
else
{
textblock_EventTitle0.Text = orgItem["OrganizationName"].ToString();
}
}
}
else
{
foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows)
{
if (deptItem.IsNull("DepartmentName"))
{
foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows)
{
textblock_EventTitle0.Text = orgItem["OrganizationName"].ToString();
}
}
else
{
textblock_EventTitle0.Text = deptItem["DepartmentName"].ToString();
}
}
}
orgDa.SelectCommand = conn.CreateCommand();
orgDa.SelectCommand.CommandText = "select OrganizationName from Organizationtbl where OrgID=2";
orgDa.SelectCommand.CommandType = CommandType.Text;
orgDa.Fill(ds, "Organizationtbl");
deptDa.SelectCommand = conn.CreateCommand();
deptDa.SelectCommand.CommandText = "select DepartmentName from Departmenttbl where DeptID=2";
deptDa.SelectCommand.CommandType = CommandType.Text;
deptDa.Fill(ds, "Departmenttbl");
if (ds.Tables["Organizationtbl"].Rows.Count == 1)
{
foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows)
{
if (orgItem.IsNull("OrganizationName"))
{
foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows)
{
textblock_EventTitle1.Text = deptItem["DepartmentName"].ToString();
}
}
else
{
textblock_EventTitle1.Text = orgItem["OrganizationName"].ToString();
}
}
}
else
{
foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows)
{
if (deptItem.IsNull("DepartmentName"))
{
foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows)
{
textblock_EventTitle1.Text = orgItem["OrganizationName"].ToString();
}
}
else
{
textblock_EventTitle1.Text = deptItem["DepartmentName"].ToString();
}
}
}
我該怎麼讓從SQL 16個到16周的TextBlocks只在一個循環?我需要你的幫助。先謝謝你。
感謝您的幫助。你的反饋真的幫助了我很多。有一個幸運的日子:) – Sephiroth111 2012-03-03 09:43:18