【发布时间】:2020-10-22 08:42:33
【问题描述】:
我正在尝试将菜单项列表加载到 WPF 中的 DataGrid 中,并且只能成功地从表中返回一行。我试图创建一个 for 循环来读取每一行并将其加载到 dtsizes,但我得到一个异常,说明我的过程或函数指定了太多参数。
private DataTable GetSizesData()
{
//NOTE: compiler will through nullpointer error if datatable is null, so create an object
DataTable dtsizes = new DataTable();
//get and store the datatable through sql connection -> applicationsetting.cs
using (SqlConnection con = new SqlConnection(ApplicationSetting.ConnectionString()))
{
//runs sql command usp load pizzas (see .txt file)
using (SqlCommand cmd = new SqlCommand("usp_Pizzas_LoadAllPizzas", con))
{
for (int i = 0; i < 11; i++)
{
//load procedure and given params
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@main_identifier", i);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
con.Close();
dtsizes.Load(sdr);
}
//read data then load into dtsizes datatable
}
}
return dtsizes;
}
这是我的 usp_PizzasAllPizzas SQL 过程:
ALTER PROCEDURE [dbo].[usp_Pizzas_LoadAllPizzas]
(
@main_identifier INT
)
AS
BEGIN
--DECLARE @IsSelected BIT
--SET @IsSelected = 0
SELECT [main_identifier] AS 'ID'
,[main_name] AS 'Title'
--,@IsSelected AS 'Select'
FROM [dbo].[main_cata] WHERE
[main_identifier] = @main_identifier
END
我确信这里的答案很简单,但我不知道如何将表中的所有条目加载到我的 dataGrid 中。我已经尝试定义一个单独的 INT 并在类的一个单独部分中循环遍历它,但仍然没有让它工作。任何反馈或 cmets 都会有所帮助,非常感谢您! Database Table
【问题讨论】:
-
在您的第二次迭代中,您尝试添加一个已在第一次迭代中添加的参数
-
将所有
cmd操作移到循环之外,除了执行。
标签: c# sql-server database wpf