Visual tìm kiếm

Màu nền
Font chữ
Font size
Chiều cao dòng

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace WindowsFormsApplication1

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();           

        }

        #region "Khai báo biến sử dụng cho việc truy cập cơ sở dữ liệu"

        //đối tượng kết nối

        private SqlConnection cn;

        // đối tượng thực hiện lệnh

        private SqlCommand cmd;

        //đối tượng lọc dữ liệu

        private SqlDataAdapter da;

        //đối tượng chứa dữ liệu tạm thời.

        private DataSet ds;

        #endregion

        #region"viết hàm hiển thị câu hỏi "

        private void Hienthicauhoi()

        {

            //b1: thực hiện mở kết nối

            cn = new SqlConnection(@"Data Source=A42F-PC\SQLEXPRESS;Initial Catalog=Trondethi;User ID=sa;Password=1122");

            cn.Open();

            //b2: thực hiện lệnh theo yêu cầu: (hiển thị <xuất dữ liệu>)

            //khởi tạo đối tượng command thông qua phương thức CreateCommand() của SqlConnection.

            cmd=cn.CreateCommand();

            //th1:

            //cmd.CommandType=CommandType.Text;

            //cmd.CommandText="Select * from Cauhoi";

            //th2:

            cmd.CommandType=CommandType.StoredProcedure;

            cmd.CommandText="HienthiCauhoi";

            //b3: đổ dữ liệu ra dataset thông qua cầu nối DataAdapter

            da=new SqlDataAdapter (cmd);

            ds=new DataSet ("bảng chứa dữ liệu");

            //sử dụng phương thức Fill() của dataAdapter

            da.Fill(ds);

            //b4: Đóng kết nối lại

            cn.Close();

            this.dataGridView1.DataSource = ds.Tables[0];

        }

        #endregion

        #region"phương thức Load"

        private void Form2_Load(object sender, EventArgs e)

        {

            Hienthicauhoi();

        }

        #endregion

    }

}

Bạn đang đọc truyện trên: Truyen2U.Pro