數據庫連接查詢 一、

暈死了,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);
            }
        }
    }

代碼全是自己寫的,還好舒服多了。

小生剛學點,希望各位大蝦以後給予支持。不要咒罵我哦,我知道這個很垃圾。。。

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