首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

ExecuteReader: CommandText 属性尚未初始化解决方法

2012-01-15 
ExecuteReader: CommandText 属性尚未初始化using Systemusing System.Collections.Genericusing System

ExecuteReader: CommandText 属性尚未初始化
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Student
{
  public partial class RPT : Form
  {
  public RPT()
  {
  InitializeComponent();
  }
  private string Rptstrconn;
  private SqlConnection sqlrptconn;
  private SqlDataAdapter sqlrptda;
  private string Rptconnstr="Data Source=WWW-F9EEA54BDCA;Initial Catalog=STUDENT;User ID=sa;Pwd=jxlsqd;Integrated Security=False";
  private SqlCommandBuilder sqlrptcomm;
  private DataSet Rptset = new DataSet();

  private void button1_Click(object sender, EventArgs e)
  {
  Rptstrconn = "select * from REWARD_PUNUSHMENT where STUDENT = " + "'" + txtrpt.Text.ToString().Trim() + "'";
  sqlrptconn = new SqlConnection(this.Rptconnstr);
  sqlrptda = new SqlDataAdapter(this.Rptstrconn, this.sqlrptconn);
  Rptset.Clear();

  sqlrptda.Fill(Rptset, "REWARD_PUNUSHMENT");
  datrpt.DataSource = Rptset.Tables[0];
  }

  private void button5_Click(object sender, EventArgs e)
  {
  int row = this.datrpt.VisibleRowCount;
  this.datrpt.CurrentCell = new DataGridCell(row + 1, 0);
  }

  private void RPT_Load(object sender, EventArgs e)
  {
  Rptstrconn = "select ID as 记录号,STUDENT as 学号,LEVEL as 奖惩代号,TIME as 奖惩时间,DESCRIPTION as 描述 from REWARD_PUNUSHMENT";
  sqlrptconn = new SqlConnection(this.Rptconnstr);
  sqlrptda = new SqlDataAdapter(this.Rptstrconn, this.sqlrptconn);
  Rptset.Clear();

  sqlrptda.Fill(Rptset, "REWARD_PUNUSHMENT");
  datrpt.DataSource = Rptset.Tables[0];
  }

  private void button4_Click(object sender, EventArgs e)
  {
  if (MessageBox.Show("您确定要退出系统吗?", "确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
  this.Close();
  }

  private void button2_Click(object sender, EventArgs e)
  {
  int row = this.datrpt.VisibleRowCount;
  this.datrpt.CurrentCell = new DataGridCell(row + 1, 0);
  if (Rptset.HasChanges())
  {
  sqlrptconn = new SqlConnection(this.Rptconnstr);
  sqlrptda = new SqlDataAdapter(this.Rptstrconn, this.sqlrptconn);
  sqlrptcomm = new SqlCommandBuilder(sqlrptda);
  sqlrptda.Update(Rptset.GetChanges(), "REWARD_PUNUSHMENT");
  Rptset.AcceptChanges();
  MessageBox.Show("信息修改成功!");
  }
  }

  private void button3_Click(object sender, EventArgs e)
  {
  int rowNumber = this.datrpt.CurrentCell.RowNumber;
  //try
  //{
  this.Rptset.Tables[0].Rows[rowNumber].Delete();
  // DataSet Rptset = new DataSet();
  this.sqlrptconn = new SqlConnection(this.Rptconnstr);
  sqlrptda = new SqlDataAdapter("", sqlrptconn);
  sqlrptcomm = new SqlCommandBuilder(sqlrptda);


  sqlrptda.Update(this.Rptset.GetChanges(), "REWARD_PUNUSHMENT"); MessageBox.Show("信息删除成功!");
  //}
  //catch
  //{ MessageBox .Show ("更新失败!");}
  }  
  }
}

  sqlrptda.Update(this.Rptset.GetChanges(), "REWARD_PUNUSHMENT"); 每次都报错 ExecuteReader: CommandText 属性尚未初始化 各位帮我看哈啊 在线等了!!!!!!!!!!

[解决办法]
问题出在你的定义上面。这样的定义每次都要初始化一遍,不管你怎么赋值。一个解决方案是加static关键字。
private static string Rptstrconn;
private static SqlConnection sqlrptconn;
private static SqlDataAdapter sqlrptda;
private static string Rptconnstr="Data Source=WWW-F9EEA54BDCA;Initial Catalog=STUDENT;User ID=sa;Pwd=jxlsqd;Integrated Security=False";
private static SqlCommandBuilder sqlrptcomm;
private static DataSet Rptset = new DataSet(); 

另外一个解决方案是用ViewStat。
[解决办法]
private void button3_Click(object sender, EventArgs e)
{
int rowNumber = this.datrpt.CurrentCell.RowNumber;
this.Rptset.Tables[0].Rows[rowNumber].Delete();
this.sqlrptconn = new SqlConnection(this.Rptconnstr);
sqlrptda = new SqlDataAdapter("", sqlrptconn);//你的查询语句跑那里去了?
sqlrptcomm = new SqlCommandBuilder(sqlrptda);
sqlrptda.Update(this.Rptset.GetChanges(), "REWARD_PUNUSHMENT"); 
MessageBox.Show("信息删除成功!");
}
[解决办法]
你执行的sql语句是空的当然提示没有初始化了。
[解决办法]
不指定查询语句,适配器不知道要更新的是哪张表

热点排行