![](/img/trans.png)
[英]Visual Studio 2013 Server Explorer custom data provider: MongoDB
[英]Visual Studio Server Explorer show table data is blank
我正在使用基於服務的數據庫開發 Winforms c# 程序。 我創建了一個名為Books
的表,該表是空的,目前沒有任何記錄。
使用一些代碼,我將值插入到表中。 出於某種原因,當我點擊顯示表格數據時,盡管我添加了一條記錄,它仍然顯示為空白。
我通過使用數據源為Books
表的DataGridView
檢查記錄是否存在(但隱藏)。 我可以看到記錄已創建,但由於某種原因未顯示在服務器資源管理器的顯示表數據視圖中。
這是我將新記錄插入表中的代碼:
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();
}
}
如您所見,“顯示表數據”視圖顯示為空白,盡管我知道有一條記錄,正如我在DataGridView
上看到的那樣
關於為什么沒有出現記錄,我是否遺漏了什么?
編輯:感謝史蒂夫指出服務器資源管理器和我的代碼具有不同的連接字符串。 改變了這些,我現在有了預期的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.