1. 程式人生 > >C#讀數據庫-SqlDataReader()方法

C#讀數據庫-SqlDataReader()方法

form serve name logs sin bsp area .sh pre

 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 }

C#讀數據庫-SqlDataReader()方法