您现在的位置: 中国IT实验室 >> 软件水平考试 >> 特别推荐 >> 文章正文
用Visual C#往数据库中添加记录(下)

ChinaITLab 收集整理  2004-8-9  保存本文  推荐给好友  QQ上看本站  收藏本站



  二.用Visual C#往SQL SERVER数据库中插入记录:
  (1)用Visual C#往Access 2000和SQL SERVER添加记录的主要区别在于使用了不同的数据库引擎。在编写程序之前,首先假设数据库服务器名称为:server1,要访问的数据库名称为:data1,数据表名称为:books。用户名为:sa。其中数据表的数据结构和Access 2000的表的结构相同。下面是程序中打开SQL SERVER的数据引擎程序代码:
  // 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1
  string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ;
  OleDbConnection myConn = new OleDbConnection ( strCon ) ;
  myConn.Open ( ) ;
  
  
  (2).用Visual C#往SQL SERVER 数据库中插入记录的源程序代码( add02.cs ):
  using System ;
  using System.Drawing ;
  using System.ComponentModel ;
  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{
  // 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1,用户名为sa。
  string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ;
  OleDbConnection myConn = new OleDbConnection ( strCon ) ;
  myConn.Open ( ) ;
  string strCom = " SELECT * FROM books " ;
  //创建一个 DataSet
  myDataSet = new DataSet ( ) ;
  //用 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 System.ComponentModel.Container ( ) ;
  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 ) ; <




 相关文章  热门文章
C#中DataGrid的数据新增编辑删除操作代码
如何使用 ADO.NET 轻松操纵数据库
C#.Net 网络程序开发基础之Socket篇
C#.Net 网络程序开发基础之TCP篇
运用 .net 中的 socket 实现文件传输
如何判断程序处于运行环境还是调试环境
从一个程序出发详细研究 DataReader
.NET 应用程序如何披上XP的外衣?
使用 C# 在应用程序之间发送消息
设计模式之工厂方法 FACTORY METHOD(三)

 文章评论


认证培训
热门专题       more
相关下载
论坛新帖
博 客