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);
}
}
}
新聞熱點(diǎn)
疑難解答
圖片精選