我想使用Visual Basic在Windows应用程序窗体中显示结果,但我不断收到此Exception错误:
System.Data.SqlClient.SqlException:'参数化查询'(@Skill nvarchar(4000))SELECT a.Skill FROM拼写INNER JOIN C'期望参数'@Skill'未提供。' / p>
我不知道该怎么办,谢谢您的时间和考虑
这是我的完整代码:
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();
}
}
}
答案 0 :(得分:0)
期望参数“ @Skill”(未提供)。
该消息已清除,查询正常,但参数@Skill的值为null
,因此请选中Class_list.SelectedValue
答案 1 :(得分:0)
我终于走了!谢谢您的帮助!
现在我意识到自己真的很傻,我只需要更改代码的这一部分:
command.Parameters.AddWithValue("@Skill",
Class_list.SelectedValue);
进入:
command.Parameters.AddWithValue("@Skill",
Name_list.SelectedValue);
因为我要执行的操作是获取数据库字符名称的值,即值为1的“ JAY”,然后将其复制到@skill中,作为结果结果:
每次我单击一个新名称时,他们的技能都会按照数据库中的说明显示
再一次感谢大家的注意