现在的位置: 首页 > 综合 > 正文

连接 Access 2007

2011年12月26日 ⁄ 综合 ⁄ 共 1355字 ⁄ 字号 评论关闭

一直都是使用的 Sql Server 2005 或者是 Sql Server 2000,

这个在代码的部署上总是很麻烦的,昨天老师在搞了个 Access , 

感觉部署起来十分方便,而且对于小型的数据,储存起来也很方便,

所以今天专门来研究了一下 Access ,

 本机电脑上装的是 Access 2007,本来以为可以直接使用连接 Access 2003 的连接方式进行连接,

结果会报错误,在网上搜了一下,找到了关于连接 Access 2007 的方法,

大致豆豆如下

 

            string conStr = "Provider=Microsoft.ACE.OleDb.12.0;";

            conStr += @"Data Source=E:\数据库\XiaoZhen.accdb;";

            conStr += "Persist Security Info=False;";

            try

            {

                using (OleDbConnection OleDbCon = new OleDbConnection(conStr))

                {

                    OleDbCon.Open();

                    using (OleDbCommand OleDbCom = OleDbCon.CreateCommand())

                    {

                        OleDbCom.CommandType = CommandType.Text;

                        string accessStr = "Select 账号 From 用户表 Where 账号='" + 

                                            TextBox2.Text.Trim() + "' And 密码='" + 

                                            TextBox3.Text.Trim() + "'";

                        OleDbCom.CommandText = accessStr;

                        using (OleDbDataReader OleDbDD = OleDbCom.ExecuteReader())

                        {

                            int count = 0;

                            while (OleDbDD.Read())

                            {

                                count++;

                            }

                            if (count > 0)

                            {

                                Label1.Text = "恭喜你,登陆成功";

                            }

                            else

                            {

                                Label1.Text = "抱歉,账号或者密码错误";

                            }

                        }

                    }

                }

            }

            catch (Exception E)

            {

                Label1.Text = E.Message.ToString();

                return;

            }

 

 

 

抱歉!评论已关闭.