向 Datatable 函数添加参数

Add parameter to Datatable function

提问人:Khayal Ads 提问时间:11/14/2023 最后编辑:XedniKhayal Ads 更新时间:11/14/2023 访问量:40

问:

我想在DataGridView中显示过程结果并从Combobox获取参数值

我创建了一个名为Report_with_depo

Create proc [dbo].[Report_with_depo]
@customer_id nvarchar (50)

AS
select customer_id ,customer_fullname, amount_withdraw ,amount_deposit ,entry_date
from Oprations b 
inner join Customers a
    on b.customer_id2 = a.customer_id
where customer_id = @customer_id

和数据表函数

public DataTable Report_with_depo()
{
    DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
    DAL.Open();
    DataTable dt = new DataTable();
    dt = DAL.SelectData("Report_with_depo", null);
    return dt;
}

显示按钮代码就像

this.dgv_Report.DataSource = Report_con.Report_with_depo();

我已经测试了我的代码并将参数替换为它有效的值,但现在我想从 Combobox 中获取值

C# SQL SQL Server 数据表 DataGridView

评论

0赞 Khayal Ads 11/14/2023
是的,它工作正常,但是在我添加参数后向过程添加参数之前,我不知道如何将其添加到数据表并从组合框中获取参数的值
0赞 Charlieface 11/14/2023
那么怎么办?为什么要使用这样的通用函数,为什么不为每个过程及其参数设置一个适当的函数?DAL.SelectData
0赞 Khayal Ads 11/14/2023
DAL 是 DataAccessLayer 文件夹,它包含数据库连接的所有要求,Select Data // 从数据库公共 DataTable 中读取 SelectData(string stored_prc, SqlParameter[] param) { cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = stored_prc; cmd.Connection = con; if (param != null) { for (int i = 0; i < param.长度;i++) { cmd.Parameters.Add(param[i]);da = 新 SqlDataAdapter(cmd);dt = 新 DataTable();呸填充(dt);返回 dt;}
0赞 Charlieface 11/14/2023
那么你的问题是什么,只是做旁注:你需要处理连接,命令和适配器,不要缓存连接对象。DAL.SelectData("Report_with_depo", new[]{ new SqlParameter("@customer_id", SqlDbType.NVarChar, 50) { Value = someValueHere } ) })
0赞 Khayal Ads 11/14/2023
很好,它解决了我的问题,非常感谢

答: 暂无答案