1. 程式人生 > >連接數據庫測試

連接數據庫測試

list dataset eno 超時 mman ogr northwind data lda

技術分享圖片
 1 use Northwind
 2 go    
 3 if exists(select * from sysobjects where name=w_cs)
 4     drop procedure w_cs
 5 go
 6     create proc w_cs
 7     (
 8     @id int
 9     )
10     as
11     SELECT * FROM [Northwind].[dbo].[Categories]
12     where CategoryID<@id
寫的存儲過程

技術分享圖片
using System;
using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; namespace 使用SqlParameter給sql語句傳參數 { public class myclass1 { public myclass1() { } public myclass1(int idtemp, string nametemp) { id = idtemp; name
= nametemp; } public int id { get; set; } public string name { get; set; } } //定義一個類,來格式化數據庫傳過來的啥東東 class Program { static void Main(string[] args) { string ip = "127.0.0.1"; string db = "Northwind"; string uid = "
sa"; string pwd = "sa"; string constr = "Data Source=" + ip + ";" + "Initial Catalog=" + db + ";" + "uid=" + uid + ";" + "pwd=" + pwd; SqlConnection cnn = new SqlConnection(constr); //使用SqlConnectionStringBuilder寫法會簡單些 string tsql = "SELECT * FROM [Northwind].[dbo].[Categories] where CategoryID<@iid";//用的語句 System.Data.SqlClient.SqlCommand tsql2 = new SqlCommand(tsql, cnn); //SqlCommand可以使用查詢語句 也可以使用存儲過程 tsql2.CommandType = CommandType.Text; //指定sql要執行的是語句,不是存儲過程和事務;默認就是 CommandType.Text int id = 5; SqlParameter[] param = new SqlParameter[] { new SqlParameter("@iid",id), //給這個數組賦值,還可以相同格式繼續添加 }; tsql2.Parameters.AddRange(param); cnn.Open(); //都定義完了開始執行了,這裏要做異常捕獲:免得超時報錯 SqlDataReader jg = tsql2.ExecuteReader(); List<myclass1> lis = new List<myclass1>();//定義一個泛型 while (jg.Read()) { myclass1 myclass_temp = new myclass1(jg.GetInt32(0), jg.GetString(1)); lis.Add(myclass_temp); }//將數據庫查詢出的值逐條放入泛型中 cnn.Close();//執行完了 // tsql2.ExecuteNonQuery();//可以用這個查詢出影響了幾行數據,也可以用他來觸發執行 foreach (myclass1 item in lis) { Console.WriteLine(item.id + item.name);//遍歷出查詢結果 } Console.ReadLine(); } } }
使用SqlParameter給sql語句傳參數 技術分享圖片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace 使用SqlParameter給存儲過程傳參數並顯示執行結果
{
    public class myclass1
    {
        public myclass1() { }
        public myclass1(int idtemp, string nametemp)
        {
            id = idtemp;
            name = nametemp;
        }
        public int id { get; set; }
        public string name { get; set; }
    } //定義一個類,來格式化數據庫傳過來的啥東東






    class Program
    {
        static void Main(string[] args)
        {
            string ip = "127.0.0.1";
            string db = "Northwind";
            string uid = "sa";
            string pwd = "sa";
            string constr = "Data Source=" + ip + ";" + "Initial Catalog=" + db + ";" + "uid=" + uid + ";" + "pwd=" + pwd;
            SqlConnection cnn = new SqlConnection(constr); //使用SqlConnectionStringBuilder寫法會簡單些

            string tsql = "w_cs";//指定存儲過程名
            System.Data.SqlClient.SqlCommand tsql2 = new SqlCommand(tsql, cnn); //SqlCommand可以使用查詢語句 也可以使用存儲過程

            tsql2.CommandType = CommandType.StoredProcedure;  //指定sql要執行的是存儲過程
            int id = 5;
            SqlParameter[] param = new SqlParameter[]
                {
                     new SqlParameter("@id",id), //給這個數組賦值,還可以相同格式繼續添加 @id 是存儲過程中的變量
                      //new SqlParameter("@office_am",office_am),
                 //new SqlParameter("@office_pm",office_pm),
                 //new SqlParameter("@factory_am",factory_am),
                 //new SqlParameter("@factory_pm",factory_pm),
                 //new SqlParameter("@memo",memo)
                };
            tsql2.Parameters.AddRange(param);


            cnn.Open();  //都定義完了開始執行了,這裏要做異常捕獲:免得超時報錯
            SqlDataReader jg = tsql2.ExecuteReader();
            List<myclass1> lis = new List<myclass1>();//定義一個泛型

            while (jg.Read())
            {
                myclass1 myclass_temp = new myclass1(jg.GetInt32(0), jg.GetString(1));
                lis.Add(myclass_temp);

            }//將數據庫查詢出的值逐條放入泛型中

            cnn.Close();//執行完了
            // tsql2.ExecuteNonQuery();//可以用這個查詢出影響了幾行數據,也可以用他來觸發執行

            foreach (myclass1 item in lis)
            {

                Console.WriteLine(item.id + item.name);//遍歷出查詢結果
            }
            Console.ReadLine();




        }
    }
}
使用SqlParameter給存儲過程傳參數並顯示執行結果 技術分享圖片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace 使用DataSet將數據讀取到本地緩存2
{
    class Program
    {
        static void Main(string[] args)
        {
            string ip = "127.0.0.1";
            string db = "Northwind";
            string uid = "sa";
            string pwd = "sa";
            string constr = "Data Source=" + ip + ";" + "Initial Catalog=" + db + ";" + "uid=" + uid + ";" + "pwd=" + pwd;
            SqlConnection cnn = new SqlConnection(constr); //使用SqlConnectionStringBuilder寫法會簡單些


            string tsql = "w_cs";//指定存儲過程名
            //SqlCommand tsql2 = new SqlCommand();
            //tsql2.Connection = cnn;
            //tsql2.CommandText = tsql;


           //SqlCommand tsql2 = new SqlCommand(tsql, cnn); //這個可以看做簡寫

            SqlDataAdapter da = new SqlDataAdapter(tsql,cnn);
            
            // da.SelectCommand = tsql2;
           da.SelectCommand.CommandType = CommandType.StoredProcedure;
            
            int id = 5;

            SqlParameter[] param = new SqlParameter[]
                {
                     new SqlParameter("@id",id)
                };
            da.SelectCommand.Parameters.AddRange(param);
            //tsql2.Parameters.AddRange(param);

            DataSet ds1 = new DataSet();
            da.Fill(ds1);
            foreach (DataRow item in ds1.Tables[0].Rows)
            {
                Console.WriteLine(item[0].ToString());
            }
            Console.ReadLine();
        }
    }
}
使用DataSet將數據讀取到本地緩存,並遍歷出來

數據讀取下來了,還可以寫個類,傳給集合,從集合裏得到值

連接數據庫測試