C#读数据库-SqlDataReader()方法

 1 using System;
 2 using System.Data;
 3 using System.Text;
 4 using System.Windows.Forms;
 5 using System.Data.SqlClient;
 6 namespace UseHasRows
 7 {
 8     public partial class Form1 : Form
 9     {
10         public Form1()
11         {
12             InitializeComponent();
13         }
14         private void button1_Click(object sender, EventArgs e)
15         {
16             try
17             {
18                 SqlConnection conn = new SqlConnection("server=.;database=db_12;uid=sa;pwd=");
19                 conn.Open();
20                 SqlCommand cmd = new SqlCommand("select * from "+textBox1.Text.Trim(), conn);
21                 SqlDataReader sdr = cmd.ExecuteReader();
22                 sdr.Read();
23                 if (sdr.HasRows)
24                 {
25                     MessageBox.Show("数据表中有值"+sdr[0].ToString()+sdr[1].ToString());
26                 }
27                 else
28                 {
29                     MessageBox.Show("数据表中没有任何数据");
30                 }
31                 sdr.Close();
32                 conn.Close();
33             }
34             catch (Exception ex)
35             {
36                 MessageBox.Show(ex.Message);
37             }
38         }
39     }
40 }
原文地址:https://www.cnblogs.com/stringAdmin/p/7599323.html