zoukankan      html  css  js  c++  java
  • 用Access作为后台数据库支撑,书写一个C#写入记录的案例

    要想操作一个数据库,不论是那种操作,首先要做的肯定是打开数据库。

     下面我们以ACCESS数据库来做例子说明如何打开一个数据库连接!

        在这里我们需要用到的是:

        System.Data.OleDb.OleDbConnection类!(如果操作SQL数据库,我们最好使用System.Data.SqlClient.SqlConnection类)

    使用程序:

    我们先定义一个String类型的变量,其中存放了我们连接数据库的连接字符串,然后在定义一个System.Data.OleDb.OleDbConnection类型的对象并实例化,最后返回这个对象!

    SQL语句

    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 + ")" ;

    定义command对象,并执行相应的SQL语句

    OleDbCommand inst = new OleDbCommand ( strInsert , myConn ) ;

    inst.ExecuteNonQuery ( ) 

    myConn.Close ( ) ;

  • 相关阅读:
    git命令使用方法
    git与svn对比
    浏览器缓存原理
    网络性能优化常用方法
    sass/scss 和 less的区别
    AngularJS和ReactJS对比
    让IE6,7,8支持HTML5新标签的方法
    Retina 屏移动设备 1px解决方案
    HttpClient学习
    国家二字码对照表
  • 原文地址:https://www.cnblogs.com/Zhangmin123/p/5257141.html
Copyright © 2011-2022 走看看