C# 我做了一个数据表。但是每次打开我的数据库/数据表的第一列时都会用到它。第二行/第二列怎么取?

C# 我做了一个数据表。但是每次打开我的数据库/数据表的第一列时都会用到它。第二行/第二列怎么取?,c#,C#,有人能帮我得到数据表的第二行/第二列吗?因为不管怎样,每次都需要第一行/第二列,即使我说我想要第二行/第二列 int score = 1; private void pbBier_Click(object sender, EventArgs e) { MySqlConnection conn = new MySqlConnection("Server=localhost;Database=patn4lj1;Uid=root;Pwd=root;");

有人能帮我得到数据表的第二行/第二列吗?因为不管怎样,每次都需要第一行/第二列,即使我说我想要第二行/第二列

 int score = 1;
    private void pbBier_Click(object sender, EventArgs e)
    {
        MySqlConnection conn = new MySqlConnection("Server=localhost;Database=patn4lj1;Uid=root;Pwd=root;");

        conn.Open();

        MySqlCommand command = conn.CreateCommand();
        command.CommandText = "select * from locaties";
        MySqlDataReader reader = command.ExecuteReader();

        DataTable dtData = new DataTable();
        dtData.Load(reader);

                 //lblX.Text = rowX["X"].ToString();


        int xPos1 = (Int32)dtData.Rows[0][0]; //dit is de 4de rij van de 1ste kollom.        //  lblX.Text = rowX[colX].ToString();
        int xPos2 = (Int32)dtData.Rows[1][0];
        int xPos3 = (Int32)dtData.Rows[2][0];
        int xPos4 = (Int32)dtData.Rows[3][0];
        int xPos5 = (Int32)dtData.Rows[4][0];

        int yPos1 = (Int32)dtData.Rows[0][1];
        int yPos2 = (Int32)dtData.Rows[1][1];
        int yPos3 = (Int32)dtData.Rows[2][1];
        int yPos4 = (Int32)dtData.Rows[3][1];
        int yPos5 = (Int32)dtData.Rows[4][1];
      //  DataColumn colY = dtData.Columns[1];
      //  DataRow rowY = dtData.Rows[4];            //  lblY.Text = rowY["Y"].ToString();           
       // int YPos = rowY.ToString()[4];            // lblY.Text = rowY[colY].ToString();

        lblAantalScore.Text = score++.ToString();



        bool Gedaan = false;

        while (Gedaan == false)
        {            
            pbBier.Location = new Point(xPos1, yPos1);

            if (pbBier.Left == xPos1 && pbBier.Top == yPos1)
            {
                Gedaan = true; tmLoop.Stop();
                MessageBox.Show("gefeliciteerd u hebt er " + lblTijd.Text + " Sec over gedaan"); tmLoop.Start();
            }


            if (Gedaan == true)
            {
                pbBier.Location = new Point(xPos2, yPos2);
            }

        //pbBier.Location = new Point(xPos3, yPos3);
        //pbBier.Location = new Point(xPos4, yPos4);
        //pbBier.Location = new Point(xPos5, yPos5);


        }



    }

**我编辑了它,有人能帮我吗?如果我点击picturebox,它将转到datareader的位置。“但我想要的是,如果我第二次单击picturebox,它必须转到datatable的新坐标。你能帮我吗**

您的
数据表
可以以
多维数组的形式访问
。例如:

dtData.Rows[0][0]; // access the first row and first column
dtData.Rows[0][1]; // access the first row and second column
dtData.Rows[0][2]; // access the first row and third column
dtData.Rows[1][0]; // access the second row and first column
dtData.Rows[1][1]; // access the second row and second column
dtData.Rows[1][2]; // access the second row and third column
如果需要,可以转到使用两个嵌套的
for
语句返回的所有字段:

for(int i = 0;i < dtData.Rows.Count;i++)//travels the rows
{
     for(int j = 0;j < dtData.Rows.Count;j++)//travels the columns
     {
          var valueField = dtData.Rows[i][j];//access the value of current field
     }
}
for(int i=0;i
相反,您可以使用:

Object data = dtData.Rows[3][1];
int xPos = data.ToString()[1]; 
使用:


DataRow row=dtData。行[1]将为您提供第二行。它是基于0的。Object data=dtData。行[1][1]将为您提供第二列。查看我新编辑的代码,我希望我的picturebox在第一次单击时移动,但当我第二次单击时,它必须转到XPO和YPO的协调处,如果您知道我的意思的话。我希望你能帮助我?你的问题仍然让我困惑,主要目标是什么?首先,根据哪个位置移动picturebox?你第二次点击我还不清楚?你可以自己更好地澄清这些疑问,这样我就可以帮助你了?看,第一次点击图片框,图片框指向坐标xPos1和yPos1。“那些xpo和ypo是我的位置”,然后第二次点击图片框,我希望图片框指向第二个位置,即xPos2和yPos2。因此,进一步使用xPos3、yPos3和xPos4、yPos4。这是我的数据库信息:我有一个名为locaties的表,在这个表中我有两列,一列是X,一列是Y。X中有5个数字:30,111,0,350,700,Y中也有5个数字:200,22,30,150,300。这意味着xPos1=列X.number30。yPos=第Y.200列。我希望你知道我的意思,所以你可以尽快帮助我!谢谢。但当我键入此命令时,is向我显示了以下错误:只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句D:\Programeren\blokboek4\week8\ZoetebierBlendiAO1C2\ZoetebierBlendiAO1C2\Form1.cs 58 13 ZoetebierBlendiAO1C2您的select语句只查找“X”列,因此,如果您尝试访问索引1处的列,是否会发生错误,因为您只返回了一列,而不是两列。现在我执行了以下命令。CommandText=“select*from locaties”;但是仍然有一个错误:索引超出范围。我是stackoverflow的新手,所以我不知道如何更新你的问题并用当前代码替换你的旧代码。嘿,现在我有一个问题,因为我说:“如果我单击picturebox,它将转到datareader的位置。”但是我想要的是,如果我第二次点击picturebox,那么它必须转到datatable的新坐标。你能帮我吗?我的PictureBoxs名字是pbBier我现在真的不知道你说的XAML是什么意思?看看我新编辑的代码,我希望我的picturebox在第一次单击时移动,但是当我第二次单击时,它必须转到Xpos和YPO的协调人那里,如果你知道我的意思的话。我希望你能帮助我?
int xPos = (Int32)dtData.Rows[3][0];//you know which access fourth row and first column?