【发布时间】:2014-11-28 12:09:21
【问题描述】:
我有来自数据库(int)的测试变量。我需要在 DateTime.AddHours 上使用这个变量。
我试过了:
DateTime.Now.AddHours(-<%#test%>)
来自数据库的测试 = 100
所以它一定是这样的:
DateTime.Now.AddHours(-100)
但是编译器出错:
CS1040: Preprocessor directives must appear as the first non-whitespace character on a line
我该如何解决?
从公共页面加载测试:
OleDbConnection Connection;
Connection = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +
Server.MapPath("~/db.mdb"));
OleDbCommand Command1,
Command1 = new OleDbCommand("SELECT FTPTARIHARALIGI FROM Table1 WHERE ID = 1", Connection);
Connection.Open();
int test = (int)Command1.ExecuteScalar();
我需要在其他类(gridview 行)中使用这个变量
public void GridView2_RowDataBound(Object sender, GridViewRowEventArgs e)
{
DateTime dt;
if (DateTime.TryParseExact(e.Row.Cells[2].Text, "yyyyMMddHHmm",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
if (dt <= DateTime.Now.AddHours(test * -1))
{
// ...
【问题讨论】:
-
什么是
-<%#test%>??如果是 ASP.NET,则添加该标记并显示更多上下文。 -
目前还不清楚你在哪里使用代码
DateTime.Now.AddHours(-<%#test%>) -
但是仍然在顶部的代码在哪里:
DateTime.Now.AddHours(-<%#test%>)?现在看来您实际上正在使用DateTime.Now.AddHours(test * -1)。此外,如果您从Page_Load初始化 局部变量test,您将无法从RowDataBound访问它。请改用字段/属性。 -
问题是 test 是局部变量
-
你必须在你的数据库操作类中创建一个属性或者通过返回类型为int的方法返回它,如果它在同一个类但不同的方法,那么请看我更新的帖子