初学者,使用数据库的.net C#
本文关键字:net 数据库 初学者 | 更新日期: 2023-09-27 18:32:03
几天来我一直在尝试能够使用带有简单 web 基础 .net 项目的数据库。我成功地遵循了使用SQL compact的教程,但有人建议我使用SQLEXPRESS,因为它可以创建.mdf文件,与VISUAL STUDIO相同。
出于沮丧,我重新安装了以下的干净副本:视窗 10Visual Studio Community 2015SQL Server Express(2014).注意:除非登录身份:设置为内置帐户,本地系统,否则不会启动。这是一个问题?
这是我的代码:[代码]
@using WebMatrix.Data;
@using System.Data.SqlClient
@{
}
@{
var Name = "";
var Position = "";
var Department = "";
var Shift = "";
if (IsPost)
{
Name = Request.Form["Name"];
Position = Request.Form["Position"];
Department = Request.Form["Department"];
Shift = Request.Form["Shift"];
String ConnStr = "Data Source=.''SQLEXPRESS;Initial Catalog='"C: ''Users''Tevis Cox''Documents''Visual Studio 2015''Projects''TutorialPages''TutorialPages''App_Data''devDB.mdf'";Integrated Security=True; MultipleActiveResultSets=True";
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO Employees(EmployeeName, Position, Department, Shift) VALUES("+ Name + "," + Position + "," + Department +"," + Shift + ")";
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Add Personnel</title>
</head>
<body>
<h1>Add Personnel</h1>
<form method="post">
<fieldset>
<legend>Personnel Information</legend>
<p>
<label for="Name">Name:</label>
<input type="text" name="Name" value="@Request.Form["Name"]" />
</p>
<p>
<label for="Position">Position:</label>
<input type="text" name="Position" value="@Request.Form["Position"]" />
</p>
<p>
<label for="Department">Department:</label>
<input type="text" name="Department" value="@Request.Form["Department"]" />
</p>
<p>
<label for="Shift">Shift:</label>
<input type="text" name="Shift" value="@Request.Form["Shift"]" />
</p>
<p><input type="submit" name="buttonSubmit" value="Add" /></p>
</fieldset>
</form>
[/代码]是的,我知道这容易受到SQL注入攻击。它将得到解决。
此设置的当前错误指出登录失败。这是在我使用 SQL Server Management Studio 连接到数据库之后。在与管理工作室联系之前,它会抱怨那里有一个同名的数据库。
如果您知道一个简单的教程,请指出我。
这段代码有效:
@using WebMatrix.Data;
@using System.Data.SqlClient
@{
}
@{
var Name = "";
var Position = "";
var Department = "";
var Shift = "";
if (IsPost)
{
Name = Request.Form["Name"];
Position = Request.Form["Position"];
Department = Request.Form["Department"];
Shift = Request.Form["Shift"];
String ConnStr = "Data Source=.''SQLEXPRESS; AttachDbFilename=C:''Users''Tevis Cox''Documents''Visual Studio 2015''Projects''TutorialPages''TutorialPages''App_Data''devDB.mdf; Database='"asdfdevDB'"; User ID=tcox; Password=123; Integrated Security=True;";
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO Employees(EmployeeName, Position, Department, Shift) VALUES(@p1, @p2, @p3, @p4)";
cmd.Parameters.Add(new SqlParameter("@p1", Name));
cmd.Parameters.Add(new SqlParameter("@p2", Position));
cmd.Parameters.Add(new SqlParameter("@p3", Department));
cmd.Parameters.Add(new SqlParameter("@p4", Shift));
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Add Personnel</title>
</head>
<body>
<h1>Add Personnel</h1>
<form method="post">
<fieldset>
<legend>Personnel Information</legend>
<p>
<label for="Name">Name:</label>
<input type="text" name="Name" value="@Request.Form["Name"]" />
</p>
<p>
<label for="Position">Position:</label>
<input type="text" name="Position" value="@Request.Form["Position"]" />
</p>
<p>
<label for="Department">Department:</label>
<input type="text" name="Department" value="@Request.Form["Department"]" />
</p>
<p>
<label for="Shift">Shift:</label>
<input type="text" name="Shift" value="@Request.Form["Shift"]" />
</p>
<p><input type="submit" name="buttonSubmit" value="Add" /></p>
</fieldset>
</form>
</body>
</html>