如何减少sql express中的数据访问时间 [英] how to reduce the time for data access in sql express
本文介绍了如何减少sql express中的数据访问时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨。,
我写的应用程序会在一秒内插入数据库10-20次,每次耗费大量时间这是代码
Hi.,
I have written an applications that will insert into database 10-20 times with in a second, this is consuming a lot of time every time here is the code
public bool Insertlog(string deviceid, string Devicename, string phase1)
{
try
{
DataSet ds = new DataSet();
string query;
query = "insert into livedatalogs (deviceid,DeviceName, phase1, date,Time) values";
query += "('" + deviceid + "','" + Devicename + "','" + phase1 + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + DateTime.Now.ToString("hh:mm:ss tt") + "')";
SqlCommand cmd = new SqlCommand(query, GetConnection());
if (cmd.ExecuteNonQuery() > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception r)
{
MessageBox.Show(r.Message);
return false;
}
finally
{
CloseConnection();
}
}
获取连接的代码
code for get connection
public static SqlConnection GetConnection()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["newConnectionString"].ConnectionString);
conn.Open();
return conn;
}
密码连接代码
Code for close connection
public static SqlConnection CloseConnection()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["newConnectionString"].ConnectionString);
conn.Close();
return conn;
}
如何优化它,因为我运行应用程序几个小时,记录速度下降导致应用程序停止。< br $> b $ b
等待任何帮助
提前感谢
how can optimize it as i am running the application for hours the logging speed drops causes application to stop.
waiting for any help
thanks in advance
推荐答案
SQL数据库的插入性能有限,提高速度的一种方法是删除表上的索引,如果你真的需要高插入速度,请尝试使用NoSql数据库,甚至是您拥有的用例的纯文本文件..
Insert performance of a SQL database is limited, one way to increase speed is removing indexes on the table, if you really need high insert speed try using a NoSql database instead or even a plain text file for the usecase you have..
执行以下操作
Do as below
public bool Insertlog(string deviceid, string Devicename, string phase1)
{
try
{
string query = "";//build your sql ;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["newConnectionString"].ConnectionString))
using (SqlCommand cmd = new SqlCommand(query, conn))
{
conn.Open();
return cmd.ExecuteNonQuery() > 0;
}
}
catch (Exception r)
{
MessageBox.Show(r.Message);
return false;
}
}
这篇关于如何减少sql express中的数据访问时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文