1//             C#数据库操作之ACCESS基本操作
  2
  3//今天所做的只是一些ACCESS的简单基本操作.其实复杂的操作也只是由这些简单操作所组成的.
  4//包括使用DataReader检索数据\增加\修改\删除
  5//我这里只是随便写写,如何把它们组成一个OOP的形式,自己去找找思路吧...
  6//其实想要真正的了解OOP是需要经验+时间+努力==OOP
  7//                  以上我只是随便乱说的.哈哈~~
  8
  9using System;
 10using System.Data;
 11using System.Data.OleDb;
 12
 13namespace Access基本操作
 14{
 15    class Program
 16    {
 17        static void Main(string[] args)
 18        {
 19            string Afile = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\C#2005\数据库操作\Access基本操作\Data\User.mdb";
 20            OleDbConnection AconnStr = new OleDbConnection(Afile);
 21            OleDbCommand Acmd = new OleDbCommand("Select * From [UserTable] order by ID",AconnStr);
 22            OleDbDataReader odr = null;
 23            AconnStr.Open();
 24            try
 25            {
 26                odr = Acmd.ExecuteReader();
 27
 28            }

 29            catch(Exception ex)
 30            {
 31                if(ex != null) Console.WriteLine("执行出错");
 32            }

 33            Console.WriteLine("开始读取数据,请等待");
 34            System.Threading.Thread.Sleep(1000);
 35
 36            if(odr != null)
 37            {
 38                string TotalInfo = "";
 39                TotalInfo += "ID号\t用户名\n";
 40                while (odr.Read())
 41                {
 42                    TotalInfo += odr["ID"].ToString() + "\t";
 43                    TotalInfo += odr["UserName"].ToString()+"\n";
 44                }

 45                odr.Close();
 46                Console.WriteLine(TotalInfo);
 47            }

 48
 49            Console.Write("是否添加一条新记录(y|n):");
 50            string strInput = null;
 51            strInput =Console.ReadLine().ToLower().ToString();
 52            if (strInput == "y")
 53            {
 54                Console.WriteLine("正在添加一条记录,请等待");
 55                System.Threading.Thread.Sleep(100);
 56
 57                // 设置数据库操作命令(Insert)
 58                string userName = null;
 59                Console.Write("请输入一个用户名:");
 60                userName = Console.ReadLine();
 61                if (userName != string.Empty)
 62                {
 63                    Acmd.CommandText = "Insert into UserTable (UserName) Values('" + userName + "')";
 64                    // 执行命令
 65                    int ExcNum = Acmd.ExecuteNonQuery();
 66                    if (ExcNum == 1)
 67                    {
 68                        Console.WriteLine("执行成功");
 69                    }

 70                    else
 71                    {
 72                        Console.WriteLine("执行失败");
 73                    }

 74                }

 75                else
 76                {
 77                    Console.WriteLine("对不起,你输入了一个空的用户名,所以系统自动放弃添加该记录");
 78                }

 79            }

 80            else
 81            {
 82                Console.WriteLine("你放弃了一次添加记录的机会......");
 83            }

 84
 85            Console.Write("是否修改一条新记录(y|n):");
 86            strInput = Console.ReadLine().ToLower().ToString();
 87            if (strInput == "y")
 88            {
 89                Console.Write("请输入你要修改记录的ID号:");
 90                string strID = null;
 91                strID = Console.ReadLine().Trim().ToLower().ToString();
 92                if (strID != string.Empty)
 93                {
 94                    Console.Write("请输入新用户名:");
 95                    string newUserName = null;
 96                    newUserName = Console.ReadLine().Trim().ToString();
 97                    if (newUserName != string.Empty)
 98                    {
 99                        Acmd.CommandText = "Update UserTable Set UserName='" + newUserName + "' Where ID=" + Convert.ToInt32(strID);
100                        int ExcNum = Acmd.ExecuteNonQuery();
101                        if (ExcNum == 1)
102                        {
103                            Console.WriteLine("执行修改命令成功");
104                        }

105                        else
106                        {
107                            Console.WriteLine("执行修改命令失败");
108                        }

109                    }

110                }

111                else
112                {
113                    Console.WriteLine("你输入的ID为空所以系统自动放弃修改记录的机会......");
114                }

115            }

116            else
117            {
118                Console.WriteLine("你放弃了一次修改记录的机会......");
119            }

120
121
122
123            Console.Write("是否删除一条新记录(y|n):");
124            strInput = Console.ReadLine().ToLower().ToString();
125            if (strInput == "y")
126            {
127                string strDelID = null;
128                Console.Write("请输入你要删除的ID号:");
129                strDelID = Console.ReadLine().Trim().ToLower();
130                if (strDelID.Length != 0)
131                {
132                    int ExcNum = 0;
133                    Acmd.CommandText = "Delete From UserTable Where ID=" + Convert.ToInt32(strDelID);
134                    try
135                    {
136                        ExcNum = Acmd.ExecuteNonQuery();
137                    }

138                    catch (Exception e)
139                    {
140                        Console.WriteLine(e.Message);
141                    }

142                    if (ExcNum == 1)
143                    {
144                        Console.WriteLine("执行删除命令成功");
145                    }

146                    else
147                    {
148                        Console.WriteLine("执行删除命令失败");
149                    }

150                }

151                else
152                {
153                    Console.WriteLine("你输入的ID号不在范围内");
154                }

155            }

156            else
157            {
158                Console.WriteLine("你放弃了一次删除记录的机会......");
159            }

160
161            Console.Read();
162            // 关闭连接
163            AconnStr.Close();
164        }

165    }

166}
posted on 2006-03-16 22:58  CIPCHK  阅读(1119)  评论(0编辑  收藏  举报