C#實現ADO連接sql server數據庫

我對ADO的理解不是多麼的透徹,到目前爲止我感覺ADO可能就是和JDBC一樣的作用,都可以連接數據庫。
目前我實現的只有使用c#通過ADO來連接sqlserver(mysql)
連接sqlServer數據庫首先下載一個sqlserver數據庫操作程序(如果可以不下就當我沒說),然後如果沒有sqlSclient程序包的話還要再VS中下載這個程序包,然後就可以進行寫代碼了
連接mysql數據庫的時候要下載一個驅動包 mysql-for-visualstudio-1.2.9.msi,版本要與電腦版本匹配,怎麼下載合適的可以百度搜索,然後進行配置一波(同百度)就可
通過存儲數據的User類

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp2
{
    class User
    {
        int id;
        string name;
        public User(int id,string name)
        {
            this.id = id;
            this.name = name;
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public void show()
        {
            Console.WriteLine(id+"  "+name);
        }
    }
}

在這裏插入圖片描述
![在這裏插入圖片描述](https://img-blog.csdnimg.cn/20200528192441913.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNjI3MDc2,size_16,color_FFFFFF,t_70

//下述的程序包最好自己手動導入吧
using System;
using System.Runtime.CompilerServices;
using System.Data.SqlClient;
using System.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.ComponentModel.Design;

namespace ConsoleApp2
{
    class Program
    {
        private string ConString = "Data Source=(local);Initial Catalog=cus;Integrated Security=TRUE";  //連接sqlserver的字符串,Data Source 對應上述的服務器名稱
                 //Initial Catalog對應的數據庫名稱,最後一個參數按照原文中即可
        SqlConnection con;        
        //與sqlserver數據庫建立的連接(mysql數據庫對應的是MysqlConnection)
        public Program()
        {
            
        }
        //增
        public int Add(User user)
        {
            try
            {
                con = new SqlConnection(ConString);
                con.Open();
                string sql = "insert into Table_1 values('"+user.Name+"')";
                SqlCommand command = con.CreateCommand();       
                //執行的命令對象(mysql對應的MysqlCommand)
                command.CommandText = sql;
                int a = command.ExecuteNonQuery();
                con.Close();
                return a;
            }
            catch
            {
                Console.WriteLine("error");
                return -99;
            }
        }
        //查
        public User select(int id)
        {
            con = new SqlConnection(ConString);
            string sql = "select * from Table_1" ;
            con.Open();
            User user=null;
            SqlCommand command = new SqlCommand(sql, con);
            SqlDataReader reader = command.ExecuteReader();
            string imfor;
            if(reader.Read())
            {
                user=new User((int)reader[0],(string)reader[1]);
            }
            con.Close();
            return user;
        }
        //刪
        public int delete(int id)
        {
            con = new SqlConnection(ConString);
            string sql = "delete from Table_1 where id='" + id + "'";
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);
            int a = command.ExecuteNonQuery();
            con.Close();
            return a;
        }
        //更新
        public int update(User user)
        {
            con = new SqlConnection(ConString);
            string sql = "update Table_1 set name='" + user.Name+"' where id="+user.Id;
            con.Open();
            SqlCommand command = new SqlCommand(sql, con);
            int a = command.ExecuteNonQuery();
            con.Close();
            return a;
        }
        //查詢所有數據
        public LinkedList<User> selectAll()
        {
            con = new SqlConnection(ConString);
            LinkedList<User> list = new LinkedList<User>();
            con.Open();
            string sql = "select * from Table_1 ";
            SqlCommand command = new SqlCommand(sql, con);
            SqlDataReader reader = command.ExecuteReader();
            while(reader.Read())
            {
                User user = new User((int)reader[0], (string)reader[1]);
                list.AddLast(user);
            }
            con.Close();
            return list;
        }
        static void Main(string[] args)
        {
            Program p= new Program();
            User user1 = new User(3, "222");
            User user2 = new User(2, "33333");
            p.Add(user1);
            p.delete(1);
            p.select(2).show();
            p.update(user2);
            p.select(2);
            LinkedList<User> list= p.selectAll();


           foreach(User user in list)
            {
                user.show();
            }
            
        }
    }
}

實現連接mysql數據庫的方式與上述代碼相似
eg:

using System;
using System.ComponentModel.Design;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace ConsoleApp3
{
    class Program
    {
        private MySqlConnection con;
        public Program()
        {
            con = new MySqlConnection("server = 127.0.0.1; port = 3306; user = root; password = 123; database = cus");
        }
        public void select()
        {
            string sql = "select * from user";
            con.Open();
            MySqlCommand command = new MySqlCommand(sql, con);
            MySqlDataReader reader = command.ExecuteReader();
            while(reader.Read())
            {
                Console.WriteLine(reader[0]+" "+reader[1]);
            }
            con.Close();
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.select();
        }
    }
}

只是實現了連接數據庫還有查詢功能,其他的功能代碼類似於連接sqlserver數據庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章