在asp.net中写一个简单的基于c#的web与sql数据库相连的旅游管理系统的动态网站
时间: 2024-03-20 13:41:40 浏览: 77
这是一个简单的基于 C# 和 SQL 数据库的旅游管理系统的动态网站示例。该网站使用 ASP.NET Web Forms 模型,使用 SQL Server 数据库存储数据。
首先,您需要创建一个 ASP.NET Web Forms 项目,并设置好连接到 SQL Server 数据库的连接字符串。可以在 Web.config 文件中设置连接字符串。
接下来,您需要创建一个数据库表来存储旅游信息。以下是一个示例 SQL 脚本,用于创建一个名为“Tour” 的表:
```sql
CREATE TABLE Tour (
TourId INT PRIMARY KEY IDENTITY,
TourName VARCHAR(50) NOT NULL,
TourDescription VARCHAR(500) NOT NULL,
TourPrice DECIMAL(10, 2) NOT NULL,
TourImageUrl VARCHAR(200) NOT NULL
)
```
下面是一个示例 ASP.NET 页面,用于显示旅游信息并允许用户添加新的旅游:
```csharp
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Tour.aspx.cs" Inherits="MyWebApp.Tour" %>
<!DOCTYPE html>
<html>
<head>
<title>Tour Management</title>
</head>
<body>
<form id="form1" runat="server">
<h1>Tour Management</h1>
<hr />
<h2>Add New Tour</h2>
<div>
<label>Tour Name:</label>
<asp:TextBox ID="txtTourName" runat="server"></asp:TextBox>
</div>
<div>
<label>Tour Description:</label>
<asp:TextBox ID="txtTourDescription" runat="server" TextMode="MultiLine"></asp:TextBox>
</div>
<div>
<label>Tour Price:</label>
<asp:TextBox ID="txtTourPrice" runat="server"></asp:TextBox>
</div>
<div>
<label>Tour Image URL:</label>
<asp:TextBox ID="txtTourImageUrl" runat="server"></asp:TextBox>
</div>
<div>
<asp:Button ID="btnAddTour" runat="server" Text="Add Tour" OnClick="btnAddTour_Click" />
</div>
<hr />
<h2>Tours</h2>
<asp:GridView ID="gvTours" runat="server" AutoGenerateColumns="False" DataKeyNames="TourId"
OnRowDeleting="gvTours_RowDeleting">
<Columns>
<asp:BoundField DataField="TourId" HeaderText="Tour ID" ReadOnly="True" />
<asp:BoundField DataField="TourName" HeaderText="Tour Name" />
<asp:BoundField DataField="TourDescription" HeaderText="Tour Description" />
<asp:BoundField DataField="TourPrice" HeaderText="Tour Price" />
<asp:BoundField DataField="TourImageUrl" HeaderText="Tour Image URL" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
```
在代码文件 Tour.aspx.cs 中,您需要编写逻辑来处理添加新旅游和删除旅游的操作。以下是一个示例代码:
```csharp
using System;
using System.Configuration;
using System.Data.SqlClient;
namespace MyWebApp
{
public partial class Tour : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindTourData();
}
}
protected void btnAddTour_Click(object sender, EventArgs e)
{
string tourName = txtTourName.Text;
string tourDescription = txtTourDescription.Text;
decimal tourPrice = decimal.Parse(txtTourPrice.Text);
string tourImageUrl = txtTourImageUrl.Text;
string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
string query = "INSERT INTO Tour (TourName, TourDescription, TourPrice, TourImageUrl) VALUES (@TourName, @TourDescription, @TourPrice, @TourImageUrl)";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@TourName", tourName);
command.Parameters.AddWithValue("@TourDescription", tourDescription);
command.Parameters.AddWithValue("@TourPrice", tourPrice);
command.Parameters.AddWithValue("@TourImageUrl", tourImageUrl);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
BindTourData();
}
protected void gvTours_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
{
int tourId = int.Parse(gvTours.DataKeys[e.RowIndex].Value.ToString());
string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
string query = "DELETE FROM Tour WHERE TourId = @TourId";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@TourId", tourId);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
BindTourData();
}
private void BindTourData()
{
string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
string query = "SELECT * FROM Tour";
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
gvTours.DataSource = reader;
gvTours.DataBind();
reader.Close();
connection.Close();
}
}
}
```
这只是一个简单的示例,您可以根据需要添加更多的功能和逻辑来创建更复杂的旅游管理系统。
阅读全文