初学者,.net C#使用数据库

时间:2016-01-26 18:15:40

标签: c# asp.net sql-server visual-studio-2012

我已经尝试了几天能够使用简单的web base .net项目的数据库。 我成功地使用了SQL compact的教程,但有人建议我使用SQLEXPRESS,因为它会创建.mdf文件,与VISUAL STUDIO相同。

出于沮丧,我重新安装了干净的副本: Windows 10 Visual Studio社区2015 SQL 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连接到数据库之后。在与管理工作室联系之前,它会抱怨那里有一个同名的数据库。

如果你知道一个简单的教程,请指出它。

1 个答案:

答案 0 :(得分:-1)

此代码有效:

@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>