作为我的上一个问题:如何保持到数据库直到屏幕关闭?
?首先,让我对我不解释我的情况表示歉意。
好吧,我的情况最多可更新一百个记录。我使用For loop
创建一个真实的工作并记录其结果。
private void button1_Click(object sender, EventArgs e)
{
int i;
KeyEventArgs keyEvent = new KeyEventArgs(Keys.Enter); //Create keydown event
Performance perf = new Performance(); //Class for measure time and logging
perf.Start(); //Start stopwatch
for (i = 1; i <= 100; i++)
{
txtLotNo.Text = i.ToString("0000000000") + "$01"; //Generate input ID
txtLotNo_KeyDown(sender, keyEvent); //Fire keydown event
}
perf.Stop(); //Stop stopwatch
perf.Log(frmInvCtrl.appPath,"Stock In (Stay connected)- " + frmInvCtrl.instance); //Logging
}
这是一个表演类。
class Performance
{
private Stopwatch _sw = new Stopwatch(); //Create stopwatch property
public double GetWatch
{
get
{
return this._sw.ElapsedMilliseconds;
}
}
public void Start()
{
Stop();
_sw.Reset();
_sw.Start();
}
public void Stop()
{
if (_sw.IsRunning)
{
_sw.Stop();
}
}
public void Log(string path,string menu)
{
string logName = path + "\Log_" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";
string logDetail = System.DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + " - [" + menu + "] "
+ "Process 100 record in [" + (((double)_sw.ElapsedMilliseconds / 1000)).ToString() + "] seconds";
using(StreamWriter writer = new StreamWriter(logName,true))
{
writer.WriteLine(logDetail); //wirtelog
}
}
}
这些是日志结果
2017/02/19 08:16:05 - [Stock In - On Cloud] Process 100 record in [68.352] seconds
2017/02/19 08:17:34 - [Stock In - On Cloud] Process 100 record in [70.184] seconds
2017/02/19 08:20:28 - [Stock In - On Cloud] Process 100 record in [56.66] seconds
2017/02/19 08:21:34 - [Stock In - On Cloud] Process 100 record in [60.605] seconds
2017/02/19 08:22:44 - [Stock In - On Cloud] Process 100 record in [68.27] seconds
2017/02/19 08:24:43 - [Stock In - Network Server] Process 100 record in [46.86] seconds
2017/02/19 08:26:05 - [Stock In - Network Server] Process 100 record in [31.746] seconds
2017/02/19 08:26:48 - [Stock In - Network Server] Process 100 record in [31.859] seconds
2017/02/19 08:27:32 - [Stock In - Network Server] Process 100 record in [31.003] seconds
2017/02/19 08:28:17 - [Stock In - Network Server] Process 100 record in [40.487] seconds
2017/02/19 08:32:42 - [Stock In (Stay connected)- On Cloud] Process 100 record in [18.196] seconds
2017/02/19 08:35:47 - [Stock In (Stay connected)- On Cloud] Process 100 record in [14.721] seconds
2017/02/19 08:36:30 - [Stock In (Stay connected)- On Cloud] Process 100 record in [15.903] seconds
2017/02/19 08:37:31 - [Stock In (Stay connected)- On Cloud] Process 100 record in [15.811] seconds
2017/02/19 08:38:15 - [Stock In (Stay connected)- On Cloud] Process 100 record in [16.4] seconds
2017/02/19 08:43:08 - [Stock In (Stay connected)- Network Server] Process 100 record in [13.09] seconds
2017/02/19 08:43:25 - [Stock In (Stay connected)- Network Server] Process 100 record in [13.03] seconds
2017/02/19 08:43:40 - [Stock In (Stay connected)- Network Server] Process 100 record in [13.051] seconds
2017/02/19 08:43:55 - [Stock In (Stay connected)- Network Server] Process 100 record in [12.992] seconds
2017/02/19 08:44:12 - [Stock In (Stay connected)- Network Server] Process 100 record in [14.953] seconds
我是通过连接池进行的。但是,在许多记录情况下,显示的这些结果保持在数据库中更快。
是否有适合这种情况的练习?
编辑:2017/02/21
这是打开表单时的打开连接:
private void frm_Load(object sender, EventArgs e) //Open menu
{
... //statement
frmMain.sqlConn1 = new SqlConnection();
frmMain.sqlConn1.ConnectionString = frmMain.connectionString1;
frmMain.sqlConn1.Open();
... //statement
}
更新代码:
public static long ScanUpdate(string lotNo)
{
string scanLotNo = "";
int scanIndex = 0;
if (!SplitBarcode(lotNo, ref scanLotNo, ref scanIndex))
{
//Invalid Barcode data
return -919;
}
//Prepare sql command
string updStatus = (frmMain.shelfScan) ? "05" : "10";
string sql = <sql statement>
try
{
using (SqlCommand sqlCmd = new SqlCommand(sql, frmMain.sqlConn1)) //frmMain.sqlConn1 is connection in form_Load()
{
if (sqlCmd.ExecuteNonQuery() <= 0)
{
//No row affect
//frmMain.sndPlay.Play();
return -99;
}
else
{
//Completed
return 0;
}
}
}
catch
{
return 99;
}
finally
{
}
}
出口时处置连接
private void btnBack_Click(object sender, EventArgs e)
{
frmMain.sqlConn1.Dispose();
this.Close();
}
从单个线程的响应的角度来看,保持连接的速度将更快。连接池的目的是通过在线程之间共享新连接的费用,同时又不消耗共享SQL Server上过多的连接。
每当将连接释放到连接池,然后重复使用时,协议堆栈将呼叫SP_ResetConnection以清理服务器上的状态。您可以通过针对SQL Server运行Profiler Tract。
由于每个过程都有每个连接字符串的连接池,因此您只有在一个过程中存在连接的争论时,您才能从连接池中受益。
这里每个人都缺少的部分是其 Windows CE ,紧凑型版本上的开放连接并不罕见的打开连接可能真的很慢。
那就是说,那些时代看起来确实有些夸张。请参阅此质量检查的方法:如何使我的SQL Server CE连接更快地打开?
在许多领域中,您可以在上面的代码中遇到问题。
- 您正在创建一个
SqlConnection
实例。 - 您所有的事件处理程序都称为静态功能,该功能引用了一个SQLConnection实例。
- 您的活动处理程序不检查它们是否在主UI线程上运行。
SqlConnection
类将自动在封面下的连接。当您调用close()
函数时,连接只会返回池。您可以通过连接字符串中的属性来控制池行为。通过保留所有功能相同的连接对象,您将强迫它们序列化。
这是对某些连接字符串属性的引用。看看Connection Lifetime
。
我将从form_load()
函数中删除SqlConnection
实例,然后写下ScanUpdate
如下:
public static long ScanUpdate(string lotNo)
{
string scanLotNo = "";
int scanIndex = 0;
if (!SplitBarcode(lotNo, ref scanLotNo, ref scanIndex))
{
//Invalid Barcode data
return -919;
}
//Prepare sql command
string updStatus = (frmMain.shelfScan) ? "05" : "10";
string sql = <sql statement>
try
{
using (SqlConnection conn = new SqlConection(frmMain.connectionString1)) {
SqlCommand sqlCmd = new SqlCommand(sql, conn);
if (sqlCmd.ExecuteNonQuery() <= 0)
{
//No row affect
//frmMain.sndPlay.Play();
return -99;
}
else
{
//Completed
return 0;
}
conn.Close();
}
}
catch
{
return 99;
}
}
对于您的*_Click
事件处理程序功能,请确保检查是否需要重新启动该事件:
private void button1_Click(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new EventArgsDelegate(button1_Click), new object[] { sender, ea });
}
// Do some stuff
}
有关事件处理的更多详细信息,请参见此答案。