错误:未将对象引用设置到对象的实例。

qiguanghao 2006-02-13 06:24:53
部分代码:
public OleDbConnection Con()
{
OleDbConnection DbCon = new OleDbConnection();
string strConnection = "Provider = Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source = "+Server.MapPath ("DB/company.mdb");
DbCon.ConnectionString = strConnection;

return DbCon;
}
public DataSet ReDataSet(string quyString)
{
DataSet ds = new DataSet();
OleDbCommand SelCom = new OleDbCommand();
OleDbDataAdapter DBada = new OleDbDataAdapter();
DBada.SelectCommand.Connection = Con();
Con().Open();
DBada.SelectCommand = SelCom;
DBada.SelectCommand.CommandText=quyString; DBada.SelectCommand.ExecuteNonQuery();
DBada.Fill(ds);
return ds;
}
...全文
102 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiguanghao 2006-02-14
  • 打赏
  • 举报
回复
//全部代码
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Haiwo
{
/// <summary>
/// DataBase 的摘要说明。
/// </summary>
public class DataBase : System.Web.UI.Page
{
private System.Data.OleDb.OleDbCommand DBCom;
public System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
public System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
public System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
public System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
private System.Data.OleDb.OleDbDataAdapter DBAda;
private System.Data.OleDb.OleDbConnection DBCon;
/// <summary>
/// 必需的设计器变量。
/// </summary>


public DataBase(System.ComponentModel.IContainer container)
{
///
/// Windows.Forms 类撰写设计器支持所必需的
///
container.Add(this);
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

public DataBase()
{
///
/// Windows.Forms 类撰写设计器支持所必需的
///
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}


/// <summary>
/// 操作数据
/// </summary>
/// <param name="SqlString">SQL命令</param>
/// <returns>如果操作成功返回真,否则返回假</returns>
public bool OperDB(string SqlString)
{
// try
// {
OleDbCommand SqlCom = new OleDbCommand();

SqlCom.Connection = Con();
// Con().Open();
SqlCom.CommandText = SqlString;
SqlCom.ExecuteNonQuery();
// }
// catch
// {
// return false;
// }

return true;
}

/// <summary>
/// 返回数据库连接
/// </summary>
/// <returns></returns>
public OleDbConnection Con()
{
OleDbConnection DbCon = new OleDbConnection();
string strConnection = "Provider = Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source = "+Server.MapPath("DB/company.mdb");
DbCon.ConnectionString = strConnection;
DbCon.Open();

return DbCon;
}

/// <summary>
/// 以DataSet形式返回,所查询的数据
/// </summary>
/// <param name="quyString">查询字符串</param>
/// <returns></returns>
public DataSet ReDataSet(string quyString)
{
DataSet ds = new DataSet();
OleDbCommand SelCom = new OleDbCommand();
OleDbDataAdapter DBada = new OleDbDataAdapter();

DBada.SelectCommand = SelCom;
DBada.SelectCommand.Connection = Con();
DBada.SelectCommand.CommandText=quyString;

DBada.Fill(ds);

return ds;
}

#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DBCom = new System.Data.OleDb.OleDbCommand();
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
this.DBAda = new System.Data.OleDb.OleDbDataAdapter();
this.DBCon = new System.Data.OleDb.OleDbConnection();
//
// DBAda
//
this.DBAda.DeleteCommand = this.oleDbDeleteCommand1;
this.DBAda.InsertCommand = this.oleDbInsertCommand1;
this.DBAda.SelectCommand = this.oleDbSelectCommand1;
this.DBAda.UpdateCommand = this.oleDbUpdateCommand1;

}
#endregion
}
}
qiguanghao 2006-02-14
  • 打赏
  • 举报
回复
经过我一宿的整改
正确的代码:
public OleDbConnection Con()
{
OleDbConnection DbCon = new OleDbConnection();
string strConnection = "Provider = Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source = "+Server.MapPath("DB/company.mdb");
DbCon.ConnectionString = strConnection;
DbCon.Open();

return DbCon;
} public DataSet ReDataSet(string quyString)
{
DataSet ds = new DataSet();
OleDbCommand SelCom = new OleDbCommand();
OleDbDataAdapter DBada = new OleDbDataAdapter();

DBada.SelectCommand = SelCom;
DBada.SelectCommand.Connection = Con();
DBada.SelectCommand.CommandText=quyString;

DBada.Fill(ds);

return ds;
}
abiho 2006-02-13
  • 打赏
  • 举报
回复
对象和方法要搞清楚
菠菜Hello_World 2006-02-13
  • 打赏
  • 举报
回复
未找到楼主那个错误,但是这两句应该有问题。
DBada.SelectCommand.Connection = Con();
Con().Open();
每次都新建了一个对象。并不是引用的同一个。
lovefootball 2006-02-13
  • 打赏
  • 举报
回复
单步调试

这种错误是使用了没有初始化的变量

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧