我不确定这是一个语法错误或我的IDE(Visual Studio)的问题,但我似乎无法insert into
我已添加到项目中的表。
private void create_user_username_box_Leave(object sender, EventArgs e)
{
// Add user/password to database when when someone leaves the area.
using (DbConnection connection = new SqlConnection(@"Server=localhost\SQLEXPRESS01;Database=master;Trusted_Connection=True;"))
{
connection.Open();
using (DbCommand command = new SqlCommand("INSERT INTO [dbo].[information] (id, password) VALUES (@id, @pw);"))
{
command.Parameters.Add(new SqlParameter("@id", create_user_username_textbox.Text));
command.Parameters.Add(new SqlParameter("@pw", create_user_password_textbox.Text));
command.Connection = connection;
command.ExecuteNonQuery();
}
}
}
当我离开文本框时,我似乎收到一条错误消息,说明
无效的对象名称'dbo.information'
实际上,在SQL Server中创建表是这样的:
CREATE TABLE [dbo].[information]
(
[Id] NVARCHAR(50) NOT NULL PRIMARY KEY,
[password] NVARCHAR(50) NULL
)
那为什么这是一个错误的名字?
答案 0 :(得分:1)
我认为这里的问题可能是连接字符串问题。
关闭此示例您使用的是本地数据库,这是我过去使用的示例连接字符串。
connectionString ="Data Source=.\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True"
初始目录是数据库名称,integrated security = true
表示对数据库使用Windows身份验证。
所以在你的例子中它看起来像这样:
connectionString ="Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"/>
与评论中提到的其他人一样,不要再次在master数据库中创建表,而是始终创建一个新表。
另外需要注意的是,个人接触是查看configuration manager以便存储连接字符串,然后您可以从那里轻松调用连接字符串。
答案 1 :(得分:0)
您正在使用连接字符串上的<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HOME</title>
<!-- TODO add manifest here -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link href="https://fonts.googleapis.com/css?family=Montserrat:200" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
<link rel="stylesheet" type="text/css" href="style.css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-expand-lg sticky-top navbar-dark bg-dark">
<div class="d-flex align-items-center justify-content-between w-100">
<button class="navbar-toggler navbar-toggler-left x collapsed" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand mx-auto ml-lg-5" href="#">
<img src="https://getbootstrap.com/assets/brand/bootstrap-solid.svg" width="30" height="30" alt="Logo">
</a>
</div>
<div class="collapse navbar-collapse mr-lg-5" id="navbarText">
<ul class="navbar-nav ml-auto">
<li class="nav-item active mx-2 animated fadeInDown">
<a class="nav-link" href="#">HOME<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item mx-2 animated fadeInDown">
<a class="nav-link" href="#">PROJECTS</a>
</li>
<li class="nav-item mx-2 animated fadeInDown">
<a class="nav-link" href="#">ABOUT</a>
</li>
<li class="nav-item mx-2 animated fadeInDown">
<a class="nav-link" href="#">CONTACT</a>
</li>
</ul>
</div>
</nav>
<div>
<p>Test</p>
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<p>ffff</p>
</body>
</html>
数据库。
您可以为自己的数据库更改master
:
master
Server=localhost\SQLEXPRESS01;Database=master;Trusted_Connection=True;