当前位置导航:炫浪网>>网络学院>>编程开发>>Visual C#教程

C#中DataReader类的使用

        using System;
        using System.IO;
        using System.Data.OleDb;
        namespace ConsoleCommandDemo
        {
            /// <summary>
            /// Class1 的摘要说明。
            /// </summary>
            class CommandClass
            {
                /// <summary>
                /// 应用程序的主入口点。
                /// </summary>
                [STAThread]
                static void Main(string[] args)
                {
                    string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DataBase.mdb;";
                    // 定义连接
                    OleDbConnection MyCon = new OleDbConnection(strCon);
                    try
                    {
                        MyCon.Open();
                        string strSQL = @"INSERT INTO ADODemo (姓名, 年龄) VALUES ('赵六', 25)";
                        OleDbCommand MyCom = new OleDbCommand(strSQL, MyCon);
                        // 执行没有返回结果的指令
                        Console.WriteLine("执行的命令:{0}", MyCom.CommandText);
                        Console.WriteLine("执行结果影响了:{0}个记录", MyCom.ExecuteNonQuery());
                        // 删除一条记录
                        strSQL = @"delete from ADODemo where (姓名 = '赵六')";
                        MyCom.CommandText = strSQL;
                        Console.WriteLine("执行的命令:{0}", MyCom.CommandText);
                        Console.WriteLine("执行结果影响了:{0}个记录", MyCom.ExecuteNonQuery());
                        // 使用参数插入一条记录
                        MyCom.CommandText ="INSERT INTO ADODemo (姓名, 年龄) VALUES (?, ?)";
                        MyCom.Parameters.Add("@姓名", OleDbType.VarChar, 20);
                        MyCom.Parameters.Add("@年龄", OleDbType.TinyInt);
                        MyCom.Parameters["@姓名"].Value = "赵六";
                        MyCom.Parameters["@年龄"].Value = 23;
                        Console.WriteLine("执行的命令:{0}", MyCom.CommandText);
                        Console.WriteLine("执行结果影响了:{0}个记录", MyCom.ExecuteNonQuery());
                        // 删除一条记录
                        strSQL = @"delete from ADODemo where (姓名 = '赵六')";
                        MyCom.CommandText = strSQL;
                        Console.WriteLine("执行的命令:{0}", MyCom.CommandText);
                        Console.WriteLine("执行结果影响了:{0}个记录", MyCom.ExecuteNonQuery());
                        // 浏览数据
                        strSQL = @"select * from ADODemo";
                        MyCom.CommandText = strSQL;
                        OleDbDataReader MyDataReader = MyCom.ExecuteReader();
                        // 显示数据库中的数据
                        Console.WriteLine("显示数据库中的数据");
                        while(MyDataReader.Read())
                        {
                            Console.WriteLine("姓名:{0} 年龄:{1}", MyDataReader.GetValue(0), MyDataReader.GetValue(2));
                        }
                        MyDataReader.Close();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("{0}", ex.ToString());
                    }
                    finally
                    {
                        MyCon.Close();
                        MyCon.Close();
                    }
                    Console.ReadLine();
                }
            }
        }

相关内容
赞助商链接