c#测试本机sql运算速度的代码示例分享
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);
}
}
}