【发布时间】:2010-11-06 22:41:36
【问题描述】:
我是 C#、ASP.NET、SQL Server Express 和一般编程的初学者。我正在尝试制作在线日历(在截止日期前)。我会阅读与此类似的所有其他帖子,但现在我有一个问题希望能帮助我。
将日历链接到数据库的步骤是什么?任何有关在何处/如何完成此操作的示例将不胜感激?
当一个日期有多个不同用户的条目时,我发现可能存在陷阱,所以如果有人知道如何再次管理它,我会全力以赴。
【问题讨论】:
我是 C#、ASP.NET、SQL Server Express 和一般编程的初学者。我正在尝试制作在线日历(在截止日期前)。我会阅读与此类似的所有其他帖子,但现在我有一个问题希望能帮助我。
将日历链接到数据库的步骤是什么?任何有关在何处/如何完成此操作的示例将不胜感激?
当一个日期有多个不同用户的条目时,我发现可能存在陷阱,所以如果有人知道如何再次管理它,我会全力以赴。
【问题讨论】:
我给你写了一个小例子来说明
希望这会有所帮助!...
// The SQL connection object to connect to the database. Takes connection string.
SqlConnection connection =
new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=Example");
// Idealy you wouldnt pass direct SQL text for injection reasons etc, but
// for example sake I will enter a simple query to get some dates from a table.
// Notice the command object takes our connection object...
SqlCommand command = new SqlCommand("Select [Date] From Dates", connection);
// Open the connection
connection.Open();
// Execute the command
SqlDataReader reader = command.ExecuteReader();
// A list of dates to store our selected dates...
List<DateTime> dates = new List<DateTime>();
// While the SqlDataReader is reading, add the date to the list.
while (reader.Read())
{
dates.Add(reader.GetDateTime(0));
}
// Close the connection!
connection.Close();
// Use the selected dates property of the ASP.NET calendar control, add every
// date that we read from the database...
foreach (DateTime date in dates)
{
Calendar1.SelectedDates.Add(date);
}
祝你好运!
【讨论】:
不知道这是否有帮助,但CodeProject 似乎有一些类似的解决方案
【讨论】:
【讨论】:
好像有人已经问过这个问题了。 Laying out a database schema for a calendar application
一个建议是使用整数字段而不是日期时间字段。他们会跑得更快。也不要害怕去规范化你的表格。所以你可能有一个日期列、年份列、月份列和日期列。
【讨论】: