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。
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。
解决方案 »
- TransactionScope问题
- WebBrowser加载网页一直处在Interactive,到不了WebBrowserReadyState.Complete
- 问题:实现一个decimal型舍入的函数
- 进程关闭问题!!!!!!!急 在线等...........
- 学好c#一定要先学好c++吗?..
- 一个DataGrid滚动条的问题.
- HTML控件的问题,请各位帮忙!!!
- 从表示层的datagrid更新了数据后,如何更新到数据库?
- vs2010output窗口缺失debug选项
- 如何操作远程机器上的共享目录!
- 关于临时表
- 听说C#的反射技术挺好的,我想问问大家有人知道如何从一个项目中搜索任何字符串,并且知道它出现的多少遍!
cmd.connection = conn;
那前面费半天劲搞一个Conn.Open有什么用?
嘿嘿嘿!!!
{
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 中。其他信息: 系统错误。
{
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 中。其他信息: 系统错误。
此行绿色显示..
{
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();
}
cmd.Connection=new SqlConnection(strConn);
去掉就可以了!
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();
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();
Microsoft SQL Server2000 的超级用户SA 密码 123456 我建的一个数据库data 其中有user这个表 而且还有记录 , 去掉SqlDataReader Reader=cmd.ExecuteReader();
没出现错误
加上 出现
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。其他信息: 系统错误。
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();
}
}
}
->
cmd.Connection= Conn ;
SqlCommand comm=new SqlCommand("select * from mtable",conn);
conn.open();
SqlDataReader sdr=comm.ExecuteReader();
While(sdr.Read()){
.....
}
sdr.Close();
conn.Close();
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;
}
我的简单时尚
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())
{
}
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;
} }
有成绿的了