Access database creation, use

Source: Internet
Author: User

1. Create an Access database table

1) Open Microsoft Access2010 in office, select the empty database to create the database Studentinfo.

2) Create a new table, select the data type in the first column of the table, and enter the column name.

3) Click Save, enter the database table name student, and complete the creation.

4) Copy the database you created to the debug file of your project to use.

2. Connect, open an Access database

1) Database Connection statement: public string ConnectionString = "Provider=microsoft.ace.oledb.12.0;data source= studentinfo.accdb";

2) Common query, execute SQL statement:

 PublicDataSet Query (stringstrSQL) {            using(OleDbConnection conn =NewOleDbConnection (ConnectionString)) {DataSet DS=NewDataSet (); Try{Conn.                    Open (); OleDbDataAdapter command=NewOleDbDataAdapter (strSQL, conn); Command. Fill (DS,"DS"); }                Catch(System.Data.SqlClient.SqlException ex) {Throw; }                returnds; }        }         Public  intExecuteSQL (stringstrSQL) {            using(OleDbConnection conn =NewOleDbConnection (ConnectionString)) {                using(OleDbCommand cmd =NewOleDbCommand (strSQL, conn)) {                    Try{Conn.                        Open (); introws =cmd.                        ExecuteNonQuery (); returnrows; }                    Catch(System.Data.SqlClient.SqlException e) {conn.                        Close (); Throwe; }                }            }        }         Public ObjectGetsingle (stringstrSQL) {            using(OleDbConnection conn =NewOleDbConnection (ConnectionString)) {                using(OleDbCommand cmd =NewOleDbCommand (strSQL, conn)) {                    Try{Conn.                        Open (); Objectobj =cmd.                        ExecuteScalar (); if(Object.Equals (obj,NULL)) ||(Object.Equals (obj, System.DBNull.Value))) {                            return NULL; }                        Else                        {                            returnobj; }                    }                    Catch(System.Data.SqlClient.SqlException e) {conn.                        Close (); Throwe; }                }            }        }
View Code

3) Enter a common database SQL statement to complete the call.

Problems:

1) Add Accessdatabaseengine Data interface driver.

2) How to not recognize the. accdb format database, see:

Http://wenku.baidu.com/link?url= Qq3f2llz8gl9zkejhkae4l1bbeavcigo2wxer0mpvqxbpcqys5vgiczme2r1lpcsmd3beygnfi1lwrv4mlixhlaniwnzogstdkgtebnluum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.