C#数据适配器参数 [英] C# Data Adapter Parameters

查看:128
本文介绍了C#数据适配器参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一些代码来获取一些数据我的数据库。在存储过程只使用了 ID 作为参数,并使用该筛选结果。我在 SSMS 运行使用EXEC命令存储过程和它的作品。但是,当我尝试使用的代码底部调用它,它失败了,说我没有提供参数。任何人都可以看到我在做什么错了?



 使用(SqlConnection的SQLCONNECT =新的SqlConnection(ConfigurationManager.ConnectionStrings [的ConnectionString] .ConnectionString))
{

{
的DataTable dtBets =新的DataTable(所有投注);使用(SqlDataAdapter的SQLDA =新SqlDataAdapter的(up_Select_all,SQLCONNECT))
{
sqlDA.SelectCommand.Parameters.Add(@ ID,SqlDbType.BigInt).value的= pCustomer
。客户ID;

sqlDA.Fill(dtBets);
返回dtBets;
}
}

赶上(SQLEXCEPTION EX)
{
// catch代码
}


解决方案

您已经忘记告诉的DataAdapter 它应该叫存储过程

 使用(SqlDataAdapter的SQLDA =新SqlDataAdapter的(up_Select_all,SQLCONNECT))
{
sqlDA.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlDA.SelectCommand.Parameters.Add(@ ID,SqlDbType.BigInt).value的= pCustomer.CustomerID;

sqlDA.Fill(dtBets);
返回dtBets;
}


I've written some code to get some data out of my database. The stored procedure only uses the ID as the parameter, and uses that to filter results. I've run the stored procedure using the EXEC command in SSMS and it works. However, when I try and call it using the code at the bottom, it fails, saying that I have not provided the parameter. Can anyone see what I'm doing wrong?

using (SqlConnection sqlConnect = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{       
    try
    {
        DataTable dtBets = new DataTable("All Bets");
        using (SqlDataAdapter sqlDA = new SqlDataAdapter("up_Select_all", sqlConnect))
        {
            sqlDA.SelectCommand.Parameters.Add("@ID", SqlDbType.BigInt).Value = pCustomer.CustomerID;

            sqlDA.Fill(dtBets);
            return dtBets;
        }                        
    }

    catch (SqlException ex)
    {
        //catch code
    }

解决方案

You have forgotten to tell the DataAdapter that it should call a Stored-Procedure:

using (SqlDataAdapter sqlDA = new SqlDataAdapter("up_Select_all", sqlConnect))
{
    sqlDA.SelectCommand.CommandType = CommandType.StoredProcedure;
    sqlDA.SelectCommand.Parameters.Add("@ID", SqlDbType.BigInt).Value = pCustomer.CustomerID;

    sqlDA.Fill(dtBets);
    return dtBets;
}    

这篇关于C#数据适配器参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆