简体   繁体   English

Visual Studio 服务器资源管理器显示表数据为空白

[英]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.

相关问题 Visual Studio 2013 Server Explorer自定义数据提供程序:MongoDB - Visual Studio 2013 Server Explorer custom data provider: MongoDB 插入登录和注册数据未显示在 SQL 服务器数据库表 Visual Studio 2019 - Insert login and registration data not show in SQL Server database table Visual Studio 2019 在Telerik Reporting Visual Studio数据浏览器中显示子类的数据源 - Show data source(s) of child class in Telerik Reporting Visual Studio Data Explorer 插入和注册数据未显示在数据库表 Visual Studio 2019 中 - Insert and Registration data not show in database table visual studio 2019 项目内部的MDF数据库。 Visual Studio Server资源管理器不显示对数据的更改 - MDF database inside project. Visual Studio Server Explorer not showing changes to the data Visual Studio,Server Explorer无法建立与Microsoft SQL Server的连接 - Visual Studio, Server Explorer Cannot Establish Connection to Microsoft SQL server 使用Visual Studio服务器资源管理器连接到远程SQL Server 2012 - Connect to remote SQL server 2012 using visual studio server explorer 为什么我的Visual Studio在解决方案资源管理器中不显示解决方案? - Why does my Visual Studio not show a solution in the Solution Explorer? Visual Studio:Git 团队资源管理器未显示任何更改 - Visual Studio: Git Team Explorer does not show any changes Visual Studio Server Explorer是否支持自定义数据库提供程序? - Does Visual Studio Server Explorer support custom database providers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM