欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#測(cè)試本機(jī)sql運(yùn)算速度的代碼示例分享

 更新時(shí)間:2014年01月03日 10:21:12   作者:  
本文代碼目的很簡(jiǎn)單,就是使用c#測(cè)試一下本機(jī)sql運(yùn)算的速度,使用循環(huán)往數(shù)據(jù)里大量插入數(shù)據(jù),計(jì)算所用時(shí)間,大家參考使用吧

復(fù)制代碼 代碼如下:

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection();
            SqlCommand comm = new SqlCommand();
            DateTime t1, t2;
            int count = 10000;  //循環(huán)次數(shù)
            string times;
            conn.ConnectionString = "Data Source=.;Initial Catalog=Server;Integrated Security=True";
            comm.CommandText = "insert into test (Cid,Cvalue) values('1','1')"; //數(shù)據(jù)插入
            comm.Connection = conn;
            Console.WriteLine("開(kāi)始插入數(shù)據(jù)\r\n開(kāi)始時(shí)間:" +(t1=DateTime.Now).ToLongTimeString());
            try
            {
                conn.Open();
                for (int i = 1; i <= count; i++)
                {
                    comm.ExecuteNonQuery(); //執(zhí)行查詢(xún)
                }
                Console.WriteLine("結(jié)束時(shí)間:" + (t2 = DateTime.Now).ToLongTimeString());
                times = GetTimeSpan(t1, t2).ToString();
                Console.WriteLine("持續(xù)時(shí)間:" + times.Substring(0, times.LastIndexOf(".") + 4));
                Console.WriteLine("本次測(cè)試總共對(duì)數(shù)據(jù)庫(kù)進(jìn)行了" + count + "次數(shù)據(jù)插入操作!");
                //comm.CommandText = "delete from test";
                //comm.ExecuteNonQuery();
                //Console.WriteLine("測(cè)試數(shù)據(jù)已刪除");
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            }
            finally
            {
                comm = null;
                conn.Close();
                conn.Close();
            }
            Console.ReadKey();
        }

        /// <summary>
        /// 返回兩個(gè)時(shí)間對(duì)象的時(shí)間間隔
        /// </summary>
        private static TimeSpan GetTimeSpan(DateTime t1, DateTime t2)
        {
            DateTime t3;
            if (DateTime.Compare(t1, t2) == 1)
            {
                t3 = t1;
                t1 = t2;
                t2 = t3;
            }
            return t2.Subtract(t1);
        }
    }
}

相關(guān)文章

最新評(píng)論