[英]Visual Studio Server Explorer show table data is blank
I am working on a Winforms c# program with a service-based database.我正在使用基于服务的数据库开发 Winforms c# 程序。 I have created a table called
Books
which is empty and currently has no records.我创建了一个名为
Books
的表,该表是空的,目前没有任何记录。
Using some code, I am inserting into the table values.使用一些代码,我将值插入到表中。 For some reason, when clicking Show Table Data, it still shows as blank despite me having added a record.
出于某种原因,当我点击显示表格数据时,尽管我添加了一条记录,它仍然显示为空白。
I checked to see if the record was there (but hidden) by using a DataGridView
with the source of its data being the Books
table.我通过使用数据源为
Books
表的DataGridView
检查记录是否存在(但隐藏)。 I could see the record had been created, but for some reason is not showing in the Server Explorer's Show Table Data view.我可以看到记录已创建,但由于某种原因未显示在服务器资源管理器的显示表数据视图中。
Here is my code for inserting a new record into the table:这是我将新记录插入表中的代码:
string query = "INSERT INTO Books (ISBN, Title, Authors, Publishers, Genre, Page_Count, Quantity) VALUES (@isbn, @title, @authors, @publishers, @genre, @page_count, @quantity)";
string connectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\LMSDB.mdf;Integrated Security=True";
// Establish connection with database
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Avoid SQL injection using parameters. Replace them with their real values
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@isbn", isbn);
command.Parameters.AddWithValue("@title", title);
command.Parameters.AddWithValue("@authors", authors);
command.Parameters.AddWithValue("@publishers", publishers);
command.Parameters.AddWithValue("@genre", genre);
command.Parameters.AddWithValue("@page_count", pageCount);
command.Parameters.AddWithValue("@quantity", quantity);
try
{
connection.Open();
if (command.ExecuteNonQuery() == 1)
{
// 1 Row affected. Success
Console.WriteLine("Book added to database successfully");
ClearControlValues();
}
}
catch (Exception ex)
{
MessageBox.Show("An error occured:\n" + ex.Message);
}
finally
{
connection.Close();
}
}
As you can see, the Show Table Data view is appearing blank, despite me knowing that there is a record as I can see it on a DataGridView
如您所见,“显示表数据”视图显示为空白,尽管我知道有一条记录,正如我在
DataGridView
上看到的那样
Is there something I'm missing as to why the record isn't appearing?关于为什么没有出现记录,我是否遗漏了什么?
EDIT : Solved thanks to Steve who pointed out that the Server Explorer and my code had different connection strings.编辑:感谢史蒂夫指出服务器资源管理器和我的代码具有不同的连接字符串。 Having changed these around, I now have the intended result.
改变了这些,我现在有了预期的结果。
I made a test with your code, and data can be inserted successfully.我用你的代码做了测试,可以成功插入数据。
As Steve said, the problem lies in your connection string.正如史蒂夫所说,问题出在您的连接字符串上。 And you can find your connectionstring by following steps:
您可以通过以下步骤找到您的连接字符串:
1.Right-click on the connection name and select Properties. 1.右键单击连接名称和 select 属性。
2.There is an item called Connection String in the Properties window. As follows: 2.Properties window里面有个Connection String项,如下:
Daniel Zhang张勇
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.