读出的数据是和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 }
时间: 2024-10-31 23:53:58