C# Visual Basic中的数据库连接和填充列表框
我想使用visual basic在windows应用程序窗体中显示结果,但我不断收到以下异常错误: System.Data.SqlClient.SqlException:'参数化查询'(@Skill nvarchar(4000))选择a.Skill FROM拼写内部连接C'需要参数'@Skill',但未提供该参数。' 我不知道该怎么办,谢谢你的时间和考虑 这是我的全部代码:C# Visual Basic中的数据库连接和填充列表框,c#,vb.net,C#,Vb.net,我想使用visual basic在windows应用程序窗体中显示结果,但我不断收到以下异常错误: System.Data.SqlClient.SqlException:'参数化查询'(@Skill nvarchar(4000))选择a.Skill FROM拼写内部连接C'需要参数'@Skill',但未提供该参数。' 我不知道该怎么办,谢谢你的时间和考虑 这是我的全部代码: using System; using System.Collections.Generic; using System.
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\u列表。选择值我终于开始了!谢谢你的帮助
我真的很愚蠢,现在我意识到了这一点,我只需要更改代码的这一部分:
command.Parameters.AddWithValue("@Skill",
Class_list.SelectedValue);
进入:
因为我想做的是获取数据库中字符名的值,即“JAY”,值为1,然后将其作为一个复制到@skill,从而得到以下结果:
每当我点击一个新名字时,他们的技能就会显示在我的数据库中
再次感谢大家提供了Class\u列表返回的值。SelectedValue
不能为空,并且有一个确定的值,例如1、2、3、a、B、,C@GrantWinney当逐字符号位于字符串内时,它不是逐字符号。SQL Server正在寻找“@Skill”作为参数名。@grantwiney您是对的。我甚至用存储过程检查了它。然而,我仍然说@我的字符串带\“与“@myString”不同
command.Parameters.AddWithValue("@Skill",
Name_list.SelectedValue);