简体   繁体   English

从数据集中获取数据

[英]Getting data out of a dataset

I am trying to get data out of an sql table using a dataset and a stored procedure, the dataset should only contain one value after the procedure is executed (I am getting id numbers, and id number is a primary key in my Costumers table so there should only be one of each). 我正在尝试使用数据集和存储过程从sql表中获取数据,执行过程后数据集应仅包含一个值(我正在获取id号,而id号是Costumers表中的主键,因此每个只能有一个)。 The IsCostumerIDTaken stored procedure just returns the id that it takes as a parameter. IsCostumerIDTaken存储过程仅返回其作为参数的id。 If it finds it, it returns it. 如果找到它,则返回它。 I dont know what it does if it doesn't, returns 0 perhaps. 不知道怎么办,可能会返回0。

 public static bool IsCostumerIDTaken(string id)
    {
        SqlConnection conObj = GetConnection();
        SqlCommand cmdObj = new SqlCommand("IsCostumerIDTaken", conObj);
        cmdObj.CommandType = CommandType.StoredProcedure;
        cmdObj.Parameters.Add("@ID", SqlDbType.NVarChar).Value = id;        
        SqlDataAdapter da = new SqlDataAdapter(cmdObj);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows[0][0].ToString() != id)
            return false;
        return true;       
    }

after executing this command in one of my pages 在我的页面之一中执行此命令后

Label1.Text = DAL.IsCostumerIDTaken(TextBox1.Text).ToString();

I get this error: 我收到此错误:

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: System.IndexOutOfRangeException: There is no row at position 0. 异常详细信息:System.IndexOutOfRangeException:位置0没有行。

 Source Error: Line 73: DataSet ds = new DataSet(); Line 74: da.Fill(ds); Line 75: if (ds.Tables[0].Rows[0][0].ToString() != id) Line 76: return false; Line 77: return true; 

What is the problem here, exactly? 到底是什么问题? Am I not pulling data out of a dataset correctly? 我不是从数据集中正确提取数据吗?

If there is no data, there will be no rows. 如果没有数据,将没有行。 You need to test ds.Tables[0].Rows.Count 您需要测试ds.Tables[0].Rows.Count

Replace this part 更换这部分

if (ds.Tables[0].Rows[0][0].ToString() != id)
            return false;
        return true;

with this: 有了这个:

return ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0][0].ToString().Equals(id);

Correct. 正确。 You are not pulling the data out correctly. 您没有正确提取数据。 It's impossible for me to tell, but somewhere in here... 我无法告诉,但在这里某处...

ds.Tables[0].Rows[0][0]

... you are getting a null object. ...您将得到一个空对象。 I would expect that the query is returning empty so the last [0] is trying to index a null . 我希望查询返回空值,因此最后一个[0]试图索引null

If you have sufficient database permissions, you can run SQL Server Profiler on the database and see the exact query with the exact parameters that are being executed. 如果您具有足够的数据库权限,则可以在数据库上运行SQL Server Profiler,并查看具有正在执行的确切参数的确切查询。 From there, you should be able to determine if the problem is with your stored procedure or with your C# code. 从那里,您应该能够确定问题出在存储过程还是C#代码上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM