“黑马程序员”~~VS2010中数据库连接小例

来源:互联网 发布:999银耳钉 知乎 编辑:程序博客网 时间:2024/06/07 12:05

---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! --------------------

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 ADO.NET
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void btnConnetion_Click(object sender, EventArgs e)
        {
            string strcon = @"Data Source=localhost;Initial Catalog=student;Integrated Security=True;";
            SqlConnection conn = new SqlConnection(strcon);
            conn.Open();
           


            string sqlStrSelect = "select * from T1";


            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter(sqlStrSelect, conn);
                DataSet dstable = new DataSet();
                adapter.Fill(dstable, "testTable");
                dataGridView1.DataSource = dstable.Tables["testTable"];
                dataGridView1.Show();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
           

        }


 

 

 

 

 

------------------------ Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------

              详细请查看:http://net.itheima.com/

原创粉丝点击