执行SQL查询脚本

时间:2014-05-21 03:27:58   收藏:0   阅读:398
bubuko.com,布布扣
static void Main(string[] args)
        {
            Console.WriteLine("输入用户编号:");
            string cusernum = Console.ReadLine();

            Console.WriteLine("输入用户名:");
            string cusername = Console.ReadLine();

            string connString = @"Data Source=.; Initial Catalog=lh0216; User ID=sa;pwd=Founder123";
            //出了using括号objConnection类会被自动释放
            using (SqlConnection objConnection = new SqlConnection(connString))
            {
                objConnection.Open();
               
                using (SqlCommand objCmd = objConnection.CreateCommand())
                {
                    objCmd.CommandText = "select * from TBZG where czgbh=‘" + cusernum + "" ;
                    using (SqlDataReader objReader = objCmd.ExecuteReader())
                    {
                        //结果集不为空
                        if (objReader.Read())
                        {
                            string cValue = objReader.GetString(objReader.GetOrdinal("czgxm"));
                            //检查手工输入的姓名与数据库中查询的名称是否相同
                            if (cValue == cusername)
                            {
                                Console.WriteLine("你好{0}",cusername);
                            }
                        }
                        else
                        {
                            Console.WriteLine("用户名不存在。");
                        }
                    }
                }
            }            
            Console.ReadKey();
        }
bubuko.com,布布扣

 

执行SQL查询脚本,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!