c test native sql speed code sample sharing
- 2020-05-30 20:58:37
- OfStack
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; // cycles
string times;
conn.ConnectionString = "Data Source=.;Initial Catalog=Server;Integrated Security=True";
comm.CommandText = "insert into test (Cid,Cvalue) values('1','1')"; // Insert data into
comm.Connection = conn;
Console.WriteLine(" Start inserting data \r\n The start time :" +(t1=DateTime.Now).ToLongTimeString());
try
{
conn.Open();
for (int i = 1; i <= count; i++)
{
comm.ExecuteNonQuery(); // Execute the query
}
Console.WriteLine(" The end of time :" + (t2 = DateTime.Now).ToLongTimeString());
times = GetTimeSpan(t1, t2).ToString();
Console.WriteLine(" The duration of the :" + times.Substring(0, times.LastIndexOf(".") + 4));
Console.WriteLine(" This test was done on the database in total " + count + " Secondary data insert operation! ");
//comm.CommandText = "delete from test";
//comm.ExecuteNonQuery();
//Console.WriteLine(" The test data has been deleted ");
}
catch (Exception err)
{
Console.WriteLine(err.Message);
}
finally
{
comm = null;
conn.Close();
conn.Close();
}
Console.ReadKey();
}
/// <summary>
/// Returns the time interval between two time objects
/// </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);
}
}
}