问题描述
我在做一个学习的项目中,我想HTML +剃须刀结合起来,把它连接到数据库。
I am doing a learning project in which i wanted to combine Html+Razor and connect it to Database.
步骤
-
创建了一个示例表(我打算用它来执行插入和更新查询)
Created a sample form ( which i intend to use to execute "insert" and "update" queries )
创建的数据库(名称:TestDatabase.mdf,位于里面的App_Data)
Created Database (Name: TestDatabase.mdf, located inside App_Data)
把一些样本值在数据库中的表名学生
Put in some sample values in the database with Table name "Student"
更新webconfig文件如下(ConnectionString的是由数据库浏览器复制它获得)
updated the webconfig file as follows (ConnectionString was obtained by copying it from Database explorer)
<connectionStrings>
<add name="TestDatabase" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\TestDatabase.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
插入剃刀code连接如下:
Inserted the Razor code for connection as follows
@{
var db = Database.Open("TestDatabase");
var query = "Select * Student";
}
<html>
<head>
<title>Testing For Database Connectivity</title>
</head>
<body>
<div>
<fieldset>
<legend>Some Sample Form</legend>
<form>
<div>
<label for="FirstName">FirstName</label>
<input type="text" name="FirstName" />
</div>
<div>
<label for="LastName">LastName</label>
<input type="text" name="LastName" />
</div>
<div>
<label for="SSN">SSN</label>
<input type="text" name="SSN" />
</div>
<div>
<input type="submit" value="submit" />
</div>
<div>
@foreach (var row in db.Query(query))
{
@row.FirstName
<br />
}
</div>
</form>
</fieldset>
</div>
</body>
</html>
但我收到以下错误
堆栈跟踪
[ArgumentException: Keyword not supported: 'attachdbfilename'.]
System.Data.SqlServerCe.SqlCeConnectionStringBuilder.GetIndex(String keyword) +192
System.Data.SqlServerCe.SqlCeConnectionStringBuilder.set_Item(String keyword, Object value) +31
System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value) +185
System.Data.SqlServerCe.SqlCeConnectionStringBuilder..ctor(String connectionString) +177
System.Data.SqlServerCe.SqlCeConnection.set_ConnectionString(String value) +239
WebMatrix.Data.DbProviderFactoryWrapper.CreateConnection(String connectionString) +96
WebMatrix.Data.<>c__DisplayClass15.<OpenConnectionStringInternal>b__14() +16
WebMatrix.Data.Database.get_Connection() +19
WebMatrix.Data.Database.EnsureConnectionOpen() +12
WebMatrix.Data.<QueryInternal>d__0.MoveNext() +71
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +327
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
WebMatrix.Data.Database.Query(String commandText, Object[] parameters) +103
ASP._Page_TestPage_cshtml.Execute() in d:\ASP.NET\RefreshTesting\TestPage.cshtml:33
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +209
System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +68
System.Web.WebPages.WebPage.ExecutePageHierarchy() +152
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +121
错误页图片
我想是非常明了,信息丰富,但如果我错过了提东西,那么请让我知道。结果
我现在不知道我错了或者我想提出一个愚蠢的错误,现在我甚至怀疑这是否可行与否。我是新来微软的语言和我曾经看过一对夫妇其中使用一些MVC的东西去实现它Youtube视频。是有可能做到这一点我已经做的方式?如果是,请帮助我,如果没有,那样的话是我可能的选择?
I am trying to be very clear and informative but if i have missed out to mention something then please let me know.
I don't know now where i am going wrong or i am making a silly mistake and now i am even doubting that whether this is possible or not. I am new to microsoft languages and i have watched a couple of Youtube videos which use some MVC thing to achieve it. is it possible to do it the way i have done ?? If yes please help me and if NO then what could be my possible options?
推荐答案
当你创建的SQL Server数据库,那么你应该将的providerName 为 System.Data.SqlClient的。
As you created Sql Server Database, then you should set providerName as System.Data.SqlClient.
和我没有看到你的初始目录
设置,我的意思是你必须设置你连接到的数据库。
And I don't see your Initial Catalog
setting, I mean you have to set which database you would connect to.
能否请您尝试添加连接字符串这样?
Could you please try to add connection string like this?
<connectionStrings>
<add name="TestDatabase" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DBName;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\TestDatabase.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
其中,数据库名
是要连接的数据库名称。
where DBName
is database name which you want to connect.
这篇关于连接HTML页面+剃刀数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!