Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1267999
  • 博文数量: 482
  • 博客积分: 13297
  • 博客等级: 上将
  • 技术积分: 2890
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-12 16:25
文章分类

全部博文(482)

文章存档

2012年(9)

2011年(407)

2010年(66)

分类: WINDOWS

2011-11-23 11:43:10

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10.   
  11. namespace WFAlianxi1  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         private void Form1_Load(object sender, EventArgs e)  
  21.         {  
  22.             const string connectionstring = "Data Source=.;Initial Catalog=student;User ID=sa;PassWord=shenbao";  
  23.             using (SqlConnection conn = new SqlConnection(connectionstring))  
  24.             {  
  25.                 try  
  26.                 {  
  27.                     conn.Open();  
  28.                     DataSet ds = new System.Data.DataSet();  
  29.                     SqlDataAdapter da = new SqlDataAdapter("select *from student", conn);  
  30.                     da.Fill(ds, "student");  
  31.                     da = new SqlDataAdapter("select *from score", conn);  
  32.                     da.Fill(ds, "score");  
  33.                     dataGridView1.AutoGenerateColumns = true;  
  34.                     bindingSource1.DataSource = ds;  
  35.                     bindingSource1.DataMember = "score";  
  36.                 }  
  37.                 catch (SqlException ex)  
  38.                 {  
  39.                     MessageBox.Show("出现错误,{0}", ex.Message);  
  40.                 }  
  41.                 finally  
  42.                 {  
  43.                     if (conn.State == ConnectionState.Open)  
  44.                     {  
  45.                         conn.Close();  
  46.                     }  
  47.                 }  
  48.             }  
  49.         }  
  50.     }  
  51. }  

====

http://blog.csdn.net/nidexuanzhe/article/details/6024303

 

 

阅读(1082) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~