C#数据库访问技术之DATAREADER对象读取数据

读出的数据是和SQL执行一样的。

这步完成之后,下步就是DATAAPDAPTER对象啦。。。

暂时不弄了,跟小区朋友打篮球先。。。

 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.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.Data.SqlClient;
11 
12 namespace WindowsFormsApplication5
13 {
14     public partial class Form1 : Form
15     {
16         SqlConnection conn;
17         public Form1()
18         {
19             InitializeComponent();
20         }
21 
22         private void button1_Click(object sender, EventArgs e)
23         {
24             if (textBox1.Text == "")
25             {
26                 MessageBox.Show("Please input database name");
27             }
28             else
29             {
30                 try
31                 {
32                     if (conn.State == ConnectionState.Open || textBox1.Text != "")
33                     {
34                         SqlCommand cmd = new SqlCommand();
35                         cmd.Connection = conn;
36                         cmd.CommandText = "select * from " + textBox1.Text.Trim();
37                         cmd.CommandType = CommandType.Text;
38                         SqlDataReader sdr = cmd.ExecuteReader();
39                         while (sdr.Read())
40                         {
41                             richTextBox1.Text += sdr[0].ToString() + "\t";
42                             richTextBox1.Text += sdr[1].ToString() + "\t";
43                             richTextBox1.Text += sdr[2].ToString() + "\n";
44                         }
45                         conn.Dispose();
46                     }
47                 }
48                     
49                     catch(Exception ex)
50                 {
51                         MessageBox.Show(ex.Message);
52                     }
53             }
54         }
55 
56 
57 
58         private void Form1_Load(object sender, EventArgs e)
59         {
60             string ConStr = "server = .; database= msdb; uid = A; pwd = B";
61             conn = new SqlConnection(ConStr);
62             conn.Open();
63 
64         }
65        }
66                     
67        
68     }

转载于:https://www.cnblogs.com/aguncn/archive/2013/05/12/3073952.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值