| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 567 人关注过本帖
标题:[求助]更新数据库出错了!?
只看楼主 加入收藏
c_sharp_man
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2005-9-28
收藏
 问题点数:0 回复次数:2 
[求助]更新数据库出错了!?

在更改数据库中的某一个字段值保存更改后出现错误
"未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 system.data.dll 中。"

源程序如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;


namespace PL2000
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class DataMangerForm : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;

private OleDbConnection _connection;

public DataMangerForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

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

/*============================================================================================================*/
//创建一个数据库链接
public void Connect()
{
try
{
//create a new connection string
string connectionString = string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = {0}; User ID=;Password=;","D:\\cssamples\\database\\pl2000.mdb");
//create a connection
OleDbConnection newConnection = new OleDbConnection(connectionString);
//Try and open it
newConnection.Open();
//Store it for use
Connection = newConnection;

//MessageBox.Show("The file opened success!");
}
catch(Exception ex)
{
//report the problem
HandleException("A conncetion could not be made.",ex);
//MessageBox.Show("The file could not be open!");

}

}
/*=============================================================================================================*/
//Connection 属性
public OleDbConnection Connection
{
get
{
return _connection;
}

set
{
//Disconnect
Disconnect();
_connection = value;
}

}
/*=============================================================================================================*/
//Disconnect方法
public void Disconnect()
{
//Do we have a connection?
if( _connection != null )
{
//Is it open?
if( _connection.State !=ConnectionState.Closed )
{
_connection.Close();
}
//Clear it
_connection = null;
}

}

/*=============================================================================================================*/
//Handler Exception
public void HandleException(string message,Exception ex)
{
//Display a message box...
MessageBox.Show(this,string.Format("{0}\n{1}:{2}",message,ex.ToString(),ex.Message) );

}
/*=============================================================================================================*/
//调用数据库的数据
public void Load_Data()
{
//Do we have a connection?
if(_connection == null )
{
MessageBox.Show("You must connect to a database!");
return;
}
//Setup...
OleDbCommand command = null;
OleDbDataAdapter adapter = null;
try
{
//Create a command...
command = _connection.CreateCommand();

//Configure a command
command.CommandText="PL2000";
command.CommandType=CommandType.TableDirect;

//Create a adapter
adapter = new OleDbDataAdapter(command);

//create and fill a database...
DataSet dataset = new DataSet();
adapter.Fill(dataset);

//Show the database
dataGrid1.DataSource=dataset;
}
catch(Exception ex)
{
//Peport the problem...
HandleException("The data could not be load",ex);
}
finally
{
//Clear up...
if(adapter != null)
{
adapter.Dispose();
}
if(command != null)
{
command.Dispose();
}
}

}
/*=============================================================================================================*/
//保存数据更改
public void SaveChanges()
{
//Do we have a connection?
if(_connection == null)
{
MessageBox.Show("You must connect a database!");
return;
}
//Get the database
DataSet dataset = (DataSet)dataGrid1.DataSource;

if(dataset == null)
{
MessageBox.Show("You must load a dataset");
return;
}
//Create...
OleDbCommand command = null;
OleDbDataAdapter adapter = null;
try
{
//Create the command...
command = _connection.CreateCommand();

//Populate...
command.CommandText="pl2000";
command.CommandType=CommandType.TableDirect;

//Create a adapter
adapter = new OleDbDataAdapter(command);

//Now create a command builder
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);

//Update
adapter.Update(dataset);

//Tell the user
MessageBox.Show("Change have been saved");
}
finally
{
//Clear up...
if(adapter != null)
{
adapter.Dispose();
}
if(command != null)
{
command.Dispose();
}
}
}
/*=============================================================================================================*/

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
//Do we have a connection?
Disconnect();

if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Top;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(752, 320);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(240, 336);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "保存更改";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(480, 336);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "关 闭";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(360, 336);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "打 印";
//
// DataMangerForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(752, 390);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.MaximizeBox = false;
this.Name = "DataMangerForm";
this.Text = "数据管理";
this.Load += new System.EventHandler(this.Form2_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

private void Form2_Load(object sender, System.EventArgs e)
{
Connect();
Load_Data();
}

private void button2_Click(object sender, System.EventArgs e)
{
Dispose(true);
}

private void button1_Click(object sender, System.EventArgs e)
{
SaveChanges();
}
}
}
/*==================================================================================*/

搜索更多相关主题的帖子: 数据库 
2006-03-16 14:53
jyplove
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-12-5
收藏
得分:0 
adapter.Update(dataset);
应该指出是对那个表的更改吧

2006-03-16 17:41
唐伯猫
Rank: 8Rank: 8
等 级:贵宾
威 望:45
帖 子:5323
专家分:58
注 册:2005-8-9
收藏
得分:0 
上面带有注释,却出现错误自己不知道怎么改.一看就知道仿写别人的代码.

一般都是这里出错: "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = {0}; User ID=;Password=;","D:\\cssamples\\database\\pl2000.mdb");
你再看看.

<iframe name="alimamaifrm" frameborder="0" marginheight="0" marginwidth="0" border="0" scrolling="no" width="300" height="170" src="/go/app/tbk_app/chongzhi_300_170.php?pid=mm_28854300_2441872_11377541&page=chongzhi_300_170.php&size_w=300&size_h=170&stru_phone=1&stru_game=1&stru_travel=1" ></iframe>
2006-03-16 17:58
快速回复:[求助]更新数据库出错了!?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016555 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved