簡體   English   中英

數據庫連接和在Visual Basic中填充ListBox

[英]Database Connection and Populating ListBox in Visual Basic

我想使用Visual Basic在Windows應用程序窗體中顯示結果,但我不斷收到此Exception錯誤:

System.Data.SqlClient.SqlException:'參數化查詢'(@Skill nvarchar(4000))SELECT a.Skill FROM拼寫INNER JOIN C'期望使用參數'@Skill',但未提供。

我不知道該怎么辦,謝謝您的時間和考慮

這是我的整個代碼:

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 rpg_game
{
    public partial class Form1 : Form
    {
        SqlConnection connection;
        string connectionString;
        public Form1()
        {
            InitializeComponent();

    connectionString=ConfigurationManager.
    ConnectionStrings["rpg_game.Properties.Settings
    .charactersConnectionString"].ConnectionString;
            Populatebox();
        }

        private void Populatebox()
        {
            using (connection = new SqlConnection(connectionString))
            using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM unit_tb", connection))
            {
                DataTable unit_table = new DataTable();

                adapter.Fill(unit_table);

                Name_list.DisplayMember = "Name";
                Name_list.ValueMember = "Id";
                Name_list.DataSource = unit_table;
            }
        }

        private void Populatebox2()
        {
            string query = "SELECT a.Skill FROM Spells a INNER JOIN Character_Skills b ON a.Id = b.spell_id WHERE b.unit_id  = @Skill";

            using (connection = new SqlConnection(connectionString))
            using (SqlCommand command = new SqlCommand(query,connection))
            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
            {
                command.Parameters.AddWithValue("@Skill", Class_list.SelectedValue);
                DataTable unit_table2 = new DataTable();

                adapter.Fill(unit_table2);

                Class_list.DisplayMember = "Skill";
                Class_list.ValueMember = "Id";
                Class_list.DataSource = unit_table2;
            }
        }

        private void Class_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            Populatebox2();
        }
    }
}

需要參數“ @Skill”(未提供)。

消息清晰,查詢正常,但參數@Skill的值為null因此請檢查Class_list.SelectedValue

我終於走了! 謝謝你的幫助!

意識到這一點后,我真的很傻,只需要更改代碼的這一部分即可:

command.Parameters.AddWithValue("@Skill", 
Class_list.SelectedValue);

變成:

command.Parameters.AddWithValue("@Skill", 
Name_list.SelectedValue);

由於我要執行的操作是獲取數據庫字符名稱的值,即“ JAY”,其值為1,然后將其復制到@skill中,作為一個結果:

我想要什么結果

每當我單擊一個新名稱時,他們的技能就會按照數據庫中的說明顯示

再次感謝大家的注意

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM