请大家看看,我哪里出错了

hfkxdwth 2003-08-20 06:07:12
我在c盘Pubs目录下,有一个数据库pubs.mdb,大家可以从下面的地质下在该数据库
http://home.dqt.com.cn/~vodone/pubs.mdb
我通过C#编写了一个程序,如下
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;

namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dg;
private System.Windows.Forms.Button btn_Save;
private OleDbDataAdapter da;
private DataSet ds;
private OleDbConnection dbcon;
private OleDbCommand cmmInsert;

/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
ds=new DataSet();
string constr="Provider=Microsoft.Jet.OleDb.4.0;Password=;User ID=Admin;Data Source=c:\\pubs\\pubs.mdb";
dbcon=new OleDbConnection(constr);
cmmInsert.Connection=this.dbcon;
cmmInsert.CommandText="Insert into authors(au_id,au_fname,au_lname,phone) values (?,?,?,?)";
cmmInsert.Parameters.Add(new OleDbParameter("id",OleDbType.Char,11,ParameterDirection.Input,false,((System.Byte)(0)),((System.Byte)(0)),"au_id",DataRowVersion.Current,null));
cmmInsert.Parameters.Add(new OleDbParameter("fname",OleDbType.Char,20,ParameterDirection.Input,false,((System.Byte)(0)),((System.Byte)(0)),"au_fname",DataRowVersion.Current,null));
cmmInsert.Parameters.Add(new OleDbParameter("lname",OleDbType.Char,40,ParameterDirection.Input,false,((System.Byte)(0)),((System.Byte)(0)),"au_lname",DataRowVersion.Current,null));
cmmInsert.Parameters.Add(new OleDbParameter("phone",OleDbType.Char,12,ParameterDirection.Input,false,((System.Byte)(0)),((System.Byte)(0)),"phone",DataRowVersion.Current,null));
da.InsertCommand=cmmInsert;




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

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dg = new System.Windows.Forms.DataGrid();
this.btn_Save = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dg)).BeginInit();
this.SuspendLayout();
//
// dg
//
this.dg.DataMember = "";
this.dg.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dg.Location = new System.Drawing.Point(24, 16);
this.dg.Name = "dg";
this.dg.Size = new System.Drawing.Size(592, 248);
this.dg.TabIndex = 0;
//
// btn_Save
//
this.btn_Save.Location = new System.Drawing.Point(472, 280);
this.btn_Save.Name = "btn_Save";
this.btn_Save.Size = new System.Drawing.Size(128, 23);
this.btn_Save.TabIndex = 1;
this.btn_Save.Text = "Inset a row";
this.btn_Save.Click += new System.EventHandler(this.btnsave_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(640, 317);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btn_Save,
this.dg});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dg)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
string select="select au_id,au_fname,au_lname,phone from authors";
da=new OleDbDataAdapter(select,dbcon);
da.Fill(ds);
dg.DataSource=ds.Tables[0];


}

private void btnsave_Click(object sender, System.EventArgs e)
{

DataRow r=ds.Tables[0].NewRow();
r["au_id"]="222-22-2222";
r["au_fname"]="王";
r["au_lname"]="廷华";
r["phone"]="801 111-2223";
ds.Tables[0].Rows.Add(r);
da.Update(ds);
this.Close();


}
}
}
======================
运行的时候,系统提示:
未处理的“System.NullReferenceException”类型的异常出现在 WindowsApplication1.exe 中

其他信息:未将对象引用设置到对象的实例。
================
大家可以将该代码复制下来,拷贝到c# windows application中运行,看看
...全文
22 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dahuzizyd 2003-08-20
  • 打赏
  • 举报
回复
在这一句
cmmInsert.Connection=this.dbcon;
前加上上面的代码。
ArEoN 2003-08-20
  • 打赏
  • 举报
回复
对象引用前要实例化,先要new一个实例
cmmInsert = new System.Data.OleDb.OleDbCommand();
引用了该名称空间的话,就不要写全名
dahuzizyd 2003-08-20
  • 打赏
  • 举报
回复
cmmInsert没有初始化,照下面的方式初始化:
cmmInsert= new OleDbCommand();
关于OleDbCommand类的使用可以参考帮助里类库System.Data.OleDb下的OleDbCommad主题
dahuzizyd 2003-08-20
  • 打赏
  • 举报
回复
在那一行出错?

110,500

社区成员

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

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

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