SqlDataReader的GetDateTime格式 [英] SQLDataReader GetDateTime Format
本文介绍了SqlDataReader的GetDateTime格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
检查我下面的code:搜索结果
Check my code below:
HTML页:
<table width="100%" align="right" cellpadding="2" cellspacing="2" border="0" bgcolor="#EAEAEA">
<tr align="center" style="background-color: yellow; color: black;">
<th colspan="2">Fauzan</th>
<th colspan="2">Febri</th>
</tr>
<tr align="left" style="background-color: gray; color: black;">
<td>ID</td>
<td>Number01</td>
<td>TheDate</td>
<td>Number02</td>
</tr>
<%=getWhileLoopData()%>
</table>
code背后:
Code Behind:
public string getWhileLoopData()
{
string htmlStr = "";
SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "SELECT * FROM MyTable WHERE TheDate = @TheDate";
thisCommand.Parameters.AddWithValue("@TheDate", txtDate.Text);
thisConnection.Open();
SqlDataReader reader = thisCommand.ExecuteReader();
while (reader.Read()) {
int id = reader.GetInt32(0);
int Number01 = reader.GetInt32(1);
DateTime TheDate = reader.GetDateTime(2);
Decimal Number02 = reader.GetDecimal(3);
//string Pass = reader.GetString(2);
htmlStr += "<tr><td>" + id + "</td><td>" + Number01 + "</td><td>" + TheDate + "</td><td>" + Number02 + "</td></tr>";
}
thisConnection.Close();
return htmlStr;
}
现在的问题是,我怎么能格式化数据读取器?像TheDate领域,从2014年6月18日12:00:00 AM'到'18 /月/ 2014年。也为'Number02'领域,我怎么可以格式化呢?就像从'0.829'变成'0.83'。搜索结果
The question is, how can i format the data reader? Like, "TheDate" field, from '6/18/2014 12:00:00 AM' to '18/Jun/2014'. Also for 'Number02' field, how can i format it? Like, from '0.829' become '0.83'.
推荐答案
您可以使用的ToString()用的自定义日期和时间格式字符串
You can format datetime using ToString() with Custom Date and Time Format Strings
string strDate = TheDate.ToString("dd/MMM/yyyy");
您可以使用 Math.Round 一>以格式编号
Number02 = Math.Round(Number02, 2);
您可以直接格式化日期和数量正在制作的HTML字符串
You can directly format date and number in the html string you are making
htmlStr += "<tr><td>" + id + "</td><td>" + Number01 + "</td><td>" +
TheDate.ToString("dd/MMM/yyyy") + "</td><td>" + Math.Round(Number02, 2) + "</td></tr>";
这篇关于SqlDataReader的GetDateTime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文