我的代码如下:
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 中。其他信息: 系统错误。===================怎么解决啊???
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 中。其他信息: 系统错误。===================怎么解决啊???
解决方案 »
- C#中字符串转义f问题
- unsafe和fixed可以用于在类的字段中定义指针吗?另外在C#中用什么删除释放指针
- 关于HttpWebRequest.KeepAlive
- 在MDI中,总是同时打开了两个窗口??
- 开发控件的一个小问题
- 请问,c#函数手册这样的书什么书好啊,大家推荐下,谢谢
- 请教两个简单的.Net问题!!在线等待!!!
- 请问开发一个淘宝这样的软件复杂吗?需要那些技术,有什么难点?大约需要多少工作日和钱?
- c#调用c++.dll文件函数!!急!!!!
- 请问在windows程序中可以实现文件上传吗?
- 我有个想法,是否可以把执行C#程序所需要的FRAMEWORK里的文件提取出来然后打包在C#程序的安装包里。
- 一个有趣的问题:大家来看看!
catch
{}
先跟踪一下吧。。把错误信息贴出吧。
加入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,还要加些什么才有错误信息啊???
{
.....
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}看到底show出 的是什么错误。不过
连接字符串中uid不能是空吧?添上sql server的登录名试一下
connStr = "Server = localhost;database = DB;uid =sa;pwd =;";
selectCmd = "SELECT * FROM GOODS";我一会儿会把分加给liufangzhu(方柱),20分不多但对您提供的实质性帮助表示感谢!