2017-10-25 8 views
0

私はC#を初めて使用しています。 私はC#Windowsフォームアプリケーションに取り組んでおり、試験実施のデスクトップアプリケーションを作成しています... 私は複数の選択肢を表示するための1つのフォームを作成しました。 次のボタンをクリックすると、SQLデータベースからすべての質問が1つずつ表示されます。下のコードでこれを実行しようとしましたが、機能していません...ボタンクリックでSQLからデータを1つずつ取得するには

マイコードはこれです:

private void btnNext_Click(object sender, EventArgs e) 
    {   
     SqlConnection connnn = new SqlConnection(); 
     connnn.ConnectionString = "Data Source=FAIZANTRADERS;Initial Catalog=ExaminationSystem;Integrated Security=True"; 
     connnn.Open(); 
     SqlDataAdapter sda = new SqlDataAdapter("SELECT isnull (min (cast (Q_ID as int)),0)+1 from CIT_Qs", connnn); 
     DataTable dt = new DataTable(); 
     sda.Fill(dt); 
     Q_idTxt.Text = dt.Rows[0][0].ToString(); 
     connnn.Close(); 

     DisplayQs();// for calling method 
    } 

これはフォームのスクリーンショットである:

Form Screenshot

+0

?常に何も返さないような並べ替えがあります。確かに質問のテキストではありません。 – DonBoitnott

+1

それらを一度にロードしてから、 'BindingNavigator'を使って1つずつ表示してください。 –

+0

"それは動作していません" .. Stack Overflowersの悪夢。 –

答えて

0

以下が(取り出されたデータは、次の項目を取得するために使用される方法として重要ではないことに注意)概念的なものであり、内のコメントを参照。

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(); 
     } 
    } 
} 
関連する問題