码迷,mamicode.com
首页 > Web开发 > 详细

第四章 ADO.NET

时间:2017-11-24 19:06:57      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:init   ons   using   ble   ring   sel   generic   eric   tables   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //定义一个需要访问的数据库的信息
            string strCon = "Server=PC-20161029WDCV\\SQL2014;Database=StudentDB;Trusted_Connection=True";
            SqlConnection con = new SqlConnection(strCon);

            //打开数据库连接
            con.Open();

            string cmdText = "select * from course";
            SqlDataAdapter da = new SqlDataAdapter(cmdText, con);

            //新建一个DataSet,用于接收查询结果
            DataSet ds = new DataSet();
            da.Fill(ds);

            //关闭数据库连接
            con.Close();

            //将查询结果的第一个表赋值给dt
            DataTable dt = ds.Tables[0];

            txtMsg.Text = "课程号    课程名\r\n";
            
            //foreach (DataRow dr in dt.Rows)
            //{
            //    txtMsg.Text += dr[0].ToString() + "   " + dr[1].ToString()+"\r\n";
            //}

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                txtMsg.Text += dt.Rows[i][0] + "    " + dt.Rows[i][1] + "\r\n";
            }

            lblMsg.Text = "共" + dt.Rows.Count + "条数据";


            //txtMsg.Text += dt.Rows[0][0].ToString() + "    "+dt.Rows[0]["课程名"].ToString();

        }
    }
}

 

第四章 ADO.NET

标签:init   ons   using   ble   ring   sel   generic   eric   tables   

原文地址:http://www.cnblogs.com/zhang1997/p/7891622.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!