こんにちは、良い日。私はコーディングに関する問題があります。SQL Serverから16個のテキストブロックまで16個の項目を表示するだけの長いコードがありますが、エラーはありませんが、短いコード行を維持したいのです。ここでは例のコード(16のうち)2つのテキストブロックです:16のテキストブロックにSQL Serverデータを表示
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();
}
}
}
私はただ一つのループで16のテキストブロックへのSQLから16個の項目を表示するにはどうすればよい
?あなたの助けが必要です。前もって感謝します。
ありがとうございました。あなたのフィードバックは本当に私を大いに助けました。恵まれた一日を :) – Sephiroth111