zoukankan      html  css  js  c++  java
  • 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行的变量.***

  • 相关阅读:
    「考试」省选27
    「考试」省选26
    「考试」省选25
    $dy$讲课总结
    「笔记」$Min\_25$筛
    「考试」省选24
    「总结」多项式生成函数例题(4)
    「总结」多项式生成函数相关(4)
    「考试」省选23
    「总结」后缀3
  • 原文地址:https://www.cnblogs.com/IT1517/p/4768325.html
Copyright © 2011-2022 走看看