在前面的一篇文章《Visual C#中轻松浏览数据库记录》中
我们介绍了用Visual C#如何把数据表中的字段值绑定到文本框的属性上和如何操作数据记录指针
随意浏览数据表中的记录
本文就接着上一篇的内容
来介绍用Visual C#如何来修改和删除数据记录
一程序设计和运行的环境设置
()视窗服务器版
()Microsoft Access Data Component 以上版本 ( MADC )
()本文程序使用的数据库的介绍
为了方便起见在选用数据库方面选用了本地数据库Access 当然你也可以选用其他类型的数据库只需要更改文章后面的程序源代码中数据库的引擎并更改对应的代码就可以了
本程序中使用的数据库名称为samplemdb在此数据库中有一张数据表books此数据表的结构如下
字段名称 字段类型 代表意思
Bookid 数字 序号
booktitle 文本 书籍名称
bookauthor 文本 书籍作者
bookprice 数字 价格
bookstock 数字 书架号
二程序设计难点和应该注意的问题
在程序设计中的重点和难点就是如何用Visual C#删除记录和如何修改记录下面就这二个问题进行必要的论述
()如何用Visual C#正确删除数据表中的记录
在用Visual C#删除记录的时候要注意的是必须从二个方面彻底删除记录即从数据库和用Visual C#编程时产生的一个DataSet对象中彻底删除在程序设计的时候如果只是删除了DataSet对象中的记录信息这种删除是一种伪删除这是因为当他退出程序又重新运行程序会发现那个要删除的记录依然还存在这是因为DataSet对象只是对数据表的一个镜像并不是真正的记录本身但如果只是从数据库中删除记录因为我们此时程序用到的数据集合是从DataSet对象中读取的子DataSet对象中依然保存此条记录的镜像所以就会发现我们根本没有删除掉记录但实际上已经删除了此时只有退出程序重新运行才会发现记录已经删除了本文使用的方法是删除以上二个方面的记录或记录镜像信息当然你也可以使用其他的方法譬如首先从数据库中删除记录然后重新建立数据连接重新创建一个新的DataSet对象这种方法虽然也可以达到相同目的但显然相对繁杂些所以本文采用的是第一种方法直接删除在程序中具体的实现语句如下
//连接到一个数据库
string strCon = Provider = MicrosoftJetOLEDB ; Data Source = samplemdb ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConnOpen ( ) ;
string strDele = DELETE FROM books WHERE bookid= + t_bookidText ;
OleDbCommand myCommand = new OleDbCommand ( strDele myConn ) ;
//从数据库中删除指定记录
myCommandExecuteNonQuery ( ) ;
//从DataSet中删除指定记录信息
myDataSetTables [ books ] Rows [ myBindPosition ] Delete ( ) ;
myDataSetTables [ books ] AcceptChanges ( ) ;
myConnClose ( ) ;
()用Visual C#来修改数据表中的记录
在用Visual C#修改记录和删除记录在程序设计中大致差不多具体的实现方式也是通过SQL语句调用来实现的下面就是在程序中修改记录的具体语句
//连接到一个数据库
string strCon = Provider = MicrosoftJetOLEDB ; Data Source = samplemdb ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConnOpen ( ) ;
//从数据库中修改指定记录
string strUpdt = UPDATE books SET booktitle =
+ t_booktitleText + bookauthor =
+ t_bookauthorText + bookprice =
+ t_bookpriceText + bookstock =
+ t_bookstockText + WHERE bookid = + t_bookidText ;
OleDbCommand myCommand = new OleDbCommand ( strUpdt myConn ) ;
myCommandExecuteNonQuery ( ) ;
myConnClose ( ) ;
()在了解了如何用Visual C#删除和修改记录以后结合《Visual C#中轻松浏览数据库记录》一文的内容就可以得到用Visual C#完成删除和修改数据记录的比较完整程序代码以下是本文中介绍程序的运行后的程序界面
点击小图放大本文中程序运行后的界面
三用Visual C#实现删除和修改数据库记录的完整源程序代码
using System ;
using SystemDrawing ;
using SystemComponentModel ;
using SystemWindowsForms ;
using SystemDataOleDb ;
using SystemData ;
public class DataEdit : Form { private SystemComponentModelContainer 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 label ;
private SystemDataDataSet myDataSet ;
private BindingManagerBase myBind ;
private bool isBound = false ;
//定义此变量是判断组件是否已经绑定数据表中的字段
public DataEdit ( ) {
// 对窗体中所需要的内容进行初始化
InitializeComponent ( ) ;
//连接到一个数据库
GetConnected ( ) ;
}
//清除程序中用到的所有资源
public override void Dispose ( ) {
baseDispose ( ) ;
componentsDispose ( ) ;
}
public void GetConnected ( )
{
try{
//创建一个 OleDbConnection对象
string strCon = Provider = MicrosoftJetOLEDB ; Data Source = samplemdb ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = SELECT * FROM books ;
//创建一个 DataSet对象
myDataSet = new DataSet ( ) ;
myConnOpen ( ) ;
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom myConn ) ;
myCommandFill ( myDataSet books ) ;
myConnClose ( ) ;
//判断数据字段是否绑定到 TextBoxes
if ( !isBound )
{
//以下是为显示数据记录而把数据表的某个字段绑定在不同的绑定到文本框Text属性上
t_bookidDataBindingsAdd ( Text myDataSet booksbookid ) ;
t_booktitleDataBindingsAdd ( Text myDataSet booksbooktitle ) ;
t_bookauthorDataBindingsAdd ( Text myDataSet booksbookauthor ) ;
t_bookpriceDataBindingsAdd ( Text myDataSet booksbookprice ) ;
t_bookstockDataBindingsAdd ( Text myDataSet booksbookstock ) ;
//设定 BindingManagerBase
//把对象DataSet和books数据表绑定到此myBind对象
myBind = thisBindingContext [ myDataSet books ] ;
isBound = true ;
}
}
catch ( Exception e )
{
MessageBoxShow ( 连接数据库发生错误为 + eToString ( ) 错误! ) ;
}
}
public static void Main ( ) {
ApplicationRun ( new DataEdit ( ) ) ;
}
private void InitializeComponent ( )
{
thiscomponents = new SystemComponentModelContainer ( ) ;
thist_bookid = new TextBox ( ) ;
thispreviousrec = new Button ( ) ;
thisl_bookauthor = new Label ( ) ;
thisdelete = new Button ( ) ;
thist_booktitle = new TextBox ( ) ;
thist_bookauthor = new TextBox ( ) ;
thist_bookprice = new TextBox ( ) ;
thisl_bookprice = new Label ( ) ;
thist_bookstock = new TextBox ( ) ;
thisl_bookstock = new Label ( ) ;
thisl_booktitle = new Label ( ) ;
thisupdate = new Button ( ) ;
thisnextrec = new Button ( ) ;
thislastrec = new Button ( ) ;
thisfirstrec = new Button ( ) ;
thislabel = new Label ( ) ;
thisl_bookid = new Label ( ) ;
t_bookidLocation = new SystemDrawingPoint ( ) ;
t_bookidSize = new SystemDrawingSize ( ) ;
t_booktitleLocation = new SystemDrawingPoint ( ) ;
t_booktitleSize = new SystemDrawingSize ( ) ;
t_bookauthorLocation = new SystemDrawingPoint ( ) ;
t_bookauthorSize = new SystemDrawingSize ( ) ;
t_bookpriceLocation = new SystemDrawingPoint ( ) ;
t_bookpriceSize = new SystemDrawingSize ( ) ;
t_bookstockLocation = new SystemDrawingPoint ( ) ;
t_bookstockSize = new SystemDrawingSize ( ) ;
//以下是设定在程序中使用到的Label属性
l_bookidLocation = new SystemDrawingPoint ( ) ;
l_bookidText = 序 号 ;
l_bookidSize = new SystemDrawingSize ( ) ;
l_bookidFont = new SystemDrawingFont ( 宋体 f ) ;
l_bookidTextAlign = SystemDrawingContentAlignmentMiddleCenter ;
l_booktitleLocation = new SystemDrawingPoint ( ) ;
l_booktitleText = 书 名 ;
l_booktitleSize = new SystemDrawingSize ( ) ;
l_booktitleFont = new SystemDrawingFont ( 宋体 f ) ;
l_booktitleTextAlign = SystemDrawingContentAlignmentMiddleCenter ;
l_bookauthorLocation = new SystemDrawingPoint ( ) ;
l_bookauthorText = 作 者;
l_bookauthorSize = new SystemDrawingSize ( ) ;
l_bookauthorFont = new SystemDrawingFont ( 宋体 f ) ;
l_bookauthorTextAlign = SystemDrawingContentAlignmentMiddleCenter ;
l_bookpriceLocation = new SystemDrawingPoint ( ) ;
l_bookpriceText = 价 格 ;
l_bookpriceSize = new SystemDrawingSize ( ) ;
l_bookpriceFont = new SystemDrawingFont ( 宋体 f ) ;
l_bookpriceTextAlign = SystemDrawingContentAlignmentMiddleCenter ;
l_bookstockLocation = new SystemDrawingPoint ( ) ;
l_bookstockText = 书 架 号 ;
l_bookstockSize = new SystemDrawingSize ( ) ;
l_bookstockFont = new SystemDrawingFont ( 宋体 f ) ;
l_bookstockTextAlign = SystemDrawingContentAlignmentMiddleCenter ;
//以下设定程序中用到的功能按钮的属性及对应的事件
deleteLocation = new SystemDrawingPoint ( ) ;
deleteForeColor = SystemDrawingColorBlack ;
deleteSize = new SystemDrawingSize ( ) ;
deleteFont = new SystemDrawingFont ( 宋体 f ) ;
deleteText = 删除记录 ;
deleteClick += new SystemEventHandler ( GoDelete ) ;
updateLocation = new SystemDrawingPoint ( ) ;
updateForeColor = SystemDrawingColorBlack ;
updateSize = new SystemDrawingSize ( ) ;
updateFont = new SystemDrawingFont ( 宋体 f ) ;
updateText = 修改记录 ;
updateClick += new SystemEventHandler ( GoUpdate ) ;
firstrecLocation = new SystemDrawingPoint ( ) ;
firstrecForeColor = SystemDrawingColorBlack ;
firstrecSize = new SystemDrawingSize ( ) ;
firstrecFont = new SystemDrawingFont ( 宋体 f ) ;
firstrecText = 首记录 ;
firstrecClick += new SystemEventHandler ( GoFirst ) ;
previousrecLocation = new SystemDrawingPoint ( ) ;
previousrecForeColor = SystemDrawingColorBlack ;
previousrecSize = new SystemDrawingSize ( ) ;
previousrecFont = new SystemDrawingFont ( 宋体 f ) ;
previousrecText = 上一条 ;
previousrecClick += new SystemEventHandler ( GoPrevious ) ;
nextrecLocation = new SystemDrawingPoint ( ) ;
nextrecForeColor = SystemDrawingColorBlack ;
nextrecSize = new SystemDrawingSize ( ) ;
nextrecFont = new SystemDrawingFont ( 宋体 f ) ;
nextrecText = 下一条 ;
nextrecClick += new SystemEventHandler ( GoNext ) ;
lastrecLocation = new SystemDrawingPoint ( ) ;
lastrecForeColor = SystemDrawingColorBlack ;
lastrecSize = new SystemDrawingSize ( ) ;
lastrecFont = new SystemDrawingFont ( 宋体 f ) ;
lastrecText = 尾记录 ;
lastrecClick += new SystemEventHandler ( GoLast ) ;
labelLocation = new SystemDrawingPoint ( ) ;
labelText = 用Visual C#来修改和删除数据库中的记录 ;
labelSize = new SystemDrawingSize ( ) ;
labelForeColor = SystemDrawingSystemColorsDesktop ;
labelFont = new SystemDrawingFont ( 宋体 f ) ;
//设定程序的主窗体的属性
thisText = 用Visual C#来修改和删除数据库中的记录! ;
thisAutoScaleBaseSize = new SystemDrawingSize ( ) ;
thisFormBorderStyle = FormBorderStyleFixedSingle ;
thisClientSize = new SystemDrawingSize ( ) ;
//在主窗体中加入组件
thisControlsAdd ( delete ) ;
thisControlsAdd ( update ) ;
thisControlsAdd ( lastrec ) ;
thisControlsAdd ( nextrec ) ;
thisControlsAdd ( previousrec ) ;
thisControlsAdd ( firstrec ) ;
thisControlsAdd ( t_bookstock ) ;
thisControlsAdd ( t_bookprice ) ;
thisControlsAdd ( t_bookauthor ) ;
thisControlsAdd ( t_booktitle ) ;
thisControlsAdd ( t_bookid ) ;
thisControlsAdd ( l_bookstock ) ;
thisControlsAdd ( l_bookprice ) ;
thisControlsAdd ( l_bookauthor ) ;
thisControlsAdd ( l_booktitle ) ;
thisControlsAdd ( l_bookid ) ;
thisControlsAdd ( label ) ;
}
//删除记录对应的事件
protected void GoDelete ( object sender SystemEventArgs e )
{
try{
//连接到一个数据库
string strCon = Provider = MicrosoftJetOLEDB ; Data Source = samplemdb ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConnOpen ( ) ;
string strDele = DELETE FROM books WHERE bookid= + t_bookidText ;
OleDbCommand myCommand = new OleDbCommand ( strDele myConn ) ;
//从数据库中删除指定记录
myCommandExecuteNonQuery ( ) ;
//从DataSet中删除指定记录
myDataSetTables [ books ] Rows [ myBindPosition ] Delete ( ) ;
myDataSetTables [ books ] AcceptChanges ( ) ;
myConnClose ( ) ;
}
catch ( Exception ed )
{
MessageBoxShow ( 删除记录错误信息 + edToString ( ) 错误! ) ;
}
}
//修改记录按钮对应的事件
protected void GoUpdate ( object sender SystemEventArgs e )
{
int i = myBindPosition ;
try{
//连接到一个数据库
string strCon = Provider = MicrosoftJetOLEDB ; Data Source = samplemdb ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConnOpen ( ) ;
//从数据库中修改指定记录
string strUpdt = UPDATE books SET booktitle =
+ t_booktitleText + bookauthor =
+ t_bookauthorText + bookprice =
+ t_bookpriceText + bookstock =
+ t_bookstockText + WHERE bookid = + t_bookidText ;
OleDbCommand myCommand = new OleDbCommand ( strUpdt myConn ) ;
myCommandExecuteNonQuery ( ) ;
myConnClose ( ) ;
}
catch ( Exception ed )
{
MessageBoxShow ( 修改指定记录错误 + edToString ( ) 错误! ) ;
}
myBindPosition = i ;
}
//尾记录按钮对应的事件
protected void GoLast ( object sender SystemEventArgs e )
{
myBindPosition = myBindCount ;
}
//下一条按钮对应的事件
protected void GoNext ( object sender SystemEventArgs e )
{
if ( myBindPosition == myBindCount )
MessageBoxShow ( 已经到尾记录! ) ;
else
myBindPosition += ;
}
//上一条按钮对应的事件
protected void GoPrevious ( object sender SystemEventArgs e )
{
if ( myBindPosition == )
MessageBoxShow ( 已经到首记录! ) ;
else
myBindPosition = ;
}
//首记录按钮对应的事件
protected void GoFirst ( object sender SystemEventArgs e )
{
myBindPosition = ;
}
}
四编译源程序代码生成执行文件
得到了Datacs源程序代码以后经过下面编译命令编译成功后即可得到执行文件dataexe
csc /t:winexe /r:systemwindowsformsdll /r:systemdatadll datacs
五总结
本文主要介绍了如何用Visual C#来删除和修改记录以及在处理这些操作的时候应该注意的一些重要问题及处理的方法如果你的机器已经满足本文要求的运行环境那么在生成的执行文件目录中建立一个samplemdb数据库在数据库中创建一个books数据表数据表的结构可按照本文上面提供的结构来建立在这一切都完毕后就可以运行程序享受Visual C#给我们带来的数据操作的快感了