数据库连接查询 一、

晕死了,NN 久了,很长时间不知道自己是了解还是不了解一个数据库的连接,今天还是自己手动的去写了一个,感觉还算好,没有那么多的难度。

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                System.Data.SqlClient.SqlConnection sqlconnection;
                System.Data.SqlClient.SqlCommand sqlcommand;
                sqlconnection = new System.Data.SqlClient.SqlConnection();
                sqlconnection.ConnectionString = "Data Source=(local);Initial Catalog=Northwind;User ID=sa";
                sqlcommand = new System.Data.SqlClient.SqlCommand();
                sqlcommand.Connection = sqlconnection;
                sqlcommand.CommandText = "select * from orders where EmployeeID=5 and customerid='vinet'";
                Console.WriteLine("loading...");
                sqlconnection.Open();
                SqlDataReader reader = sqlcommand.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine("{0}-{1}", reader["orderid"].ToString (), reader["customerid"].ToString ());
                }
                reader.Close();
                sqlcommand.CommandText = "DELETE FROM ORDERS where ORDERID=455656";
                Console.WriteLine(sqlcommand .ExecuteNonQuery ().ToString ());
                sqlconnection.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }

代码全是自己写的,还好舒服多了。

小生刚学点,希望各位大虾以后给予支持。不要咒骂我哦,我知道这个很垃圾。。。

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