在Button Click上逐个从sql获取数据

时间:2017-10-25 11:16:34

标签: c# winforms

我是C#的新手。 我正在开发一个C#Windows表单应用程序,我正在进行一个检查指导桌面应用程序... 我已经制作了一个显示多个选择问题的表格。 当我点击下一个按钮时,我希望每个问题都是从sql数据库逐个显示...我试图通过下面的代码执行此操作,但它不起作用...

我的代码是这样的:

 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的截图:

Form Screenshot

1 个答案:

答案 0 :(得分: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();
        }
    }
}