我是一名学生自学.net,讲些一系列的博文来记录本阶段学习的内容,由于期末考试临近有可能在放假之前写不完,剩下的寒假补上,初学难免有
向大家指点,但请不要使用过激之言。本系列数据库以sql sever为例
1、先来谈一下什么是ado.net
把含有一些.net的基本类,可以把这些类分解为数据提供对象和用户对象:
提供者对象专用于每一种类型的数据源,专用于提供者对象完成读写工作
用户对象是将数据读入到内存后用来访问和操纵数据。
2、提供者对象
1、连接对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;//使用SQL Sever data命名空间
namespace 数据库
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SqlConnection con = new SqlConnection(@"data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|
\dbNumber.mdf;Integrated Security=True;User Instance=True"))
            {
                con.Open();
             
            }//或用try catch finnly  做异常处理
            Console.WriteLine("打开数据库成功!");
            Console.ReadLine();
  }
}
使用sqlConnection连接对象,测试打开数据库
p_w_picpath
2、命令对象SqlCommand
可以向此对对对象数据库发送命令,如代码如下:
class Program
    {
        static void Main(string[] args)
        {
            using (SqlConnection conn = new SqlConnection(@"data Source=.\SQLEXPRESS;database=Mybase;user id=sa;password=123"))
            {
                conn.Open();//打开数据库
                Console.WriteLine("打开连接成功!");
                using (SqlCommand comm = conn.CreateCommand())
                {
                    comm.CommandText = "insert into mytable(id,Name,Age)values(5,'wy',19)";
                    comm.ExecuteNonQuery();
                    Console.WriteLine("数据插入成功!");
                }
                Console.Read();
            }
            
        }
    }
执行结果:
 p_w_picpath
p_w_picpath
最后一条是插入的(我没设主键啊)
3、删除数据和更新数据和插入数据语法基本相同,只是SQL语句的不同:
static void Main(string[] args)
        {
            using (SqlConnection conn = new SqlConnection(@"data Source=.\SQLEXPRESS;database=Mybase;user id=sa;password=123"))
            {
                conn.Open();//打开数据库
                Console.WriteLine("打开连接成功!");
                using (SqlCommand comm = conn.CreateCommand())
                {
                    comm.CommandText = "delete from Mytable where Name='june' ";
                    comm.ExecuteNonQuery();
                    Console.WriteLine("数据删除成功!");
                    
                }
                Console.Read();
            }
            
        }
 p_w_picpath p_w_picpath
4、用DataReader读取对象
 static void Main(string[] args)
        {
            using (SqlConnection conn = new SqlConnection(@"data Source=.\SQLEXPRESS;database=Mybase;user id=sa;password=123"))
            {
                conn.Open();//打开数据库
                Console.WriteLine("打开连接成功!");
                using (SqlCommand comm = conn.CreateCommand())
                {
                    comm.CommandText = "select * from Mytable where Age=20 ";
                    SqlDataReader reader = comm.ExecuteReader();
                    while (reader.Read())
                    {
                        Console.WriteLine("{0}\t{1}",reader["Name"],reader["Age"]);
                    }
                   
                }
                Console.Read();
            }
            
        }

执行结果:

p_w_picpath

5、最后以一个简单的登录系统来完成今天的学习

 static void Main(string[] args)
        {
            string name,password;
            Console.WriteLine("请输入你的用户名:");
            name = Convert.ToString(Console.ReadLine());
            Console.WriteLine("请输入你的密码:");
            password = Convert.ToString(Console.ReadLine());
            using (SqlConnection conn = new SqlConnection(@"data Source=.\SQLEXPRESS;database=Mybase;user id=sa;password=123"))
            {
                conn.Open();//打开数据库
                Console.WriteLine("打开连接成功!");
                using (SqlCommand comm = conn.CreateCommand())
                {

                    comm.CommandText = "select * from T_Load where Name='"+name+"'";
                    SqlDataReader reader = comm.ExecuteReader();
                    if (reader.Read())
                    {
                        string psw = reader.GetString(reader.GetOrdinal("_PassWord"));
                        if (password == psw)
                        {
                            Console.WriteLine("登陆成功!");
                        }
                        else
                        {
                            Console.WriteLine("密码输入错误!");
                        }

                    }
                    else
                    {
                        Console.WriteLine("用户名输入错误!");
                    }
                }
                Console.Read();
            }
            
        }
p_w_picpath
好了,本文学习了操作数据库的最基本的知识,下部分将是关于用户对象操作部分,如dataSet等