ASP.net无法附加文件作为数据库 [英] ASP.net unable to attach file as database
问题描述
我是一个数据输入窗体的数据库上使用C#和ASP.net在Visual Studio 2013年的工作我试图建立一个表格,让我进入任天堂电力杂志评出的30结果轮询成在数据库中的表。
I'm working on a data entry form for a database using C# and ASP.net in Visual Studio 2013. What I'm trying to build is a form that would allow me to enter the results for Nintendo Power Magazine's Top 30 poll into a table in the database.
有关问题的页面的计划是有一个GridView这样我就可以看到什么样的结果,我已输入,并且将被用于实际的数据输入一个FormView。为了使这个过程更简单,我通过一个下拉菜单中的问题和游戏,并避免对NES的游戏男孩混乱唐老鸭俱乐部和唐老鸭俱乐部,我已经设置了一个额外的下拉菜单选择一个游戏已经发布了什么系统。该菜单的结果,反过来,将决定比赛将在游戏下拉列表中的内容。
The plan for the page in question is to have a GridView so I can see what results that I've already entered, and a FormView which will be used for the actual data entry. To make the process simpler I'm the issue and game through a drop-down menu, and to avoid confusing "Duck Tales" for the NES and "Duck Tales" for the Game Boy, I've set up an additional drop-down menu to select what system a game has been released on. The results of that menu, in turn, will determine what games will be in the "Games" drop down list.
所以,我几乎完成把这个表格连同 - 然而,测试形式时,我遇到了一个问题。当我去请从下拉菜单中选择一个系统,我得到这个错误信息:
So, I've almost finished putting this form together - however, I'm running into a problem when testing the form. When I go to select a system from the drop-down menu, I get this error message:
无法附加文件C:\\用户\\ TEST \\文档\\ Visual Studio中2013\\WebSites\\NPPollDataEntry\\App_Data\\aspnet-NPPollDataEntry-14ef47ad-43d0-4a7d-b1c9-de351dbca3d5.mdf'数据库ASPNET-NPPollDataEntry-14ef47ad-43d0-4a7d-b1c9-de351dbca3d5。
Cannot attach the file 'C:\Users\TEST\Documents\Visual Studio 2013\WebSites\NPPollDataEntry\App_Data\aspnet-NPPollDataEntry-14ef47ad-43d0-4a7d-b1c9-de351dbca3d5.mdf' as database 'aspnet-NPPollDataEntry-14ef47ad-43d0-4a7d-b1c9-de351dbca3d5'.
我在做什么错了?
窗体页:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ResultsEntryForm.aspx.cs" Inherits="ResultsEntryForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="ResultsDataEntryForm" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="PollResultsDataSource"
AllowSorting="True">
<Columns>
<asp:BoundField DataField="Issue_Date" HeaderText="Issue_Date"
SortExpression="Issue_Date" />
<asp:BoundField DataField="Platform_Type" HeaderText="Platform_Type"
SortExpression="Platform_Type" />
<asp:BoundField DataField="Element_Title" HeaderText="Element_Title"
SortExpression="Element_Title" />
<asp:BoundField DataField="Poll_Score" HeaderText="Poll_Score"
SortExpression="Poll_Score" />
</Columns>
</asp:GridView>
<asp:FormView ID="ResultsFormView" runat="server" AllowPaging="True"
DataSourceID="PollResultsDataSource">
<InsertItemTemplate>
Issue_Date:
<asp:DropDownList ID="IssueDateDropDownList" runat="server" AutoPostBack="True"
DataSourceID="IssueDateDropDownDataSource" DataTextField="Issue_Date"
DataValueField="Issue_ID">
</asp:DropDownList>
<br />
Platform_Type:
<asp:DropDownList ID="PlatformDropDownList" runat="server"
DataSourceID="PlatformDropDownDataSource" DataTextField="Platform_Type"
DataValueField="Platform_ID" AutoPostBack="True"
OnSelectedIndexChanged="PlatformDropDownList_SelectedIndexChanged">
</asp:DropDownList>
<br />
Element_Title:
<asp:DropDownList ID="TitleDropDownList" runat="server" AutoPostBack="True">
</asp:DropDownList>
<br />
Poll_Score:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" ValidationGroup="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
Issue_Date:
<asp:Label ID="Issue_DateLabel" runat="server"
Text='<%# Bind("Issue_Date") %>' />
<br />
Platform_Type:
<asp:Label ID="Platform_TypeLabel" runat="server"
Text='<%# Bind("Platform_Type") %>' />
<br />
Element_Title:
<asp:Label ID="Element_TitleLabel" runat="server"
Text='<%# Bind("Element_Title") %>' />
<br />
Poll_Score:
<asp:Label ID="Poll_ScoreLabel" runat="server"
Text='<%# Bind("Poll_Score") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
</asp:FormView>
</div>
<asp:SqlDataSource ID="PollResultsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>"
InsertCommand="INSERT INTO Poll_Results(Issue_ID, Element_Group_ID, Poll_Score) VALUES (@Issue_ID, @Element_Group_ID, @Poll_Score)"
SelectCommand="SELECT NintendoPowerIssue.Issue_Date, na_lkpPlatformTypes.Platform_Type, na_Games.Element_Title, Poll_Results.Poll_Score
FROM Poll_Results INNER JOIN NintendoPowerIssue ON Poll_Results.Issue_ID = NintendoPowerIssue.Issue_ID
INNER JOIN na_Games ON Poll_Results.Element_Group_ID = na_Games.Element_Group_ID
INNER JOIN na_lkpPlatformTypes ON na_Games.Platform_ID = na_lkpPlatformTypes.Platform_ID"
UpdateCommand="UPDATE Poll_Results SET Poll_Score = @Poll_Score
FROM Poll_Results
INNER JOIN na_Games ON Poll_Results.Element_Group_ID = na_Games.Element_Group_ID
INNER JOIN NintendoPowerIssue ON Poll_Results.Issue_ID = NintendoPowerIssue.Issue_ID
WHERE (Poll_Results.Issue_ID = @Issue_ID) AND (Poll_Results.Element_Group_ID = @Element_Group_ID)">
<InsertParameters>
<asp:Parameter Name="Issue_ID" />
<asp:Parameter Name="Element_Group_ID" />
<asp:Parameter Name="Poll_Score" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Poll_Score" />
<asp:Parameter Name="Issue_ID" />
<asp:Parameter Name="Element_Group_ID" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="IssueDateDropDownDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>"
SelectCommand="SELECT [Issue_ID], [Issue_Date] FROM [NintendoPowerIssue]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="PlatformDropDownDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:NintendoPowerPollConnectionString %>"
SelectCommand="SELECT [Platform_ID], [Platform_Type] FROM [na_lkpPlatformTypes]" />
</form>
</body>
</html>
返回code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class ResultsEntryForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private DataTable BindDropDownList(string field)
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
try
{
connection.Open();
string sqlStatement = "SELECT [Element_Group_ID], [Element_Title] FROM [na_Games] WHERE ([Platform_ID] = @Platform_ID)";
SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlCmd.Parameters.AddWithValue("@Platform_ID", field);
sqlDa.Fill(dt);
}
catch (System.Data.SqlClient.SqlException ex)
{
Server.ClearError();
Response.Write(ex.Message + ("<br />") + ex.Source);
}
finally
{
connection.Close();
}
return dt;
}
protected void PlatformDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl1 = (DropDownList)sender;
FormView fv = (FormView)ddl1.NamingContainer;
if (fv != null)
{
DropDownList ddl2 = (DropDownList)fv.FindControl("TitleDropDownList");
{
DataTable dt = BindDropDownList(ddl1.SelectedItem.Value);
ddl2.DataTextField = "Field1";
ddl2.DataValueField = "Field2";
ddl2.DataBind();
}
}
}
}
在此先感谢您的帮助。
Thanks in advance for your help.
推荐答案
我想问题可能是一样的这样一个问题:
I think the problem might be the same as in this question:
MDF文件需要连接到SQL Server实例。
MDF files need to be attached to a sql server instance.
这篇关于ASP.net无法附加文件作为数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!