using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace label
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        public DataTable CX(string sql)
        {
            string connection = "Server=(local);uid=;pwd=";
            SqlDataAdapter DA = new SqlDataAdapter(sql, connection);
            DataTable DT = new DataTable();
            DA.Fill(DT);
            return DT;
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string connection = "Server=(local);uid=;pwd=";
            SqlConnection conn = new SqlConnection(connection);
            conn.Open();
            string comtext="SELECT * FROM [TABLE]";
            SqlCommand com = new SqlCommand(comtext, conn);
            SqlDataReader dr =com.ExecuteReader();
            if (dr.Read())
            {
               DataTable table=new DataTable ();
               this.Text = (string)table.Rows[2][3]; 
            }
            
            }
    }
}

解决方案 »

  1.   

    private void textBox1_TextChanged(object sender, EventArgs e) 
            { 
                string connection = "Server=(local);uid=;pwd="; 
                SqlConnection conn = new SqlConnection(connection); 
                conn.Open(); 
                string comtext="SELECT * FROM [TABLE]"; 
                SqlCommand com = new SqlCommand(comtext, conn); 
                SqlDataReader dr =com.ExecuteReader(); 
                if (dr.Read()) 
                { 
                  DataTable table=new DataTable (); 
                  this.Text = (string)table.Rows[2][3]; 
    这里明显读取不到数据阿
    要么你用dr["column"]; 要么你用上面的SqlDataAdapter填充到dataset里面
                } 
                
                } 
        } 
      

  2.   

    同意楼上的观点。
    你new了一个新的DataTable然后,没有对其填充值,然后你就用第二行第三列的字段,所以程序报错
      

  3.   

     private void textBox1_TextChanged(object sender, EventArgs e) 
            { 
                string connection = "Server=(local);uid=;pwd="; 
                SqlConnection conn = new SqlConnection(connection); 
                conn.Open(); 
                string comtext="SELECT * FROM [TABLE]"; 
                SqlCommand com = new SqlCommand(comtext, conn); 
                SqlDataReader dr =com.ExecuteReader(); 
                if (dr.Read()) 
                { 
                  this.Text = dr.GetString(dr.GetOrdinal("你要绑定的列名"));
                } 
                
                } 
      

  4.   

                  DataTable table=new DataTable (); 
                  this.Text = (string)table.Rows[2][3]; 

    this.Text =dr[列];
      

  5.   

     this.Text = dr[列名].ToString()
      

  6.   

    DataTable table=new DataTable (); 
    this.Text = (string)table.Rows[2][3]; 
    table是刚new 的,里面什么都没有.