SqlConnection  Conn= new SqlConnection();
strConn="data Source=(local);";
strConn+="Initial Catalog=data;";
strConn+="User id=sa;password=123456;";
Conn.ConnectionString=strConn;
SqlCommand cmd=new SqlCommand();
Conn.Open();
cmd.Connection=new SqlConnection(strConn);
cmd.CommandText="select * from user";

SqlDataReader  Reader=cmd.ExecuteReader();怎么出现:其他信息: ExecuteReader 需要打开的并且可用的连接。该连接的当前状态是 Closed。

解决方案 »

  1.   

    少了一段
    cmd.connection = conn;
      

  2.   

    cmd.Connection=new SqlConnection(strConn);
      

  3.   

    既然有cmd.Connection=new SqlConnection(strConn);
    那前面费半天劲搞一个Conn.Open有什么用?
    嘿嘿嘿!!!
      

  4.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    SqlConnection  Conn= new SqlConnection();
    strConn="data Source=(local);";
    strConn+="Initial Catalog=data;";
    strConn+="User id=sa;password=123456;";
    Conn.ConnectionString=strConn;
    SqlCommand cmd=new SqlCommand();
    Conn.Open();
    cmd.Connection=new SqlConnection(strConn);
    cmd.Connection = Conn;
    cmd.CommandText="select * from user";

    SqlDataReader  Reader=cmd.ExecuteReader();
    }未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。其他信息: 系统错误。
      

  5.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    SqlConnection  Conn= new SqlConnection();
    strConn="data Source=(local);";
    strConn+="Initial Catalog=data;";
    strConn+="User id=sa;password=123456;";
    Conn.ConnectionString=strConn;
    SqlCommand cmd=new SqlCommand();
    Conn.Open();
    cmd.Connection = Conn;
    cmd.CommandText="select * from user";

    SqlDataReader  Reader=cmd.ExecuteReader();
    }
    未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。其他信息: 系统错误。
      

  6.   

    SqlDataReader  Reader=cmd.ExecuteReader();
     此行绿色显示..
      

  7.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    SqlConnection  Conn= new SqlConnection();
    string strConn="data Source=(local);";
    strConn += "Initial Catalog=data;";
    strConn += "User id=sa;password=123456;";
    Conn.ConnectionString = strConn;
    Conn.Open(); SqlCommand cmd=new SqlCommand("select * from user",Conn);
    SqlDataReader  Reader=cmd.ExecuteReader();
    Conn.Close();
    }
      

  8.   

    第二次的
    cmd.Connection=new SqlConnection(strConn);
    去掉就可以了!
      

  9.   

    SqlConnection  Conn= new SqlConnection();
    strConn="data Source=(local);";
    strConn+="Initial Catalog=data;";
    strConn+="User id=sa;password=123456;";//一般默认密码为空或(sa)的
    Conn.ConnectionString=strConn;
                      Conn.Open();
    SqlCommand cmd=new SqlCommand(strConn,Conn);
                      cmd.ExecuteNonQuery();
                      Conn.Close();
      

  10.   

    把下面的做相应修改即可SqlConnection myConnection=new SqlConnection();
    string strConn;
    strConn="server=Localhost;uid=sa;pwd=xiaohua;database=Northwind";
    myConnection.ConnectionString=strConn;
    string strCMD="select CategoryName from ";
    SqlCommand myCommand=new SqlCommand(strCMD,myConnection);
    SqlDataReader myReader;
    myConnection.Open();
    myReader=myCommand.ExecuteReader();
    /* while(myReader.Read())
    {
    drpList.Items.Add(myReader.GetString(0));
        }
    */
        myConnection.Close();
      

  11.   

    感谢大家 所有的代码我都实验了,在我计算机上不可以,我用的是Microsoft Visual Studio .NET 2003+Microsoft SQL Server2000 +windows server 2000  系统的事吧 
    Microsoft SQL Server2000  的超级用户SA  密码 123456  我建的一个数据库data 其中有user这个表 而且还有记录 , 去掉SqlDataReader  Reader=cmd.ExecuteReader();
     没出现错误 
    加上 出现
    未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。其他信息: 系统错误。
      

  12.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;
    namespace Windows
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.PictureBox pictureBox1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
    private string strConn;
    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.pictureBox1 = new System.Windows.Forms.PictureBox();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(72, 40);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(72, 16);
    this.label1.TabIndex = 0;
    this.label1.Text = "用户名";
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(72, 80);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(72, 16);
    this.label2.TabIndex = 0;
    this.label2.Text = "密码";
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(152, 40);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(96, 21);
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "";
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(152, 80);
    this.textBox2.Name = "textBox2";
    this.textBox2.Size = new System.Drawing.Size(96, 21);
    this.textBox2.TabIndex = 1;
    this.textBox2.Text = "";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(112, 128);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(112, 24);
    this.button1.TabIndex = 2;
    this.button1.Text = "登陆";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // pictureBox1
    // 
    this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
    this.pictureBox1.Location = new System.Drawing.Point(0, 0);
    this.pictureBox1.Name = "pictureBox1";
    this.pictureBox1.Size = new System.Drawing.Size(72, 184);
    this.pictureBox1.TabIndex = 3;
    this.pictureBox1.TabStop = false;
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 173);
    this.Controls.Add(this.pictureBox1);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.textBox2);
    this.Name = "Form1";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "登陆窗口";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e)
    {


    } private void button1_Click(object sender, System.EventArgs e)
    {
    SqlConnection  Conn= new SqlConnection();
    strConn="data Source=(local);";
    strConn+="Initial Catalog=data;";
    strConn+="User id=sa;password=123456;";
    Conn.ConnectionString=strConn;
    SqlCommand cmd=new SqlCommand();
    Conn.Open();
    cmd.Connection = Conn;
    cmd.CommandText="select * from user";

    SqlDataReader  Reader=cmd.ExecuteReader();
    }
    }
    }
      

  13.   

    cmd.Connection=new SqlConnection(strConn);
    ->
    cmd.Connection= Conn ;
      

  14.   

    SqlConnection conn=new SqlConnection("server=.;database=test;uid=sa;pwd=sa");
    SqlCommand comm=new SqlCommand("select * from mtable",conn);
    conn.open();
    SqlDataReader sdr=comm.ExecuteReader();
    While(sdr.Read()){
        .....
    }
    sdr.Close();
    conn.Close();
      

  15.   

    SqlCommand cmd=new SqlCommand("",Conn);
      

  16.   

    我想你的是不是因为没有读呢,你试下这个,在页面上加个label1
    SqlConnection  Conn= new SqlConnection();
    strConn="data Source=(local);";
    strConn+="Initial Catalog=data;";
    strConn+="User id=sa;password=123456;";
    Conn.ConnectionString=strConn;
                                  Conn.Open();
            string sel="select * from users";
    SqlCommand cmd=new SqlCommand(sel,Conn);

    SqlDataReader  Reader=cmd.ExecuteReader();
    while(Reader.Read())
    {
    label1.text+=dr["你要显示的字段"].ToString;
    }
      

  17.   

    嘿嘿 
     我的简单时尚
    int Artid= Convert.ToInt32(Request.QueryString["ArticleID"].ToString());
                SqlConnection objconn = new SqlConnection(ConfigurationManager.AppSettings["connstr"]);
                objconn.Open();
                string objsql = "select * from Gzjh where G_id='" + Artid + "'";
                SqlCommand objcmd = new SqlCommand(objsql, objconn);
                SqlDataReader dr = objcmd.ExecuteReader();
                if (dr.Read())
                {
                }
      

  18.   

    SqlConnection myConn = new SqlConnection("server=(local);database=aaa;uid=sa;pwd=;");
         private DataSet selectTable(string str)
            {
                ds = new DataSet();
                try
                {
                    sqlDa = new SqlDataAdapter(str, myConn);
                    myConn.Open();
                    sqlDa.Fill(ds, "movie");
                    myConn.Close();
                    return ds;
                }
                catch
                {
                    MessageBox.Show("连接数据库不成功");
                    return null;
                }        }
      

  19.   

    string sel="select * from user";
    有成绿的了