-4

我想执行 Query 10 次以获取随机值,但我的代码永远不会停止,
它一直在循环并且没有响应。

这是我的代码

int j = 0;
do {
    DataSet AVATARLINE = new DataSet();
    SqlDataAdapter AVATARLINE_1 = new SqlDataAdapter("DECLARE
            @Random INT,@Upper INT,
            @Lower INT SET @Lower = (select MIN(ID)from MastryID)SET
            @Upper = (select MAX(ID)from MastryID)+1 SELECT
            @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)SELECT @Random", connection);
    AVATARLINE_1.Fill(AVATARLINE);
    foreach (DataRow row in AVATARLINE.Tables[0].Rows)
    {
        for (int i = 0; i < AVATARLINE.Tables[0].Columns.Count; i++)
        richTextBox1.Text += row[i].ToString() + Environment.NewLine;
    }
}
while (j < 10); // 0-9

任何想法如何使它只工作 10 次。

4

2 回答 2

4

给你:)......你似乎没有j在你的循环中增加

int j = 0;
    do {
            DataSet AVATARLINE = new DataSet();
            SqlDataAdapter AVATARLINE_1 = new SqlDataAdapter("DECLARE @Random INT,@Upper INT,@Lower INT SET @Lower = (select MIN(ID)from MastryID)SET @Upper = (select MAX(ID)from MastryID)+1 SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)SELECT @Random", connection);
            AVATARLINE_1.Fill(AVATARLINE);
            foreach (DataRow row in AVATARLINE.Tables[0].Rows)
            {
                for (int i = 0; i < AVATARLINE.Tables[0].Columns.Count; i++)
                    richTextBox1.Text += row[i].ToString() + Environment.NewLine;
            } 
        j++; 
        }
    while (j < 10); // 0-9
于 2013-01-25T08:04:15.963 回答
0

添加++

int j = 0;
do {
        DataSet AVATARLINE = new DataSet();
        SqlDataAdapter AVATARLINE_1 = new SqlDataAdapter("DECLARE @Random INT,@Upper INT,@Lower INT SET @Lower = (select MIN(ID)from MastryID)SET @Upper = (select MAX(ID)from MastryID)+1 SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)SELECT @Random", connection);
        AVATARLINE_1.Fill(AVATARLINE);
        foreach (DataRow row in AVATARLINE.Tables[0].Rows)
        {
            for (int i = 0; i < AVATARLINE.Tables[0].Columns.Count; i++)
                richTextBox1.Text += row[i].ToString() + Environment.NewLine;
        }  
    }
while (++j < 10); // 0-9

任何其他递增方法j都可以。更传统的方法是 for 循环:

for (int j = 0; j < 10; j++)
{
    DataSet AVATARLINE = new DataSet();
    SqlDataAdapter AVATARLINE_1 = new SqlDataAdapter("DECLARE @Random INT,@Upper INT,@Lower INT SET @Lower = (select MIN(ID)from MastryID)SET @Upper = (select MAX(ID)from MastryID)+1 SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)SELECT @Random", connection);
    AVATARLINE_1.Fill(AVATARLINE);
    foreach (DataRow row in AVATARLINE.Tables[0].Rows)
    {
        for (int i = 0; i < AVATARLINE.Tables[0].Columns.Count; i++)
            richTextBox1.Text += row[i].ToString() + Environment.NewLine;
    }  
}

最后,您可能想了解该Random课程:http: //msdn.microsoft.com/en-us/library/system.random.aspx

于 2013-01-25T08:04:14.573 回答