Home  >  Article  >  Database  >  Access数据库创建、使用

Access数据库创建、使用

WBOY
WBOYOriginal
2016-06-07 15:44:252098browse

1、创建Access数据库表 1)在office中打开Microsoft Access2010,选择空数据库创建数据库StudentInfo。 2)创建新表,在表格第一列选择数据类型,并输入列名。 3)点击保存,输入数据库表名Student,完成创建。 4)将创建的数据库拷贝到项目的Debug文件中即

1、创建Access数据库表

1)在office中打开Microsoft Access2010,选择空数据库创建数据库StudentInfo。

2)创建新表,在表格第一列选择数据类型,并输入列名。

3)点击保存,输入数据库表名Student,完成创建。

4)将创建的数据库拷贝到项目的Debug文件中即可使用。

 

2、连接、打开Access数据库

1)数据库连接语句:public string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= StudentInfo.accdb"; 

2)常用的查询、执行sql语句:

Access数据库创建、使用Access数据库创建、使用

<span>public</span> DataSet Query(<span>string</span><span> strSql)
        {
            </span><span>using</span> (OleDbConnection conn = <span>new</span><span> OleDbConnection(ConnectionString))
            {
                DataSet ds </span>= <span>new</span><span> DataSet();
                </span><span>try</span><span>
                {
                    conn.Open();
                    OleDbDataAdapter command </span>= <span>new</span><span> OleDbDataAdapter(strSql, conn);
                    command.Fill(ds, </span><span>"</span><span>ds</span><span>"</span><span>);
                }
                </span><span>catch</span><span> (System.Data.SqlClient.SqlException ex)
                {
                    </span><span>throw</span><span>;
                }

                </span><span>return</span><span> ds;
            }
        }

        </span><span>public</span>  <span>int</span> ExecuteSql(<span>string</span><span> strSql)
        {
            </span><span>using</span> (OleDbConnection conn = <span>new</span><span> OleDbConnection(ConnectionString))
            {
                </span><span>using</span> (OleDbCommand cmd = <span>new</span><span> OleDbCommand(strSql, conn))
                {
                    </span><span>try</span><span>
                    {
                        conn.Open();
                        </span><span>int</span> rows =<span> cmd.ExecuteNonQuery();
                        </span><span>return</span><span> rows;
                    }
                    </span><span>catch</span><span> (System.Data.SqlClient.SqlException e)
                    {
                        conn.Close();
                        </span><span>throw</span><span> e;
                    }
                }
            }
        }

        </span><span>public</span> <span>object</span> GetSingle(<span>string</span><span> strSql)
        {
            </span><span>using</span> (OleDbConnection conn = <span>new</span><span> OleDbConnection(ConnectionString))
            {
                </span><span>using</span> (OleDbCommand cmd = <span>new</span><span> OleDbCommand(strSql, conn))
                {
                    </span><span>try</span><span>
                    {
                        conn.Open();
                        </span><span>object</span> obj =<span> cmd.ExecuteScalar();
                        </span><span>if</span> ((Object.Equals(obj, <span>null</span>)) ||<span> (Object.Equals(obj, System.DBNull.Value)))
                        {
                            </span><span>return</span> <span>null</span><span>;
                        }
                        </span><span>else</span><span>
                        {
                            </span><span>return</span><span> obj;
                        }
                    }
                    </span><span>catch</span><span> (System.Data.SqlClient.SqlException e)
                    {
                        conn.Close();
                        </span><span>throw</span><span> e;
                    }
                }
            }
        }</span>
View Code

3)输入常用的数据库sql语句即可完成调用。

 

常见问题:

1)添加AccessDatabaseEngine数据接口驱动。

2)如何不能识别.accdb格式数据库,则查看:

http://wenku.baidu.com/link?url=qQ3f2LLz8gL9ZKEjHKae4l1BBeaVCiGo2WXEr0mpVqXbPcQys5VGiCZme2r1lPCSmd3beyGNfi1lwrv4MLIxHLANIwnZOGSTDKGTEBNlUum

 

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn