zoukankan      html  css  js  c++  java
  • .Net连接数据库-曾,删,改,查(AOD.Net)

    连接数据库

            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行的变量.***

  • 相关阅读:
    Spring基础篇——AOP切面编程
    像我这样的人
    Java 内部类
    SQLite 带你入门
    Spring基础篇——通过Java注解和XML配置装配bean
    Spring基础篇——自动化装配bean
    VMware虚拟机+CentOS系统安装
    Spring基础篇——Spring容器和应用上下文理解
    Spring基础篇——DI/IOC和AOP原理初识
    Web 项目刚要打包,却找不到项目资源?
  • 原文地址:https://www.cnblogs.com/liujiangping/p/4618286.html
Copyright © 2011-2022 走看看