C#连接数据库-曾,删,改,查

连接数据库

复制代码
        static void Main(string[] args)
        {
            //SqlConnection conn = new SqlConnection();//实例化
            //conn.ConnectionString = "server=.;database=mydb;uid=sa;pwd=123";//初始化-----可以用下面一条语句执行
            SqlConnection conn = new SqlConnection("server=.;database=mydb;uid=sa;pwd=123");
            Console.WriteLine(conn.State);
            conn.Open();//打开数据库连接
            Console.WriteLine(conn.State);
            conn.Close();//关闭数据库连接
            Console.WriteLine(conn.State);
           
        }
复制代码

连接数据库并插入数据

复制代码
        static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection("server=.;database=mydb;uid=sa;pwd=123");
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;//初始化,通过conn数据对象连接数据库
            cmd.CommandText = "insert into info values('p005','学生','0','n001','1990-02-1')";//""内为SQL语句
            cmd.ExecuteNonQuery();
            conn.Close();
            Console.WriteLine("OK");
        }
复制代码

连接数据库并修改数据

复制代码
        public static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection("server=.;database=mydb;uid=sa;pwd=123");
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "update info set name='学员' where code='p005'";
            cmd.ExecuteNonQuery();
            conn.Close();
        }
复制代码

连接数据库并删除数据

复制代码
        public static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection("server=.;database=mydb;uid=sa;pwd=123");                               
            conn.Open();            
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "delete from info where code='p005'";
            cmd.ExecuteNonQuery();
            conn.Close();           
        }
复制代码

 

 连接数据库查询数据

复制代码
        static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection("server=.;database=mydb;uid=sa;pwd=123");            
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from info";
            SqlDataReader r = cmd.ExecuteReader();//查询语句用ExecuteReader返回SqlDataReader类
            while (r.Read())
            {
                
                Console.WriteLine(r[0].ToString()+r[1].ToString()+r[2].ToString());
            }
            conn.Close();
        }
复制代码

 

 

 案例:用户登录实现

复制代码
        public static void Main(string[] arges)
        {
            Console.Write("用户名:");
            string uid = Console.ReadLine();
            Console.Write("密码:");
            string pwd = Console.ReadLine();
            SqlConnection conn = new SqlConnection("server=.;database=mydb;uid=sa;pwd=123");            
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from login where username='"+uid+"' and password='"+pwd+"'";
            SqlDataReader r = cmd.ExecuteReader();
            if (r.HasRows == false)
            {
                Console.WriteLine("嗯哼,不对");
            }
            else
            {
                Console.WriteLine("嗯哼,正确");
            }
            conn.Close();
        }
复制代码

*** .HasRows()与.read()区别:HasRows只关系读数据库中的数据是否可行,而不执行读的操作.Read是执行读的操作看是否执行成功.二者都返回一个bool行的变量.***

posted @ 2015-08-29 08:35  怎能不累  阅读(189)  评论(0编辑  收藏  举报