SQL表:
ID CreatedDate MessageText CreatedBy IsActive ClientText LocationText ProviderText SpecialtyText
1 March 4, 2015 dsdfsdf Test1 False
2 March 4, 2015 dsdfsdf Test2 True
3 March 4, 2015 dsdfsdf Test2 True Test Client location1 Test2 MD
4 March 4, 2015 This is to add Test1 False Test Client location2 Test3 DD
获取表格:
using (SqlConnection conn = new SqlConnection(gloString))
{
string strSql = @"SELECT * FROM [DB].[dbo].[table2]";
try
{
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
// this will query your database and return the result to your datatable
myDataSet = new DataSet();
da.Fill(myDataSet);
}
catch (Exception ce)
{
}
}
我的ASP.net页面中有以下标签:<asp:Label ID="lblMessage" Font-Size="x-small" runat="server" Text="" ClientIDMode="Static"></asp:Label>
如何更新标签:
lblMessage.Text += "<b>ALL MESSAGES:</b> <br />";
foreach (ROW WHERE COLUMN {CLIENTTEXT}, {LOCATIONTEXT}, {PROVIDERTEXT}, AND {SPECIALTYTEXT} IS EMPTY)
{
lblMessage.Text += {CREATEDDATE} + " - " + {MESSAGETEXT} + "<br /><br />";
}
lblMessage.Text += "<hr />";
lblMessage.Text += "<b>SPECIFIC MESSAGES:</b> <br />";
foreach (ROW WHERE COLUMN {CLIENTTEXT}, {LOCATIONTEXT}, {PROVIDERTEXT}, OR {SPECIALTYTEXT} IS NOT EMPTY)
{
lblMessage.Text += {CREATEDDATE} + " - " + {MESSAGETEXT} + "<br /><br />";
}
答案 0 :(得分:1)
我没有看到您展示的2 for循环的差异。
如果它只是一个表,那么你应该使用DataTable而不是DataSet
你可以这样做:
foreach (DataRow row in table.Rows)//table is a DataTable
{
var clientText = row["ClientText"].ToString();
var locationText = row["LocationText"].ToString();
var providerText = row["ProviderText"].ToString();
var specialtyText = row["SpecialtyText"].ToString();
var createdDate = row["CreatedDate"].ToString();
var messageText = row["MessageText"].ToString();
if (string.IsNullOrEmpty(clientText) &&
string.IsNullOrEmpty(locationText) &&
string.IsNullOrEmpty(providerText) &&
string.IsNullOrEmpty(specialtyText))
{
lblMessage.Text += createdDate + " - " + messageText + "<br /><br />";
}
}
然后根据需要添加else块。您也可以使用linQ
foreach (var clientText in from DataRow row in table.Rows
let clientText = row["ClientText"].ToString()
let locationText = row["LocationText"].ToString()
let providerText = row["ProviderText"].ToString()
let specialtyText = row["SpecialtyText"].ToString()
where string.IsNullOrEmpty(clientText) &&
string.IsNullOrEmpty(locationText) &&
string.IsNullOrEmpty(providerText) &&
string.IsNullOrEmpty(specialtyText)
select clientText)
{
//some code here
}
答案 1 :(得分:1)
我得到它的方式,你想使用不包含“SpecialityText”的行更新你的标签。问题是你不知道如何。
假设您在页面中使用的DataSet在获取之后仍然被命名为 myDataSet ,以下代码应该可以解决问题。请注意我假设您已导入一些数据访问命名空间。 DBNull.Value 应该替换为帮助程序使用的null表示形式。
string specific = "";
string generic = "";
foreach (DataRow r in myDataSet.tables[0].Rows){
//Add all your conditions here. It seems you want two groups of messages
if (r["SpecialtyText"].ToString == ""){
generic += r["CreatedDate"] + " - " + r["MessageText"] + "<br /><br />";
}
else{
specific += r["CreatedDate"] + " - " + r["MessageText"] + "<br /><br />";
}
}
lblMessage.Text = generic + "<hr /><b>SPECIFIC MESSAGES:</b> <br />" + specific;