国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c#測試本機sql運算速度的代碼示例分享

2020-01-24 02:57:26
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

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;  //循環次數
            string times;
            conn.ConnectionString = "Data Source=.;Initial Catalog=Server;Integrated Security=True";
            comm.CommandText = "insert into test (Cid,Cvalue) values('1','1')"; //數據插入
            comm.Connection = conn;
            Console.WriteLine("開始插入數據/r/n開始時間:" +(t1=DateTime.Now).ToLongTimeString());
            try
            {
                conn.Open();
                for (int i = 1; i <= count; i++)
                {
                    comm.ExecuteNonQuery(); //執行查詢
                }
                Console.WriteLine("結束時間:" + (t2 = DateTime.Now).ToLongTimeString());
                times = GetTimeSpan(t1, t2).ToString();
                Console.WriteLine("持續時間:" + times.Substring(0, times.LastIndexOf(".") + 4));
                Console.WriteLine("本次測試總共對數據庫進行了" + count + "次數據插入操作!");
                //comm.CommandText = "delete from test";
                //comm.ExecuteNonQuery();
                //Console.WriteLine("測試數據已刪除");
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            }
            finally
            {
                comm = null;
                conn.Close();
                conn.Close();
            }
            Console.ReadKey();
        }

        /// <summary>
        /// 返回兩個時間對象的時間間隔
        /// </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);
        }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荣成市| 桑日县| 兰考县| 庄浪县| 阳原县| 永靖县| 巴青县| 浦北县| 奉化市| 松潘县| 桃园市| 连州市| 武威市| 连江县| 红安县| 和田县| 集贤县| 南靖县| 保康县| 建德市| 朝阳市| 体育| 布拖县| 东宁县| 南雄市| 太仆寺旗| 英吉沙县| 剑阁县| 榆社县| 武冈市| 东城区| 周宁县| 都匀市| 乌恰县| 开远市| 翁源县| 靖西县| 延安市| 会同县| 高陵县| 徐闻县|