コードを実行すると、エラーは発生せず、コードは正しく実行されているように見えますが、データベースのデータはリストビューに表示されません。ここでローカルの.mdfのデータがリストビューに表示されない
は、私のデザイナーのフォームのコードは次のとおりです。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace Cookbook
{
public partial class frmMain : Form
{
SqlConnection connection;
string connectionString;
public frmMain()
{
InitializeComponent();
connectionString = ConfigurationManager.ConnectionStrings["Cookbook.Properties.Settings.CookbookConnectionString"].ConnectionString;
}
public void frmMain_Load(object sender, EventArgs e)
{
PopulateRecipes();
}
public void PopulateRecipes()
{
using (connection = new SqlConnection(connectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Recipe", connection))
{
DataTable recipeTable = new DataTable();
adapter.Fill(recipeTable);
lstRecipes.DisplayMember = "Name";
lstRecipes.ValueMember = "Id";
lstRecipes.DataSource = recipeTable;
}
}
public void PopulateIngrediants()
{
string query = "SELECT a.Name FROM Ingrediants a" +
"INNER JOIN RecipeIngrediant b ON a.Id = b.IngrediantId" +
"WHERE b.RecipeId = @RecipeId";
using (connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(query, connection))
using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
{
command.Parameters.AddWithValue("@RecipeId", lstRecipes.SelectedValue);
DataTable ingrediantsTable = new DataTable();
adapter.Fill(ingrediantsTable);
lstIngrediants.DisplayMember = "Name";
lstIngrediants.ValueMember = "Id";
lstIngrediants.DataSource = ingrediantsTable;
}
}
public void lstRecipes_SelectedIndexChanged(object sender, EventArgs e)
{
PopulateIngrediants();
}
private void lblRecipes_Click(object sender, EventArgs e)
{
}
private void frmMain_Load_1(object sender, EventArgs e)
{
}
}
}
マイアプリの設定:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="Cookbook.Properties.Settings.CookbookConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Cookbook.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
これは私のコードの構造であり、私が実行したときにビューが空白です:
注:Visual Studio Community 2015アップデート3を使用しています。私はVSとC#を初めて使用しています。
詳細情報が必要な場合はお知らせください。 – Inconnu