[asp.net](试图连接到数据库的网站) 的视觉studio网页代码中的错误

发布时间: 2016/7/17 0:58:13
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想创建一个网站 (visual studio) 并将其连接到数据库 (微软 sql 服务器 2014年) 第一次我正在学习从一个视频在 youtube 上本教程是关于咖啡的网站和数据库

我有几个错误,但我不知道问题的症结所在

在此页中的错误Coffe.aspx.cs

这里是页面 (Coffee.aspx.cs) 代码

using System;
using System.Collections;
using System.Text;

namespace Pages
{
    public partial class Pages_Coffee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FillPage();
        }

        private void FillPage()
        {
            ArrayList coffeeList = ConnectionClass.GetCoffeeByType(DropDownList1.SelectedValue);
            StringBuilder sb = new StringBuilder();

            foreach (Coffee coffee in coffeeList)
            {
                sb.Append(
                    string.Format(
                        @"<table class='coffeeTable'>
            <tr>
                <th rowspan='6' width='150px'><img runat='server' src='{6}' /></th>
                <th width='50px'>Name: </td>
                <td>{0}</td>
            </tr>

            <tr>
                <th>Type: </th>
                <td>{1}</td>
            </tr>

            <tr>
                <th>Price: </th>
                <td>{2} $</td>
            </tr>

            <tr>
                <th>Roast: </th>
                <td>{3}</td>
            </tr>

            <tr>
                <th>Origin: </th>
                <td>{4}</td>
            </tr>

            <tr>
                <td colspan='2'>{5}</td>
            </tr>           

           </table>", coffee.Name, coffee.Type, coffee.Price, coffee.Roast, coffee.Country, coffee.Review, coffee.Image));
                lblOuput.Text = sb.ToString();
            }
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillPage();
        }
    }
}

tis 也是 (Coffee.aspx)code 页

<%@ Page Title="" Language="C#" MasterPageFile="~/Masterpage.master" AutoEventWireup="true" CodeFile="Coffee.aspx.cs" Inherits="Coffee" %>

<script runat="server">

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
</script>


<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p>
        Select a type:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="sds_type" DataTextField="type" DataValueField="type" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:SqlDataSource ID="sds_type" runat="server" ConnectionString="<%$ ConnectionStrings:CoffeeDBConnectionString %>" SelectCommand="SELECT DISTINCT [type] FROM [coffee] ORDER BY [type]"></asp:SqlDataSource>
    </p>
    <p>
        <asp:Label ID="lblOutput" runat="server" Text="Label"></asp:Label>
    </p>
</asp:Content>

这是 th (ConnectionClass.cs)code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;

public static class ConnectionClass
{
    private static SqlConnection conn;
    private static SqlCommand command;

    static ConnectionClass()
    {
        string connectionString = 
            ConfigurationManager.ConnectionStrings["coffeeConnection"].ToString();
        conn = new SqlConnection(connectionString);
        command = new SqlCommand("", conn);
    }

    public static ArrayList GetCoffeeByType(string coffeeType)
    {
        ArrayList list = new ArrayList();
        string query = string.Format("SELECT * FROM coffee WHERE type LIKE '{0}'", coffeeType);

        try
        {
            conn.Open();
            command.CommandText = query;
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                int id = reader.GetInt32(0);
                string name = reader.GetString(1);
                string type = reader.GetString(2);
                double price = reader.GetDouble(3);
                string roast = reader.GetString(4);
                string country = reader.GetString(5);
                string image = reader.GetString(6);
                string review = reader.GetString(7);

                Coffee coffee = new Coffee(id, name, type, price, roast, country, image, review);
                list.Add(coffee);
            }
        }
        finally
        {
            conn.Close();
        }

        return list;
    }

}

(web.confg)code

    <?xml version="1.0"?>
    <configuration>
      <appSettings/>

      <connectionStrings>
        <clear/>
        <add name="coffeeConnection" 
             connectionString="Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=CoffeeDB;Integrated Security=True" 
             providerName="System.Data.SqlClient"/>
      </connectionStrings>

      <system.web>
        <compilation debug="true" targetFramework="4.0">
          <assemblies>
            <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
            <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
          </assemblies>
        </compilation>
        <authentication mode="Windows"/>
      </system.web>

    </configuration>

and (Coffee.cs)code

public class Coffee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public double Price { get; set; }
    public string Roast { get; set; }
    public string Country { get; set; }
    public string Image { get; set; }
    public string Review { get; set; }

    public Coffee(int id, string name, string type, double price, string roast, string country, string image, string review)
    {
        Id = id;
        Name = name;
        Type = type;
        Price = price;
        Roast = roast;
        Country = country;
        Image = image;
        Review = review;
    }
}

最后,这是 SQL 代码

GO
CREATE TABLE [dbo].[coffee](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](50) NOT NULL,
    [type] [varchar](50) NOT NULL,
    [price] [float] NOT NULL,
    [roast] [varchar](50) NOT NULL,
    [country] [varchar](50) NOT NULL,
    [image] [varchar](255) NULL,
    [review] [text] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[coffee] ON
INSERT [dbo].[coffee] ([id], [name], [type], [price], [roast], [country], [image], [review]) VALUES (1, N'Café au Lait', N'Classic', 2.25, N'Medium', N'France', N'../Images/Coffee/Cafe-Au-Lait.jpg', N'A coffee beverage consisting strong or bold coffee (sometimes espresso) mixed with scalded milk in approximately a 1:1 ratio.')
INSERT [dbo].[coffee] ([id], [name], [type], [price], [roast], [country], [image], [review]) VALUES (2, N'Caffè Americano', N'Espresso', 2.25, N'Medium', N'Italy', N'../Images/coffee/caffe_americano.jpg', N'Similar in strength and taste to American-style brewed coffee, there are subtle differences achieved by pulling a fresh shot of espresso for the beverage base.')
INSERT [dbo].[coffee] ([id], [name], [type], [price], [roast], [country], [image], [review]) VALUES (3, N'Peppermint White Chocolate Mocha', N'Espresso', 3.25, N'Medium', N'Italy', N'../Images/coffee/white-chocolate-peppermint-mocha.jpg', N'Espresso with white chocolate and peppermint flavored syrups.
Topped with sweetened whipped cream and dark chocolate curls.')
INSERT [dbo].[coffee] ([id], [name], [type], [price], [roast], [country], [image], [review]) VALUES (4, N'Irish Coffee', N'Alcoholic', 2.25, N'Dark', N'Ireland', N'../Images/coffee/irish coffee.jpg', N'A cocktail consisting of hot coffee, Irish whiskey, and sugar, stirred, and topped with thick cream. The coffee is drunk through the cream.')
SET IDENTITY_INSERT [dbo].[coffee] OFF

我仍然是个初学者 ~ ~ 我需要了解什么是这些错误以及如何解决这些问题

解决方法 1:

好的它看起来像你创建页面名称 Coffee ,然后将代码隐藏文件中的类重命名为 Page_Coffee 或也许其他的方式。 无论哪种方式的问题是,在 Coffee.aspx 文件中,你有这

CodeFile="Coffee.aspx.cs" Inherits="Coffee"

但在 Coffee.aspx.cs 文件中,您将声明像这类

public partial class Pages_Coffee : System.Web.UI.Page

如果你同步的类名称继承的名字,它应解决你要编译错误。

public partial class Coffee : System.Web.UI.Page
官方微信
官方QQ群
31647020