猿问

加锁导致内存占用逐渐加大?怎么办

 1  
 2 这是第一个窗体的代码
 3 
 4  private void button1_Click(object sender, EventArgs e)
 5         {
 6             while (true)
 7             {
 8                 SqlConnection conn = new SqlConnection("Data Source=DMWIAJHLRQMX6QS;Initial Catalog=test;Persist Security Info=True;User ID=sa;Pwd=111111");
 9                 conn.Open();
10                 string cmdStr = "insert into test_interfacetable0760 with(xlock) (SendPhone,text,SendTime,states,UserID) values ('13888888888','测试内容','2013-8-14',1,32) ";
11                 SqlCommand cmd = new SqlCommand();
12                 cmd.Connection = conn;
13                 cmd.CommandText = cmdStr;
14                 try
15                 {
16                     cmd.ExecuteNonQuery();
17                 }
18                 catch (Exception ex)
19                 {
20                     MessageBox.Show(ex.Message);
21                 }
22                 finally
23                 {
24                     conn.Close();
25                 }
26             }
27         }
28 
29 
30 这是第二个窗体的代码
31 
32 private void button1_Click(object sender, EventArgs e)
33         {
34             while (true)
35             {
36                 SqlConnection conn = new SqlConnection("Data Source=DMWIAJHLRQMX6QS;Initial Catalog=test;Persist Security Info=True;User ID=sa;Pwd=111111");
37                 conn.Open();
38                 SqlCommand cmd = new SqlCommand();
39                 cmd.Connection = conn;
40                 string selecStr = "select top 1 * from test_interfacetable0760 with(xlock)  where states=1 ";
41                 cmd.CommandText = selecStr;
42                 int tempid = Convert.ToInt32(cmd.ExecuteScalar());
43                 string cmdStr = "update test_interfacetable0760 with(xlock)   set SendPhone='13777777777',text='更新的测试内容',SendTime='2000-8-14',states=2,UserID=22 where tempid=" + tempid + "";
44                 cmd.CommandText = cmdStr;
45                 try
46                 {
47                     cmd.ExecuteNonQuery();
48                 }
49                 catch (Exception ex)
50                 {
51                     MessageBox.Show(ex.Message);
52                 }
53                 finally
54                 {
55                     conn.Close();
56                 }
57             }
58         }

我 开着两个程序同时运行的时候,操作什么的都正确(数据库可以看见这些操作的正确性),可是内存在不停的增加,cpu也占用100%了,这是为什么呢

阿波罗的战车
浏览 794回答 5
5回答

呼如林

把while循环去掉

森林海

这不是加锁的问题,是代码的问题,建议改进代码

慕标琳琳

去掉while?
随时随地看视频慕课网APP

相关分类

SQL Server
我要回答