首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

奇怪,数据库操作,为啥有些代码没有运行

2013-02-25 
奇怪,数据库操作,为什么有些代码没有运行?初学者,代码难看,莫怪.我标记的代码运行不到,没有输出,好奇怪呀.

奇怪,数据库操作,为什么有些代码没有运行?
初学者,代码难看,莫怪.
我标记的代码运行不到,没有输出,好奇怪呀.有没有人帮解释一下.谢谢!!

    public class TestCode
    {
        private static List<string> ids = new List<string>();
        private static List<string> names = new List<string>();

        private static string myConnstr;
        private static OleDbConnection myconn; // 数据库连接
        private static string mysql; // 数据库操作语句
        private static OleDbCommand mycommand = new OleDbCommand();
        private static string dbpath; // 数据库名

        private static int InsertStartId;

        // 初始化数据库.
        private static void initdatabase()
        {
            dbpath = "oledb.mdb";
            myConnstr = 
                @"Provider=Microsoft.Jet.OleDB.4.0;Data Source=" + 
                dbpath;
        }

        // 查询数据库
        private static void findall()
        {
            ids.Clear();
            names.Clear();
            mysql = @"select id,user_name from users"; // 操作
            using (myconn)
            {
                myconn = new OleDbConnection(myConnstr);
                myconn.Open();
                mycommand.CommandText = mysql;
                mycommand.Connection = myconn;
                OleDbDataReader myreader = mycommand.ExecuteReader(); 
                int i = 0;
                while (myreader.Read())// 读取
                {
                    ids.Add(myreader["id"].ToString());
                    names.Add(myreader["user_name"].ToString());


                    i++;
                }

                /***********这里好像没有运行*********************/

                InsertStartId = i + 1;
                Console.WriteLine("Strat ID is: " + InsertStartId.ToString());

                /***********这里好像没有运行*********************/

                myconn.Close();
                myreader.Close();
            }
        }

        // 插入数据库数据.
        private static bool insert()
        {
            mysql = 
                @"insert into users Values(
                '" + InsertStartId.ToString() +  @"',
                'insert" + InsertStartId.ToString() + @"')";
            using (myconn)
            {
                myconn = new OleDbConnection(myConnstr);
                myconn.Open();
                mycommand.CommandText = mysql;
                mycommand.Connection = myconn;
                if(mycommand.ExecuteNonQuery() > 0)
                {
                    return true;
                }
                else
                    return false;
            }
        }

        public static void Main()
        {
            // ToDo: type todo code here;
            initdatabase();

            while(true)


            {
                findall();
                foreach (string _name in names)
                {
                    Console.WriteLine(_name);
                }
                Console.WriteLine("Press Any Key To Insert!");
                Console.ReadKey();

                if(insert())
                {
                    Console.WriteLine("Insert" + InsertStartId + "'th Data Success!!");
                    Console.WriteLine("Press Any Key To List Result!");
                    Console.ReadKey();
                }
            }
        }

断点调试下
[解决办法]
用數據庫操作軟件了吧?試下提交下事務試試
[解决办法]
没有被执行到,
打断点进去,看看
是不是被跳过了
[解决办法]
是不是没执行到就抛异常了 ,断点调试一下吧 

热点排行