C# 从数据集中提取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14099183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:39:37  来源:igfitidea点击:

extracting data from data set

c#.net

提问by Freelancer

Respected Users,

尊敬的用户,

I am extracting data using data set. I want to put value in textbox. But value is not comming.

我正在使用数据集提取数据。我想在文本框中输入值。但价值并没有到来。

I have following Code

我有以下代码

try
            {
                da = new SqlDataAdapter("select ID from Customer where Name='" + gvBkPendingSearch.SelectedRows[0].Cells[1].Value.ToString() + "'",con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    txtCustomerID.Text = ds.Tables[0].Rows[0].ToString();
            }
            catch (Exception ex)
            {

            }
            finally
            {
            }

txtCustomerID is my textbox. It is capturing value as>>>>>System.Data.DataRow

txtCustomerID 是我的文本框。它正在捕获价值为>>>>>System.Data.DataRow

Error is in txtCustomerID.Text = ds.Tables[0].Rows[0].ToString();

错误在 txtCustomerID.Text = ds.Tables[0].Rows[0].ToString();

but i am not able to understand it. Please help me.

但我无法理解。请帮我。

采纳答案by Pawan Nogariya

change it like this

像这样改变它

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
   txtCustomerID.Text = ds.Tables[0].Rows[i]["ID"].ToString();

The mistake you are doing is, you are accessing this

你正在做的错误是,你正在访问这个

ds.Tables[0].Rows[0].ToString();

means 0th row, the whole row!! not the column value

表示第 0 行,整行!!不是列值

And the datatable row is System.Data.DataRowin .Net

并且数据表行在System.Data.DataRow.Net 中

回答by Darin Dimitrov

You need to select the column:

您需要选择列:

txtCustomerID.Text = ds.Tables[0].Rows[i][0].ToString();

Also note that you are overwriting the value of the textbox on each iteration of the loop. So what you will end up with is the ID of the last record in this textbox.

另请注意,您将在循环的每次迭代中覆盖文本框的值。所以你最终会得到这个文本框中最后一条记录的 ID。

Also your query seems vulnerable to SQL injection. Personally I would recommend you scraping the DataSets in favor of an ORM or even plain old ADO.NET:

此外,您的查询似乎容易受到 SQL 注入的影响。我个人建议您刮取数据集以支持 ORM 甚至是普通的旧 ADO.NET:

public static IEnumerable<int> GetIds(string name)
{
    using (var conn = new SqlConnection("Your connection string comes here"))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = "select ID from Customer where Name=@Name";
        cmd.Parameters.AddWithValue("@Name", name);
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                yield return reader.GetInt32(reader.GetOrdinal("ID"));
            }
        }
    }
}

And now you could happily use this function:

现在你可以愉快地使用这个函数了:

string name = gvBkPendingSearch.SelectedRows[0].Cells[1].Value.ToString();
int id = GetIds(name).FirstOrDefault();
txtCustomerID.Text = id.ToString();