从现有代码、当前日期绑定网格视图数据
Bind grid view data from existing code, current date
我想使用下面的代码并在 GridView 中显示。
情况是,当用户单击 GET CURRENT DATE TRANS 时,GridView 将显示今天的日期作为结果。我已经将 GridView id 插入为 GridView1
string connetionString = null;
SqlConnection connection;
SqlCommand command;
string sql = null;
connetionString = "Data Source=AXSQL;Initial Catalog=UniKL;User ID=aten;Password=pass@WORD1";
sql = "Select * FROM [UniKL].[dbo].[BudgetPlanning_Transaction] WHERE [SubmmitedDateTime] = cast(getdate() as date)";
GridView1.DataBind();
connection = new SqlConnection(connetionString);
connection.Open();
command = new SqlCommand(sql, connection);
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
一个建议是更改:
sql = "Select * FROM [UniKL].[dbo].[BudgetPlanning_Transaction] WHERE [SubmmitedDateTime] = cast(getdate() as date)";
类似于:
sql = "Select * FROM [UniKL].[dbo].[BudgetPlanning_Transaction] WHERE [SubmmitedDateTime] = " + DateTime.Now.toShortDateString();
或
sql = "Select * FROM [UniKL].[dbo].[BudgetPlanning_Transaction] WHERE [SubmmitedDateTime] = " + DateTime.Now.toString("yyyy-MM-dd");
我猜你的列 SubmittedDateTime
是 sql 中的 DateTime
类型,因此你还需要 cast
date
中的此列以匹配当前这样的约会
cast([SubmmitedDateTime] as Date) = cast(getdate() as date);
因此您的查询将如下所示
sql = "Select * FROM [UniKL].[dbo].[BudgetPlanning_Transaction] WHERE cast([SubmmitedDateTime] as Date) = cast(getdate() as date)";
您没有将数据源分配给 GridView1。请使用以下代码:
string connetionString = null;
SqlConnection connection;
SqlCommand command;
string sql = null;
connetionString = "Data Source=AXSQL;Initial Catalog=UniKL;User ID=aten;Password=pass@WORD1";
sql = "Select * FROM [UniKL].[dbo].[BudgetPlanning_Transaction] WHERE [SubmmitedDateTime] = cast(getdate() as date)";
//GridView1.DataBind();
connection = new SqlConnection(connetionString);
connection.Open();
command = new SqlCommand(sql, connection);
//Get data into a reader
SqlDataReader sr = command.ExecuteReader();
//command.ExecuteNonQuery();
//Set the datasource of GridView1
GridView1.DataSource = sr;
GridView1.DataBind();
command.Dispose();
connection.Close();
我想使用下面的代码并在 GridView 中显示。 情况是,当用户单击 GET CURRENT DATE TRANS 时,GridView 将显示今天的日期作为结果。我已经将 GridView id 插入为 GridView1
string connetionString = null;
SqlConnection connection;
SqlCommand command;
string sql = null;
connetionString = "Data Source=AXSQL;Initial Catalog=UniKL;User ID=aten;Password=pass@WORD1";
sql = "Select * FROM [UniKL].[dbo].[BudgetPlanning_Transaction] WHERE [SubmmitedDateTime] = cast(getdate() as date)";
GridView1.DataBind();
connection = new SqlConnection(connetionString);
connection.Open();
command = new SqlCommand(sql, connection);
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
一个建议是更改:
sql = "Select * FROM [UniKL].[dbo].[BudgetPlanning_Transaction] WHERE [SubmmitedDateTime] = cast(getdate() as date)";
类似于:
sql = "Select * FROM [UniKL].[dbo].[BudgetPlanning_Transaction] WHERE [SubmmitedDateTime] = " + DateTime.Now.toShortDateString();
或
sql = "Select * FROM [UniKL].[dbo].[BudgetPlanning_Transaction] WHERE [SubmmitedDateTime] = " + DateTime.Now.toString("yyyy-MM-dd");
我猜你的列 SubmittedDateTime
是 sql 中的 DateTime
类型,因此你还需要 cast
date
中的此列以匹配当前这样的约会
cast([SubmmitedDateTime] as Date) = cast(getdate() as date);
因此您的查询将如下所示
sql = "Select * FROM [UniKL].[dbo].[BudgetPlanning_Transaction] WHERE cast([SubmmitedDateTime] as Date) = cast(getdate() as date)";
您没有将数据源分配给 GridView1。请使用以下代码:
string connetionString = null;
SqlConnection connection;
SqlCommand command;
string sql = null;
connetionString = "Data Source=AXSQL;Initial Catalog=UniKL;User ID=aten;Password=pass@WORD1";
sql = "Select * FROM [UniKL].[dbo].[BudgetPlanning_Transaction] WHERE [SubmmitedDateTime] = cast(getdate() as date)";
//GridView1.DataBind();
connection = new SqlConnection(connetionString);
connection.Open();
command = new SqlCommand(sql, connection);
//Get data into a reader
SqlDataReader sr = command.ExecuteReader();
//command.ExecuteNonQuery();
//Set the datasource of GridView1
GridView1.DataSource = sr;
GridView1.DataBind();
command.Dispose();
connection.Close();