| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1655 人关注过本帖
标题:[求助]数据库的插入、删除、更新操作
只看楼主 加入收藏
dxq8293
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2005-4-10
收藏
 问题点数:0 回复次数:8 
[求助]数据库的插入、删除、更新操作
 求助:关于数据库的插入、删除、更新操作代码的实现
详情请见压缩包,要求数据库数据随着相应操作而改变。
请各位高手帮忙呵,很急。
fxighQUB.rar (10.98 KB) [求助]数据库的插入、删除、更新操作

搜索更多相关主题的帖子: 数据库 删除 详情 
2005-04-29 23:01
幻风幻云
Rank: 1
等 级:新手上路
帖 子:762
专家分:0
注 册:2005-1-14
收藏
得分:0 
晚上刚装的系统

还没装.net,期待高手保佑你

2005-04-29 23:45
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 

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

namespace WindowsApplication4 { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button5; private System.Windows.Forms.Button button6; private System.Windows.Forms.Button button7; private System.Windows.Forms.Button button8; private System.Windows.Forms.Button button9; private System.Data.SqlClient.SqlConnection sqlConnection1; private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1; private System.Data.SqlClient.SqlCommand sqlSelectCommand1; private System.Data.SqlClient.SqlCommand sqlInsertCommand1; private System.Data.DataSet dataSet1; private BindingManagerBase myBind; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null;

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

// // TOD 在 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.groupBox1 = new System.Windows.Forms.GroupBox(); this.textBox3 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button(); this.button6 = new System.Windows.Forms.Button(); this.button7 = new System.Windows.Forms.Button(); this.button8 = new System.Windows.Forms.Button(); this.button9 = new System.Windows.Forms.Button(); this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter(); this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand(); this.dataSet1 = new System.Data.DataSet(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.textBox3); this.groupBox1.Controls.Add(this.textBox2); this.groupBox1.Controls.Add(this.textBox1); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Location = new System.Drawing.Point(8, 8); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(280, 144); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "通讯录"; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(96, 112); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(152, 21); this.textBox3.TabIndex = 6; this.textBox3.Text = ""; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(96, 80); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(152, 21); this.textBox2.TabIndex = 5; this.textBox2.Text = ""; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(96, 48); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(152, 21); this.textBox1.TabIndex = 4; this.textBox1.Text = ""; // // label3 // this.label3.Location = new System.Drawing.Point(8, 112); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(40, 23); this.label3.TabIndex = 2; this.label3.Text = "邮件"; // // label2 // this.label2.Location = new System.Drawing.Point(8, 80); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(48, 24); this.label2.TabIndex = 1; this.label2.Text = "电话"; // // label1 // this.label1.Location = new System.Drawing.Point(8, 48); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(48, 24); this.label1.TabIndex = 0; this.label1.Text = "姓名"; // // button1 // this.button1.Location = new System.Drawing.Point(16, 168); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(56, 23); this.button1.TabIndex = 1; this.button1.Text = "最前"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(88, 168); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(56, 23); this.button2.TabIndex = 2; this.button2.Text = "前一个"; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Location = new System.Drawing.Point(152, 168); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(56, 23); this.button3.TabIndex = 3; this.button3.Text = "后一个"; this.button3.Click += new System.EventHandler(this.button3_Click); // // button4 // this.button4.Location = new System.Drawing.Point(224, 168); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(56, 23); this.button4.TabIndex = 4; this.button4.Text = "最后"; this.button4.Click += new System.EventHandler(this.button4_Click); // // button5 // this.button5.Location = new System.Drawing.Point(16, 216); this.button5.Name = "button5"; this.button5.Size = new System.Drawing.Size(40, 23); this.button5.TabIndex = 5; this.button5.Text = "新建"; this.button5.Click += new System.EventHandler(this.button5_Click); // // button6 // this.button6.Location = new System.Drawing.Point(72, 216); this.button6.Name = "button6"; this.button6.Size = new System.Drawing.Size(40, 23); this.button6.TabIndex = 6; this.button6.Text = "添加"; this.button6.Click += new System.EventHandler(this.button6_Click); // // button7 // this.button7.Location = new System.Drawing.Point(128, 216); this.button7.Name = "button7"; this.button7.Size = new System.Drawing.Size(40, 23); this.button7.TabIndex = 7; this.button7.Text = "修改"; this.button7.Click += new System.EventHandler(this.button7_Click); // // button8 // this.button8.Location = new System.Drawing.Point(184, 216); this.button8.Name = "button8"; this.button8.Size = new System.Drawing.Size(40, 23); this.button8.TabIndex = 8; this.button8.Text = "删除"; this.button8.Click += new System.EventHandler(this.button8_Click); // // button9 // this.button9.Location = new System.Drawing.Point(248, 216); this.button9.Name = "button9"; this.button9.Size = new System.Drawing.Size(40, 23); this.button9.TabIndex = 9; this.button9.Text = "退出"; this.button9.Click += new System.EventHandler(this.button9_Click); // // sqlConnection1 // this.sqlConnection1.ConnectionString = "workstation id=\"NS-121\";packet size=4096;user id=sa;data source=\"NS-121\";persist " + "security info=True;initial catalog=tongxun;password=123456789"; // // sqlDataAdapter1 // this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1; this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1; this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "tx", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("姓名", "姓名"), new System.Data.Common.DataColumnMapping("电话", "电话"), new System.Data.Common.DataColumnMapping("邮件", "邮件")})}); // // sqlInsertCommand1 // this.sqlInsertCommand1.CommandText = "INSERT INTO tx(姓名, 电话, 邮件) VALUES (@姓名, @电话, @邮件); SELECT 姓名, 电话, 邮件 FROM tx"; this.sqlInsertCommand1.Connection = this.sqlConnection1; this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@姓名", System.Data.SqlDbType.VarChar, 10, "姓名")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@电话", System.Data.SqlDbType.VarChar, 15, "电话")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@邮件", System.Data.SqlDbType.VarChar, 20, "邮件")); // // sqlSelectCommand1 // this.sqlSelectCommand1.CommandText = "SELECT 姓名, 电话, 邮件 FROM tx"; this.sqlSelectCommand1.Connection = this.sqlConnection1; // // dataSet1 // this.dataSet1.DataSetName = "NewDataSet"; this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN"); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button9); this.Controls.Add(this.button8); this.Controls.Add(this.button7); this.Controls.Add(this.button6); this.Controls.Add(this.button5); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.groupBox1); this.Name = "Form1"; this.Text = "Form1"; this.groupBox1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit(); this.ResumeLayout(false);

} #endregion

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


我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-04-30 09:49
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
private void DataBinding() { //刷新数据集 this.sqlDataAdapter1.Fill(this.dataSet1,"tx"); //控件数据绑定 this.textBox1.DataBindings.Add("Text",this.dataSet1,"tx.姓名"); this.textBox2.DataBindings.Add("Text",this.dataSet1,"tx.电话"); this.textBox3.DataBindings.Add("Text",this.dataSet1,"tx.邮件");//构造数据访问指针 myBind=this.BindingContext[this.dataSet1,"tx"]; }

private void button1_Click(object sender, System.EventArgs e) { this.myBind.Position=0; }

private void button2_Click(object sender, System.EventArgs e) { if(this.myBind.Position==0) { MessageBox.Show("没有前一条记录了","警告",MessageBoxButtons.OK,MessageBoxIcon.Information ); } else { this.myBind.Position--; } }

private void button3_Click(object sender, System.EventArgs e) { if(this.myBind.Position==this.myBind.Count-1) { MessageBox.Show("没有后一条记录了","警告",MessageBoxButtons.OK,MessageBoxIcon.Information); } else { this.myBind.Position++; } }

private void button4_Click(object sender, System.EventArgs e) { this.myBind.Position=this.myBind.Count-1; }

private void button5_Click(object sender, System.EventArgs e) { this.textBox1.Text=""; this.textBox2.Text=""; this.textBox3.Text="";

}

private void button6_Click(object sender, System.EventArgs e) { try { if(this.textBox1.Text!="" & this.textBox2.Text!="" & this.textBox3.Text!="") { //添加记录的SQL命令 string insert="INSERT INTO tx(姓名,电话,邮件) VALUES ('"; insert+=this.textBox1.Text+"', '"; insert+=this.textBox2.Text+"', '"; insert+=this.textBox3.Text+"') ";

//初始化SqlCommand SqlCommand comm=new SqlCommand(); comm.CommandText=insert; comm.Connection=this.sqlConnection1; //打开数据库连接 this.sqlConnection1.Open(); //执行添加记录的语句 comm.ExecuteNonQuery(); //关闭记录 this.sqlConnection1.Close(); //更新数据集 this.dataSet1.Tables["tx"].Rows[this.myBind.Position].BeginEdit(); this.dataSet1.Tables["tx"].Rows[this.myBind.Position].EndEdit(); this.dataSet1.AcceptChanges();

//成功提示 MessageBox.Show("添加记录成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); this.sqlDataAdapter1.Fill(this.dataSet1,"tx"); } else { //错误提示 MessageBox.Show("请将字段信息输入完整","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } } catch(Exception error) { //错误提示 MessageBox.Show("添加时发生错误","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); //如果打开了连接,则关闭它 if(this.sqlConnection1.State==ConnectionState.Open) { this.sqlConnection1.Close(); } } }

private void button7_Click(object sender, System.EventArgs e) { try { //首先判断添加的信息是束完整 if(this.textBox1.Text!="" & this.textBox2.Text!="" & this.textBox3.Text!="") { //更新记录的SQL语句 string update="UPDATE tx SET "; update += "电话='"; update+= this.textBox2.Text+"', 邮件='"; update+= this.textBox3.Text+"' WHERE 姓名='"; update+= this.textBox1.Text+"'"; //初始化SqlCommand SqlCommand comm=new SqlCommand(); comm.CommandText=update; comm.Connection=this.sqlConnection1; //打开数据库连接 this.sqlConnection1.Open(); //执行添加记录的语句 comm.ExecuteNonQuery(); //关闭记录 this.sqlConnection1.Close(); //更新数据集 this.dataSet1.Tables["tx"].Rows[this.myBind.Position].BeginEdit(); this.dataSet1.Tables["tx"].Rows[this.myBind.Position].EndEdit(); this.dataSet1.AcceptChanges();

//成功提示 MessageBox.Show("修改记录成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); this.sqlDataAdapter1.Fill(this.dataSet1,"tx"); } else { //错误提示 MessageBox.Show("请将字段信息输入完整","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } } catch(Exception error) { //错误提示 MessageBox.Show("修改记录时发生错误","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); //如果打开了连接,则关闭它 if(this.sqlConnection1.State==ConnectionState.Open) { this.sqlConnection1.Close(); } }

}

private void button8_Click(object sender, System.EventArgs e) { if(MessageBox.Show("是否删除记录","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information)==DialogResult.OK) { try { string delete="DELETE From tx WHERE 姓名='"; delete+=this.textBox1.Text+"'";

//初始化SqlCommand SqlCommand comm=new SqlCommand(); comm.CommandText=delete; comm.Connection=this.sqlConnection1; //打开数据库连接 this.sqlConnection1.Open(); //执行添加记录的语句 comm.ExecuteNonQuery(); //关闭记录 this.sqlConnection1.Close(); //更新数据集 this.dataSet1.Tables["tx"].Rows[this.myBind.Position].Delete(); this.dataSet1.AcceptChanges();

//成功提示 MessageBox.Show("删除记录成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); this.sqlDataAdapter1.Fill(this.dataSet1,"tx"); } catch(Exception error) { //错误提示 MessageBox.Show("删除记录时发生错误","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); //如果打开了连接,则关闭它 if(this.sqlConnection1.State==ConnectionState.Open) { this.sqlConnection1.Close(); } } } }

private void button9_Click(object sender, System.EventArgs e) { this.Close(); Application.Exit(); }

} }


我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-04-30 09:50
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
上面两段一起复制过去。
你再根据你要的改一下就行了。
不行千万不要怪我啊。我这是行的啊。

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-04-30 09:51
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
数据库和表不一样。有的你改了。嘎嘎

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-04-30 09:53
超级管理员
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2005-4-28
收藏
得分:0 
还行!
2005-04-30 10:05
dxq8293
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2005-4-10
收藏
得分:0 
谢谢你yushengou,我会试试用这些代码,如果出现问题还希望你能再帮我解决喔
2005-05-02 20:31
dxq8293
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2005-4-10
收藏
得分:0 
yushengou,你给我的代码是在Windows应用程序下设计的,我现在想建立ASP.NET Web应用程序,这些代码应该怎么修改?
2005-05-02 20:38
快速回复:[求助]数据库的插入、删除、更新操作
数据加载中...
 
   



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

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