在SQL Sever2005/2000下创建一个数据库myDB,该数据库中只有一个学生基本信息(StudentInfo)表

来源:互联网 发布:lol2016年度数据回顾 编辑:程序博客网 时间:2024/06/01 07:47
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 Form1 : Form    {        SqlConnection cn;        public Form1()        {            InitializeComponent();        }        public SqlConnection getCN()        {            string strCN = "Data Source=.\\SQLEXPRESS;Integrated Security=True";            cn = new SqlConnection(strCN);            return cn;        }        private void Form1_Load(object sender, EventArgs e)        {            cn = getCN();            try            {                cn.Open();                string strSQL = "select * from Table1";                SqlDataAdapter da = new SqlDataAdapter(strSQL, cn);                DataSet ds = new DataSet();                da.Fill(ds);                dataGridView1.DataSource = ds.Tables[0];                SqlCommand cmd = new SqlCommand(strSQL, cn);                SqlDataReader dr = cmd.ExecuteReader();                while (dr.Read())                {                    if (!listBox1.Items.Contains(dr[1]))                    {                        listBox1.Items.Add(dr[1]);                    }                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }            finally            {                cn.Dispose();            }        }           }}

0 0