我正在为一个项目创建一个网站,但不断收到此错误。网站的此部分是注册页面,访问者在此输入他们的信息,然后代码会将其写入我连接的数据库中。我正在使用Visual Studio,并且我的网页是.cshtml文件。我正在使用的execute命令如下:
db.Execute(insertCommand, name, email, number, address, psw, psw_repeat);
这是完整的数据库代码:
@{
var name = "";
var email = "";
var number = "";
var address = "";
var psw = "";
var psw_repeat = "";
if (IsPost)
{
name = @Request.Form["name"];
email = @Request.Form["email"];
address = @Request.Form["address"];
psw = @Request.Form["psw"];
psw = @Request.Form["psw_repeat"];
var db = Database.Open("UserDetails");
var insertCommand = "INSERT INTO [dbo.details] (Name, Email, Number, DeliveryAddress, Password, RepeatPassword) VALUES(@name, @email, @number, @address, @psw, @psw_repeat)";
db.Execute(insertCommand, name, email, number, address, psw, psw_repeat);
}
}
当我尝试运行网页时,一切正常,我可以输入详细信息。当我单击提交按钮时,将显示此错误:
“ /”应用程序中的服务器错误。与网络相关或 建立与以下对象的连接时发生特定于实例的错误 SQL Server。服务器未找到或无法访问。校验 实例名称正确并且已将SQL Server配置为 允许远程连接。 (提供者:SQL网络接口,错误:26 -错误定位指定的服务器/实例)
此行以红色突出显示:
db.Execute(insertCommand, name, email, number, address, psw, psw_repeat);
我看过许多类似的问题,并尝试添加:
<system.web>
<customErrors mode="Off"/>
在我的web.config文件中,但这没有帮助。
请帮助我了解问题所在以及可能的解决方案。 谢谢。
编辑: 经过进一步研究,我尝试在Microsoft SQL Server Management Studio中打开数据库。输入服务器名称:“ RM48-11”时,我再次收到此错误:
TITLE: Connect to Server
------------------------------
Cannot connect to RM48-11.
------------------------------
ADDITIONAL INFORMATION:
A network-related or instance-specific error occurred while establishing a
connection to SQL Server. The server was not found or was not accessible.
Verify that the instance name is correct and that SQL Server is configured
to allow remote connections. (provider: Named Pipes Provider, error: 40 -
Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 2)
For help, click: http://go.microsoft.com/fwlink?
ProdName=Microsoft%20SQL%20Server&EvtSrc=MSSQLServer&EvtID=2&LinkId=20476
------------------------------
The system cannot find the file specified
------------------------------
BUTTONS:
OK
------------------------------
完整代码:
@using WebMatrix.Data;
@using System.Data.SQLClient;
@{
var name = "";
var email = "";
var number = "";
var address = "";
var psw = "";
var psw_repeat = "";
if (IsPost)
{
name = @Request.Form["name"];
email = @Request.Form["email"];
address = @Request.Form["address"];
psw = @Request.Form["psw"];
psw = @Request.Form["psw_repeat"];
var db = Database.Open("UserDetails");
var insertCommand = "INSERT INTO [dbo.details] (Name, Email, Number,
DeliveryAddress, Password, RepeatPassword) VALUES(@name, @email, @number,
@address, @psw, @psw_repeat)";
db.Execute(insertCommand, name, email, number, address, psw,
psw_repeat);
}
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
* {
box-sizing: border-box;
}
.container {
padding: 0px;
background-color: white;
}
input[type=text], input[type=password] {
width: 100%;
padding: 15px;
margin: 10px 0 5px 0;
display: inline-block;
border: none;
background: #f1f1f1;
}
input[type=text]:focus, input[type=password]:focus {
background-color: #ddd;
outline: none;
}
hr {
border: 1px solid #f1f1f1;
margin-bottom: 5px;
}
.registerbtn {
background-color: #4CAF50;
color: white;
padding: 16px 20px;
margin: 4px 0;
border: none;
cursor: pointer;
width: 100%;
opacity: 0.9;
}
.registerbtn:hover {
opacity: 1;
}
a {
color: dodgerblue;
}
</style>
</head>
<body>
<form id="form1" action="" method="post">
<div class="container">
<h1>Register</h1>
<p>Please fill in this form to create an account.</p>
<hr>
<label for="name"><b>Name</b></label>
<input type="text" name="name" value="@Request.Form["name"]" />
<label for="email"><b>Email</b></label>
<input type="text" name="email" value="@Request.Form["email"]" />
<label for="number"><b>Phone Number</b></label>
<input type="text" name="number" value="@Request.Form["number"]" />
<label for="address"><b>Delivery Address</b></label>
<input type="text" name="address" value="@Request.Form["address"]" />
<label for="psw"><b>Password</b></label>
<input type="text" name="psw" value="@Request.Form["psw"]" />
<label for="psw_repet"><b>Repeat Password</b></label>
<input type="text" name="psw_repeat" value="@Request.Form["psw_repeat"]" />
<input type="submit" name="buttonSubmit" value="Register">
</div>
<div class="container signin">
<p>Already have an account? <a href="login.aspx">Sign in</a></p>
</div>
</form>