0

数値の SQL 行を複数のテキスト ボックスに読み込もうとしています。基本的に、行には「;」で区切られた数字が含まれます。「;」で区切られたときに各番号を取得するにはどうすればよいですか 独自のテキストボックスにありますか?

        SqlConnection conn = new SqlConnection("Data Source=LAURA-PC;Initial Catalog=Sudoku;Integrated Security=True");
        conn.Open();

        SqlCommand command = conn.CreateCommand();
        command.CommandText = "Select puzzle from Puzzle";
        command.CommandType = CommandType.Text;

        SqlDataReader reader = command.ExecuteReader();
        if (reader.Read())
        {
            textBox1.Text = reader.GetValue(0).ToString();
            textBox2?
            textbox3?
        }
        conn.Close();
4

2 に答える 2

1

TextBoxes と同じ数 (または少ない数) の数値があると仮定すると、TextBoxes を配列に入れ、次のコードを使用できます。

...
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
    string[] tokens = reader.GetString(0).Split(';');
    for(int i = 0; i < tokens.Length; i++)
    {
        textBoxes[i].Text = tokens[i];
    }
}
...
于 2012-06-07T02:13:46.543 に答える