我的代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;namespace Sql_DataReader1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox rtbShow;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; 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()
{
this.rtbShow = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
// 
// rtbShow
// 
this.rtbShow.Location = new System.Drawing.Point(40, 40);
this.rtbShow.Name = "rtbShow";
this.rtbShow.Size = new System.Drawing.Size(344, 200);
this.rtbShow.TabIndex = 0;
this.rtbShow.Text = "";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(448, 309);
this.Controls.Add(this.rtbShow);
this.Name = "Form1";
this.Text = "Form1";
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)
{
string connStr,selectCmd;
connStr = "Server = localhost;database = DB;uid =sa;pwd =;";
selectCmd = "SELECT * FROM GOODS"; SqlConnection conn;
SqlCommand cmd;
SqlDataReader reader; conn = new SqlConnection(connStr);
conn.Open();
运行后绿条指向这行--> cmd = new SqlCommand(selectCmd,conn); reader = cmd.ExecuteReader();
for(int i = 0;i < reader.FieldCount;i++)
{
rtbShow.Text += reader.GetName(i) + "\t";
}
rtbShow.Text += "\n\n";

while(reader.Read())
{
for(int j = 0;j < reader.FieldCount;j++)
{
rtbShow.Text += reader[j] + "\t";
}
rtbShow.Text += "\n";
}
reader.Close();
conn.Close();
}
}
}
===================执行后出错信息如下:未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。其他信息: 系统错误。===================怎么解决啊???

解决方案 »

  1.   

    你用try-catch来捕获一下,看看是什么错误。
      

  2.   

    用 try catch 先把 异常 捕获到 看看再说
      

  3.   

    你可以先用工具箱中的[数据]拖动一个SqlConnection 组件用向导连接一下数据库,可能是数据库连接字符串格式不对。
      

  4.   

    用try{}
      catch
      {}
    先跟踪一下吧。。把错误信息贴出吧。
      

  5.   

    try,catch,加上就可以运行了,但richTextBox没有连接到数据库!
    加入try,catch后的代码!==========================
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;namespace Sql_DataReader1
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.RichTextBox rtbShow;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; 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()
    {
    this.rtbShow = new System.Windows.Forms.RichTextBox();
    this.SuspendLayout();
    // 
    // rtbShow
    // 
    this.rtbShow.Location = new System.Drawing.Point(40, 40);
    this.rtbShow.Name = "rtbShow";
    this.rtbShow.Size = new System.Drawing.Size(344, 200);
    this.rtbShow.TabIndex = 0;
    this.rtbShow.Text = "";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(448, 309);
    this.Controls.Add(this.rtbShow);
    this.Name = "Form1";
    this.Text = "Form1";
    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)
    {
    try
    {
    string connStr,selectCmd;
    connStr = "Server = localhost;database = DB;uid =;pwd =;";
    selectCmd = "SELECT * FROM GOODS"; SqlConnection conn;
    SqlCommand cmd;
    SqlDataReader reader; conn = new SqlConnection(connStr);
    conn.Open();
    cmd = new SqlCommand(selectCmd,conn); reader = cmd.ExecuteReader();
    for(int i = 0;i < reader.FieldCount;i++)
    {
    rtbShow.Text += reader.GetName(i) + "\t";
    }
    rtbShow.Text += "\n\n";

    while(reader.Read())
    {
    for(int j = 0;j < reader.FieldCount;j++)
    {
    rtbShow.Text += reader[j] + "\t";
    }
    rtbShow.Text += "\n";
    }
    reader.Close();
    conn.Close();
    }
    catch
    {

    }
    }
    }
    }
    ====================
    try,catch,还要加些什么才有错误信息啊???
      

  6.   

    不是加try,catch就行了,你用断点测试,看一下程序是否报错,在catch中加入Messagebox.Show(e.Message);看一下什么问题
      

  7.   

    try
    {
    .....
    }catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }看到底show出 的是什么错误。不过
    连接字符串中uid不能是空吧?添上sql server的登录名试一下
      

  8.   

    呵呵,liufangzhu(方柱)的方法试了一下,提示:数据库不存在或访问被拒绝!!!我想应该是下面代码有问题但怎么解决啊!string connStr,selectCmd;
    connStr = "Server = localhost;database = DB;uid =sa;pwd =;";
    selectCmd = "SELECT * FROM GOODS";我一会儿会把分加给liufangzhu(方柱),20分不多但对您提供的实质性帮助表示感谢!
      

  9.   

    我使用的是登录方式是"windows NT 集成安全性(W)"connStr = "Server = localhost;database = DB;uid =sa;pwd =;";上一行的Server = ;database = ;uid =;pwd =;应该怎么写啊!!!