以下が(取り出されたデータは、次の項目を取得するために使用される方法として重要ではないことに注意)概念的なものであり、内のコメントを参照。
using System.Data.SqlClient;
using System.Data;
namespace WindowsFormsApplication3
{
public class Operations
{
string ConnectionString = "Data Source=KARENS-PC;" +
"Initial Catalog=ForumExamples;Integrated Security=True";
public DataTable Read()
{
DataTable dt = new DataTable();
// add column so we can get one row/field on each button click
dt.Columns.Add(new DataColumn() { ColumnName = "Used", DataType = typeof(bool) });
using (SqlConnection cn = new SqlConnection { ConnectionString = ConnectionString })
{
using (SqlCommand cmd = new SqlCommand { Connection = cn })
{
cmd.CommandText = "SELECT FullName FROM People";
cn.Open();
dt.Load(cmd.ExecuteReader());
}
}
// by default the column we added, the value is null so set it to false
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i].SetField<bool>("Used", false);
}
return dt;
}
}
}
その文が行うことになっているものを形成コード
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// private DataTable to work with in button1
DataTable dtDemo;
// create an instance of our data class, use it in button1
Operations ops = new Operations();
private void button1_Click(object sender, EventArgs e)
{
var result = dtDemo.AsEnumerable().FirstOrDefault(row => row.Field<bool>("Used") == false);
// find first row that is available
if (result != null)
{
Console.WriteLine(result.Field<string>("FullName"));
// mark it as used
result.SetField<bool>("Used", true);
}
else
{
Console.WriteLine("Done"); // we have used all rows
}
}
/// <summary>
/// Load our data
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
dtDemo = ops.Read();
}
}
}
?常に何も返さないような並べ替えがあります。確かに質問のテキストではありません。 – DonBoitnott
それらを一度にロードしてから、 'BindingNavigator'を使って1つずつ表示してください。 –
"それは動作していません" .. Stack Overflowersの悪夢。 –