| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 579 人关注过本帖
标题:[求助]dataGrid
只看楼主 加入收藏
笨笨熊
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-8-16
收藏
 问题点数:0 回复次数:7 
[求助]dataGrid
大家好:
  有关的dataGrid代码谁还有更多,就是那种在dataGrid:添加,删除,修改.......
搜索更多相关主题的帖子: dataGrid 
2005-08-16 10:59
梦幻情缘
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:769
专家分:20
注 册:2005-4-4
收藏
得分:0 
在本篇文章中,我们将介绍Visual C#对数据库的一个基本操作,即:如何往数据库中添加记录。我们将通过一些数据库操作的例子,来具体说明一下。为了更清楚的说明这个问题,在选用数据库方面采用了二种当前比较典型的数据库,其一是本地数据库--Access 2000,另外一个是远程数据库--SQL SERVER 7.0。首先介绍如何用Visual C#来添加Access 2000数据库的记录。

一.用Visual C#来添加Access 2000数据库的记录
(一).程序设计和运行的环境设置:
(1)视窗2000服务器版
(2)Microsoft Data Acess Component 2.6 以上版本 ( MDAC 2.6 )
(3)本文程序使用的数据库的介绍:

程序中使用的数据库名称为sample.mdb,在此数据库中有一张数据表books。此数据表的结构如下:
字段名称 字段类型 代表意思
Bookid 数字 序号
booktitle 文本 书籍名称
bookauthor 文本 书籍作者
bookprice 数字 价格
bookstock 数字 书架号

(二).程序设计难点和应该注意的问题:
如何正确的往数据库中添加记录是本文要讨论的一个重点和难点,下面就是解决这一问题的具体思路:
(1)创建并打开一个 OleDbConnection对象。
(2)创建一个插入一条记录的SQL语句。
(3)创建一个OleDbCommand对象。
(4)通过此OleDbCommand对象完成对插入一条记录到数据库的操作。
以下是在程序中实现的具体语句:
string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;
OleDbConnection myConn = new OleDbConnection ( strConn ) ;
myConn.Open ( ) ;
string strInsert = " INSERT INTO books ( bookid , booktitle , bookauthor , bookprice , bookstock ) VALUES ( " ;
strInsert += t_bookid.Text + ", '" ;
strInsert += t_booktitle.Text + "', '" ;
strInsert += t_bookauthor.Text + "', " ;
strInsert += t_bookprice.Text + ", " ;
strInsert += t_bookstock.Text + ")" ;
OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;
inst.ExecuteNonQuery ( ) ;
myConn.Close ( ) ;


(三).用Visual C#来插入记录的程序源代码( add.cs )和执行后的界面:
下图是add.cs编译后的执行界面:



add.cs源程序代码:
using System ;
using System.Drawing ;
using ;
using System.Windows.Forms ;
using System.Data.OleDb ;
using System.Data ;
//导入程序中使用到的名称空间
public class DataAdd : Form {
private Button lastrec ;
private Button nextrec ;
private Button previousrec ;
private Button firstrec ;
private Container components ;
private Label title ;
private Button t_new ;
private Button save ;
private TextBox t_bookstock ;
private TextBox t_bookprice ;
private TextBox t_bookauthor ;
private TextBox t_booktitle ;
private TextBox t_bookid ;
private Label l_bookstock ;
private Label l_bookprice ;
private Label l_bookauthor ;
private Label l_booktitle ;
private Label l_bookid ;
private DataSet myDataSet ;
private BindingManagerBase myBind ;
//定义在程序中要使用的组件
public DataAdd ( ) {
//连接到一个数据库
GetConnected ( ) ;
// 对窗体中所需要的内容进行初始化
InitializeComponent ( );
}
//释放程序使用过的所以资源
public override void Dispose ( ) {
base.Dispose ( ) ;
components.Dispose ( ) ;
}
public static void Main ( ) {
Application.Run ( new DataAdd ( ) ) ;
}
public void GetConnected ( )
{
try{
//创建一个 OleDbConnection对象
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM books " ;
//创建一个 DataSet
myDataSet = new DataSet ( ) ;
myConn.Open ( ) ;
//用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
//把Dataset绑定books数据表
myCommand.Fill ( myDataSet , "books" ) ;
//关闭此OleDbConnection
myConn.Close ( ) ;
}
catch ( Exception e )
{
MessageBox.Show ( "连接错误! " + e.ToString ( ) , "错误" ) ;
}
}
private void InitializeComponent ( )
{
components = new ( ) ;
nextrec = new Button ( ) ;
lastrec = new Button ( ) ;
previousrec = new Button ( ) ;
firstrec = new Button ( ) ;
t_bookprice = new TextBox ( ) ;
l_booktitle = new Label ( ) ;
l_bookprice = new Label ( ) ;
l_bookauthor = new Label ( ) ;
t_bookid = new TextBox ( ) ;
save = new Button ( ) ;
title = new Label ( ) ;
t_bookauthor = new TextBox ( ) ;
t_booktitle = new TextBox ( ) ;
t_new = new Button ( ) ;
l_bookstock = new Label ( ) ;
t_bookstock = new TextBox ( ) ;
l_bookid = new Label ( ) ;
//以下是对数据浏览的四个按钮进行初始化
firstrec.Location = new System.Drawing.Point ( 65 , 312 ) ;
firstrec.ForeColor = System.Drawing.Color.Black ;
firstrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
firstrec.Font = new System.Drawing.Font("仿宋", 8f );
firstrec.Text = "首记录";
firstrec.Click += new System.EventHandler(GoFirst);
previousrec.Location = new System.Drawing.Point ( 135 , 312 ) ;
previousrec.ForeColor = System.Drawing.Color.Black ;
previousrec.Size = new System.Drawing.Size(40, 24) ;
previousrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
previousrec.Text = "上一条" ;
previousrec.Click += new System.EventHandler ( GoPrevious ) ;
nextrec.Location = new System.Drawing.Point ( 205 , 312 );
nextrec.ForeColor = System.Drawing.Color.Black ;
nextrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
nextrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
nextrec.Text = "下一条" ;
nextrec.Click += new System.EventHandler ( GoNext );
lastrec.Location = new System.Drawing.Point ( 275 , 312 ) ;
lastrec.ForeColor = System.Drawing.Color.Black ;
lastrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
lastrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;
lastrec.Text = "尾记录" ;
lastrec.Click += new System.EventHandler ( GoLast ) ;
//以下是对显示标签进行初始化
l_bookid.Location = new System.Drawing.Point ( 24 , 56 ) ;
l_bookid.Text = "书本序号:" ;
l_bookid.Size = new System.Drawing.Size ( 112, 20 ) ;
l_bookid.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
l_bookid.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
l_booktitle.Location = new System.Drawing.Point ( 24 , 108 ) ;
l_booktitle.Text = "书 名:";
l_booktitle.Size = new System.Drawing.Size ( 112 , 20 ) ;
l_booktitle.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
l_booktitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
l_bookprice.Location = new System.Drawing.Point ( 24 , 212 ) ;
l_bookprice.Text = "价 格:" ;
l_bookprice.Size = new System.Drawing.Size ( 112 , 20 ) ;
l_bookprice.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
l_bookprice.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
l_bookstock.Location = new System.Drawing.Point ( 24 , 264 ) ;
l_bookstock.Text = "书 架 号:" ;
l_bookstock.Size = new System.Drawing.Size ( 112 , 20 ) ;
l_bookstock.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
l_bookstock.TabIndex = 16 ;
l_bookstock.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
l_bookauthor.Location = new System.Drawing.Point ( 24 , 160 ) ;
l_bookauthor.Text = "作 者:" ;
l_bookauthor.Size = new System.Drawing.Size ( 112 , 20 ) ;
l_bookauthor.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;
l_bookauthor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
title.Location = new System.Drawing.Point ( 32 , 16 ) ;
title.Text = "利用Vsiual C#来增加数据记录!" ;
title.Size = new System.Drawing.Size ( 336 , 24 ) ;
title.ForeColor = System.Drawing.Color.Green ;
title.Font = new System.Drawing.Font ( "仿宋" , 14f , System.Drawing.FontStyle.Bold ) ;
//以下是对为显示数据记录而设定的标签和文本框进行初始化,并把记录绑定在不同的绑定到文本框"Text"属性上
t_bookid.Location = new System.Drawing.Point ( 184 , 56 ) ;
t_bookid.Size = new System.Drawing.Size ( 80 , 20 ) ;
t_bookid.DataBindings.Add ( "Text" , myDataSet , "books.bookid" ) ;
t_bookstock.Location = new System.Drawing.Point ( 184 , 264 ) ;
t_bookstock.Size = new System.Drawing.Size ( 80 , 20 ) ;
t_bookstock.DataBindings.Add ( "Text" , myDataSet , "books.bookstock" ) ;
t_booktitle.Location = new System.Drawing.Point ( 184 , 108 ) ;
t_booktitle.Size = new System.Drawing.Size ( 176 , 20 ) ;
t_booktitle.DataBindings.Add( "Text" , myDataSet , "books.booktitle" ) ;
t_bookprice.Location = new System.Drawing.Point ( 184 , 212 ) ;
t_bookprice.Size = new System.Drawing.Size ( 80 , 20 ) ;
t_bookprice.DataBindings.Add ( "Text" , myDataSet , "books.bookprice" ) ;
t_bookauthor.Location = new System.Drawing.Point ( 184 , 160 ) ;
t_bookauthor.Size = new System.Drawing.Size ( 128 , 20 ) ;
t_bookauthor.DataBindings.Add ( "Text" , myDataSet , "books.bookauthor" ) ;
t_new.Location = new System.Drawing.Point ( 62 , 354 ) ;
t_new.Size = new System.Drawing.Size ( 96 , 32 ) ;
t_new.Text = "新建记录" ;
t_new.Click += new System.EventHandler ( t_newClick ) ;
save.Location = new System.Drawing.Point ( 222 , 354 ) ;
save.Size = new System.Drawing.Size ( 96 , 32 ) ;
save.TabIndex = 4 ;
save.Text = "保存记录" ;
save.Click += new System.EventHandler ( saveClick ) ;
this.Text = "利用Vsiual C#来增加数据记录的程序窗口!" ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
this.FormBorderStyle = FormBorderStyle.Fixed3D ;
this.ClientSize = new System.Drawing.Size ( 390 , 400 ) ;
//在窗体中加入下列组件
this.Controls.Add ( lastrec ) ;
this.Controls.Add ( nextrec ) ;
this.Controls.Add ( previousrec ) ;
this.Controls.Add ( firstrec ) ;
this.Controls.Add ( title ) ;
this.Controls.Add ( t_new ) ;
this.Controls.Add ( save ) ;
this.Controls.Add ( t_bookstock ) ;
this.Controls.Add ( t_bookprice ) ;
this.Controls.Add ( t_bookauthor ) ;
this.Controls.Add ( t_booktitle ) ;
this.Controls.Add ( t_bookid ) ;
this.Controls.Add ( l_bookstock ) ;
this.Controls.Add ( l_bookprice ) ;
this.Controls.Add ( l_bookauthor ) ;
this.Controls.Add ( l_booktitle ) ;
this.Controls.Add ( l_bookid ) ;
//把对象DataSet和"books"数据表绑定到此myBind对象
myBind= this.BindingContext [ myDataSet , "books" ] ;
}
protected void saveClick ( object sender , System.EventArgs e )
{
try
{
//判断所有字段是否添完,添完则执行,反之弹出提示
if ( t_bookid.Text != "" && t_booktitle.Text != "" && t_bookauthor.Text != "" && t_bookprice.Text != "" && t_bookstock.Text != "" )
{
string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;
OleDbConnection myConn = new OleDbConnection ( strConn ) ;
myConn.Open ( ) ;
string strInsert = " INSERT INTO books ( bookid , booktitle , bookauthor , bookprice , bookstock ) VALUES ( " ;
strInsert += t_bookid.Text + ", '" ;
strInsert += t_booktitle.Text + "', '" ;
strInsert += t_bookauthor.Text + "', " ;
strInsert += t_bookprice.Text + ", " ;
strInsert += t_bookstock.Text + ")" ;
OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;
inst.ExecuteNonQuery ( ) ;
myConn.Close ( ) ;
}
else
{
MessageBox.Show ( "必须填满所有字段值!" , "错误!" ) ;
}
}
catch ( Exception ed )
{
MessageBox.Show ( "保存数据记录发生 " + ed.ToString ( ) , "错误!" ) ;
}
}
protected void t_newClick ( object sender , System.EventArgs e )
{
t_bookid.Text = "" ;
t_booktitle.Text = "" ;
t_bookauthor.Text = "" ;
t_bookprice.Text = "" ;
t_bookstock.Text = "" ;
}
//按钮"尾记录"对象事件程序
protected void GoLast ( object sender , System.EventArgs e )
{
myBind.Position = myBind.Count - 1 ;
}

//按钮"下一条"对象事件程序
protected void GoNext ( object sender , System.EventArgs e )
{
if ( myBind.Position == myBind.Count -1 )
MessageBox.Show ( "已经到了最后一条记录!" ) ;
else
myBind.Position += 1 ;
}
//按钮"上一条"对象事件程序
protected void GoPrevious ( object sender , System.EventArgs e )
{
if ( myBind.Position == 0 )
MessageBox.Show ( "已经到了第一条记录!" ) ;
else
myBind.Position -= 1 ;
}
//按钮"首记录"对象事件程序
protected void GoFirst ( object sender , System.EventArgs e )
{
myBind.Position = 0 ;
}
}


成功编译上面代码,就可以往Access 2000数据库里面插入记录了。
2005-08-16 11:34
梦幻情缘
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:769
专家分:20
注 册:2005-4-4
收藏
得分:0 
using System ;
using System.Drawing ;
using ;
using System.Windows.Forms ;
using System.Data.OleDb ;
using System.Data ;
public class DataEdit : Form { private components ;
private Button delete ;
private Button update ;
private Button lastrec ;
private Button nextrec ;
private Button previousrec ;
private Button firstrec ;
private TextBox t_bookstock ;
private TextBox t_bookprice ;
private TextBox t_bookauthor ;
private TextBox t_booktitle ;
private TextBox t_bookid ;
private Label l_bookstock ;
private Label l_bookprice ;
private Label l_bookauthor ;
private Label l_booktitle ;
private Label l_bookid ;
private Label label1 ;
private System.Data.DataSet myDataSet ;
private BindingManagerBase myBind ;
private bool isBound = false ;
//定义此变量,是判断组件是否已经绑定数据表中的字段

public DataEdit ( ) {
// 对窗体中所需要的内容进行初始化
InitializeComponent ( ) ;
//连接到一个数据库
GetConnected ( ) ;
}
//清除程序中用到的所有资源
public override void Dispose ( ) {
base.Dispose ( ) ;
components.Dispose ( ) ;
}
public void GetConnected ( )
{
try{
//创建一个 OleDbConnection对象
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM books " ;
//创建一个 DataSet对象
myDataSet = new DataSet ( ) ;
myConn.Open ( ) ;
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
myCommand.Fill ( myDataSet , "books" ) ;
myConn.Close ( ) ;
//判断数据字段是否绑定到 TextBoxes
if ( !isBound )
{
//以下是为显示数据记录而把数据表的某个字段绑定在不同的绑定到文本框"Text"属性上
t_bookid.DataBindings.Add ( "Text" , myDataSet , "books.bookid" ) ;
t_booktitle.DataBindings.Add ( "Text" , myDataSet , "books.booktitle" ) ;
t_bookauthor.DataBindings.Add ( "Text" , myDataSet , "books.bookauthor" ) ;
t_bookprice.DataBindings.Add ( "Text" , myDataSet , "books.bookprice" ) ;
t_bookstock.DataBindings.Add ( "Text" , myDataSet , "books.bookstock" ) ;
//设定 BindingManagerBase
//把对象DataSet和"books"数据表绑定到此myBind对象
myBind = this.BindingContext [ myDataSet , "books" ] ;
isBound = true ;
}
}
catch ( Exception e )
{
MessageBox.Show ( "连接数据库发生错误为:" + e.ToString ( ) , "错误!" ) ;
}
}
public static void Main ( ) {
Application.Run ( new DataEdit ( ) ) ;
}
private void InitializeComponent ( )
{
= new ( ) ;
this.t_bookid = new TextBox ( ) ;
this.previousrec = new Button ( ) ;
this.l_bookauthor = new Label ( ) ;
this.delete = new Button ( ) ;
this.t_booktitle = new TextBox ( ) ;
this.t_bookauthor = new TextBox ( ) ;
this.t_bookprice = new TextBox ( ) ;
this.l_bookprice = new Label ( ) ;
this.t_bookstock = new TextBox ( ) ;
this.l_bookstock = new Label ( ) ;
this.l_booktitle = new Label ( ) ;
this.update = new Button ( ) ;
this.nextrec = new Button ( ) ;
this.lastrec = new Button ( ) ;
this.firstrec = new Button ( ) ;
this.label1 = new Label ( ) ;
this.l_bookid = new Label ( ) ;
t_bookid.Location = new System.Drawing.Point ( 184 , 56 ) ;
t_bookid.Size = new System.Drawing.Size ( 80 , 20 ) ;

t_booktitle.Location = new System.Drawing.Point ( 184 , 108 ) ;
t_booktitle.Size = new System.Drawing.Size ( 176 , 20 ) ;

t_bookauthor.Location = new System.Drawing.Point ( 184 , 160 ) ;
t_bookauthor.Size = new System.Drawing.Size ( 128 , 20 ) ;

t_bookprice.Location = new System.Drawing.Point ( 184 , 212 ) ;
t_bookprice.Size = new System.Drawing.Size ( 80 , 20 ) ;

t_bookstock.Location = new System.Drawing.Point ( 184 , 264 ) ;
t_bookstock.Size = new System.Drawing.Size ( 80 , 20 ) ;
//以下是设定在程序中使用到的Label属性
l_bookid.Location = new System.Drawing.Point ( 24 , 56 ) ;
l_bookid.Text = "序 号:" ;
l_bookid.Size = new System.Drawing.Size ( 142 , 20 ) ;
l_bookid.Font = new System.Drawing.Font ( "宋体" , 12f ) ;
l_bookid.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
l_booktitle.Location = new System.Drawing.Point ( 24 , 108 ) ;
l_booktitle.Text = "书 名:" ;
l_booktitle.Size = new System.Drawing.Size ( 142 , 20 ) ;
l_booktitle.Font = new System.Drawing.Font ( "宋体" , 12f ) ;
l_booktitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
l_bookauthor.Location = new System.Drawing.Point ( 24 , 160 ) ;
l_bookauthor.Text = "作 者:";
l_bookauthor.Size = new System.Drawing.Size ( 142 , 20 ) ;
l_bookauthor.Font = new System.Drawing.Font ( "宋体" , 12f ) ;
l_bookauthor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
l_bookprice.Location = new System.Drawing.Point ( 24 , 212 ) ;
l_bookprice.Text = "价 格:" ;
l_bookprice.Size = new System.Drawing.Size ( 142 , 20 ) ;
l_bookprice.Font = new System.Drawing.Font ( "宋体" , 12f ) ;
l_bookprice.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;

l_bookstock.Location = new System.Drawing.Point ( 24 , 264 ) ;
l_bookstock.Text = "书 架 号:" ;
l_bookstock.Size = new System.Drawing.Size ( 142 , 20 ) ;
l_bookstock.Font = new System.Drawing.Font ( "宋体" , 12f ) ;
l_bookstock.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;

//以下设定程序中用到的功能按钮的属性及对应的事件
delete.Location = new System.Drawing.Point ( 104 , 352 ) ;
delete.ForeColor = System.Drawing.Color.Black ;
delete.Size = new System.Drawing.Size ( 80 , 24 ) ;
delete.Font = new System.Drawing.Font ( "宋体" , 9f ) ;
delete.Text = "删除记录" ;
delete.Click += new System.EventHandler ( GoDelete ) ;

update.Location = new System.Drawing.Point ( 204 , 352 ) ;
update.ForeColor = System.Drawing.Color.Black ;
update.Size = new System.Drawing.Size ( 80 , 24 ) ;
update.Font = new System.Drawing.Font ( "宋体" , 9f ) ;
update.Text = "修改记录" ;
update.Click += new System.EventHandler ( GoUpdate ) ;

firstrec.Location = new System.Drawing.Point ( 64 , 312 ) ;
firstrec.ForeColor = System.Drawing.Color.Black ;
firstrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
firstrec.Font = new System.Drawing.Font ( "宋体" , 9f ) ;
firstrec.Text = "首记录" ;
firstrec.Click += new System.EventHandler ( GoFirst ) ;

previousrec.Location = new System.Drawing.Point ( 136 , 312 ) ;
previousrec.ForeColor = System.Drawing.Color.Black ;
previousrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
previousrec.Font = new System.Drawing.Font ( "宋体" , 9f ) ;
previousrec.Text = "上一条" ;
previousrec.Click += new System.EventHandler ( GoPrevious ) ;

nextrec.Location = new System.Drawing.Point ( 208 , 312 ) ;
nextrec.ForeColor = System.Drawing.Color.Black ;
nextrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
nextrec.Font = new System.Drawing.Font ( "宋体" , 9f ) ;
nextrec.Text = "下一条" ;
nextrec.Click += new System.EventHandler ( GoNext ) ;

lastrec.Location = new System.Drawing.Point ( 280 , 312 ) ;
lastrec.ForeColor = System.Drawing.Color.Black ;
lastrec.Size = new System.Drawing.Size ( 40 , 24 ) ;
lastrec.Font = new System.Drawing.Font ( "宋体" , 9f ) ;
lastrec.Text = "尾记录" ;
lastrec.Click += new System.EventHandler ( GoLast ) ;

label1.Location = new System.Drawing.Point ( 60 , 20 ) ;
label1.Text = "用Visual C#来修改和删除数据库中的记录" ;
label1.Size = new System.Drawing.Size ( 296 , 24 ) ;
label1.ForeColor = System.Drawing.SystemColors.Desktop ;
label1.Font = new System.Drawing.Font ( "宋体" , 14f ) ;
//设定程序的主窗体的属性
this.Text = "用Visual C#来修改和删除数据库中的记录!" ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
this.FormBorderStyle = FormBorderStyle.FixedSingle ;
this.ClientSize = new System.Drawing.Size ( 394 , 425 ) ;
//在主窗体中加入组件
this.Controls.Add ( delete ) ;
this.Controls.Add ( update ) ;
this.Controls.Add ( lastrec ) ;
this.Controls.Add ( nextrec ) ;
this.Controls.Add ( previousrec ) ;
this.Controls.Add ( firstrec ) ;
this.Controls.Add ( t_bookstock ) ;
this.Controls.Add ( t_bookprice ) ;
this.Controls.Add ( t_bookauthor ) ;
this.Controls.Add ( t_booktitle ) ;
this.Controls.Add ( t_bookid ) ;
this.Controls.Add ( l_bookstock ) ;
this.Controls.Add ( l_bookprice ) ;
this.Controls.Add ( l_bookauthor ) ;
this.Controls.Add ( l_booktitle ) ;
this.Controls.Add ( l_bookid ) ;
this.Controls.Add ( label1 ) ;

}
//"删除记录"对应的事件
protected void GoDelete ( object sender, System.EventArgs e )
{
try{
//连接到一个数据库
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn.Open ( ) ;
string strDele = "DELETE FROM books WHERE bookid= " + t_bookid.Text ;
OleDbCommand myCommand = new OleDbCommand ( strDele , myConn ) ;
//从数据库中删除指定记录
myCommand.ExecuteNonQuery ( ) ;
//从DataSet中删除指定记录
myDataSet.Tables [ "books" ] . Rows [ myBind.Position ] . Delete ( ) ;
myDataSet.Tables [ "books" ] . AcceptChanges ( ) ;
myConn.Close ( ) ;
}
catch ( Exception ed )
{
MessageBox.Show ( "删除记录错误信息: " + ed.ToString ( ) , "错误!" ) ;
}
}
//"修改记录"按钮对应的事件
protected void GoUpdate ( object sender , System.EventArgs e )
{
int i = myBind.Position ;
try{
//连接到一个数据库
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn.Open ( ) ;

//从数据库中修改指定记录
string strUpdt = " UPDATE books SET booktitle = '"
+ t_booktitle.Text + "' , bookauthor = '"
+ t_bookauthor.Text + "' , bookprice = "
+ t_bookprice.Text + " , bookstock = "
+ t_bookstock.Text + " WHERE bookid = " + t_bookid.Text ;

OleDbCommand myCommand = new OleDbCommand ( strUpdt , myConn ) ;
myCommand.ExecuteNonQuery ( ) ;
myConn.Close ( ) ;
}
catch ( Exception ed )
{
MessageBox.Show ( "修改指定记录错误: " + ed.ToString ( ) , "错误!" ) ;
}
myBind.Position = i ;
}
//"尾记录"按钮对应的事件
protected void GoLast ( object sender , System.EventArgs e )
{
myBind.Position = myBind.Count - 1 ;
}
//"下一条"按钮对应的事件
protected void GoNext ( object sender , System.EventArgs e )
{
if ( myBind.Position == myBind.Count - 1 )
MessageBox.Show ( "已经到尾记录!" ) ;
else
myBind.Position += 1 ;
}
//"上一条"按钮对应的事件
protected void GoPrevious ( object sender , System.EventArgs e )
{
if ( myBind.Position == 0 )
MessageBox.Show ( "已经到首记录!" ) ;
else
myBind.Position -= 1 ;
}
//"首记录"按钮对应的事件
protected void GoFirst ( object sender , System.EventArgs e )
{
myBind.Position = 0 ;
}
}
2005-08-16 11:36
梦幻情缘
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:769
专家分:20
注 册:2005-4-4
收藏
得分:0 
这些内容网上到处都是。
2005-08-16 11:36
bagger
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:33
帖 子:891
专家分:0
注 册:2005-8-16
收藏
得分:0 
有没有SQL SERVER的啊???

【三元毕业设计论文】
三元论文真的只有三元钱
客服QQ:742670649
http://shop35094218./
2005-08-17 07:55
风霜
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:7
帖 子:242
专家分:0
注 册:2005-3-4
收藏
得分:0 
小韩!
你毁人呢!

昔日犹存, 昔日枉存. 故人尚在, 故人何在?
2005-08-17 18:00
梦幻情缘
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:769
专家分:20
注 册:2005-4-4
收藏
得分:0 
什么叫毁人啊?

-----------------------------------------------------------------------------------

昨夜西风凋碧树。独上高楼,望尽天涯路。
衣带渐宽终不悔,为伊消得人憔悴。
众里寻他千百度,蓦然回首,那人却在灯火阑珊处。
2005-08-17 18:43
唐伯猫
Rank: 8Rank: 8
等 级:贵宾
威 望:45
帖 子:5323
专家分:58
注 册:2005-8-9
收藏
得分:0 
以下是引用bagger在2005-8-17 7:55:43的发言: 有没有SQL SERVER的啊???
SQL SERVER也多的是啊!自己找了!

<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>
2005-08-18 11:53
快速回复:[求助]dataGrid
数据加载中...
 
   



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

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