我是 C#、ASP.NET、SQL Server Express 和一般编程的初学者。我正在尝试制作在线日历(在截止日期前)。我会阅读与此类似的所有其他帖子,但现在我有一个问题,我希望它能引导我前进。
将日历链接到数据库的步骤是什么?如果能举出在何处/如何完成此操作的任何示例,我们将不胜感激?
当一个日期有多个不同用户输入时,我发现可能存在陷阱,所以如果有人知道如何再次管理它,我会洗耳恭听。
最佳答案
我给你写了一个小例子来说明
- 连接到 SQL 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);
}
祝你好运!
关于c# - 日历数据库架构的最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1062527/