| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 970 人关注过本帖
标题:[求助] 高手帮忙 ~ 数据库更新问题
取消只看楼主 加入收藏
hyei1984
Rank: 2
等 级:论坛游民
帖 子:36
专家分:15
注 册:2007-3-26
收藏
 问题点数:0 回复次数:7 
[求助] 高手帮忙 ~ 数据库更新问题

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace 数据库3
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
private System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
private System.Data.OleDb.OleDbConnection oleDbConnection1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private 数据库3.DataSet1 dataSet11;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

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()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.dataSet11 = new 数据库3.DataSet1();
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.DataSource = this.dataSet11.jiben;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 16);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(568, 320);
this.dataGrid1.TabIndex = 0;
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
//
// oleDbSelectCommand1
//
this.oleDbSelectCommand1.CommandText = "SELECT ID, 城市, 密码, 昵称, 年龄, 性别 FROM jiben";
this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
//
// oleDbConnection1
//
this.oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\\\db2.mdb";
//
// oleDbInsertCommand1
//
this.oleDbInsertCommand1.Connection = this.oleDbConnection1;
//
// oleDbUpdateCommand1
//
this.oleDbUpdateCommand1.CommandText = "UPDATE jiben SET 年龄 = '25' WHERE (城市 = '成都')";
this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;

//
// oleDbDataAdapter1
//
this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;
this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;
this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "jiben", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ID", "ID"),
new System.Data.Common.DataColumnMapping("城市", "城市"),
new System.Data.Common.DataColumnMapping("密码", "密码"),
new System.Data.Common.DataColumnMapping("昵称", "昵称"),
new System.Data.Common.DataColumnMapping("年龄", "年龄"),
new System.Data.Common.DataColumnMapping("性别", "性别")})});
this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(72, 368);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 32);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(224, 368);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(144, 40);
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(576, 488);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
this.ResumeLayout(false);

}
#endregion

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

private void button1_Click(object sender, System.EventArgs e)
{
try
{
dataSet11.Clear ();
oleDbDataAdapter1.Fill (dataSet11,"jiben");
}
catch(Exception ex)
{ MessageBox.Show(ex.ToString()); }


}

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

oleDbDataAdapter1.Update (dataSet11, "jiben");
}
catch
(Exception ex)
{ MessageBox.Show(ex.ToString()); }
}
}
}

搜索更多相关主题的帖子: 数据库 
2007-03-29 17:19
hyei1984
Rank: 2
等 级:论坛游民
帖 子:36
专家分:15
注 册:2007-3-26
收藏
得分:0 
我想在  按下 button2 之后 更新数据库  

2007-03-29 17:28
hyei1984
Rank: 2
等 级:论坛游民
帖 子:36
专家分:15
注 册:2007-3-26
收藏
得分:0 
但是 它并没有更新 不知道为什么

2007-03-29 17:29
hyei1984
Rank: 2
等 级:论坛游民
帖 子:36
专家分:15
注 册:2007-3-26
收藏
得分:0 
那我应该怎么做呢

2007-03-29 17:29
hyei1984
Rank: 2
等 级:论坛游民
帖 子:36
专家分:15
注 册:2007-3-26
收藏
得分:0 
是啊  说清楚一点啊  具体怎么操作

2007-03-29 17:31
hyei1984
Rank: 2
等 级:论坛游民
帖 子:36
专家分:15
注 册:2007-3-26
收藏
得分:0 


楼主同志啊;

那你就把修改数据库的语句写在button2中,不就好了吗?

为什么要转那么大的弯呢??

但如果不能更新 写在那不都一样吗?
我现在要先让它能更新

[此贴子已经被作者于2007-3-29 17:36:30编辑过]


2007-03-29 17:33
hyei1984
Rank: 2
等 级:论坛游民
帖 子:36
专家分:15
注 册:2007-3-26
收藏
得分:0 
谢谢

2007-03-29 17:42
hyei1984
Rank: 2
等 级:论坛游民
帖 子:36
专家分:15
注 册:2007-3-26
收藏
得分:0 

在原有的基础上加了着几句 可以了 谢谢 liuminghui

private void button2_Click(object sender, System.EventArgs e)
{
try
{
oleDbConnection1.Open ();
oleDbDataAdapter1.Update (dataSet11, "jiben");

oleDbDataAdapter1.UpdateCommand.ExecuteNonQuery ();
oleDbConnection1.Close ();
dataSet11.Clear ();
oleDbDataAdapter1.Fill (dataSet11,"jiben");

}
catch (Exception ex)
{ MessageBox.Show(ex.ToString()); }
}
}
}


2007-03-29 19:14
快速回复:[求助] 高手帮忙 ~ 数据库更新问题
数据加载中...
 
   



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

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