none
winform中多线程出现锁表的情况如何避免? RRS feed

  • 问题

  • winform程序中使用多线程可能在同一时刻update同一张表的不同行的数据照成鼠标的情况!

    private void tongdaoa()
            {
                try
                {
                    string conn = ConfigurationSettings.AppSettings["db"];
                    string conna = ConfigurationSettings.AppSettings["dba"];
                    string sql = "select top 1000 id,mobile,bianhao,priority,flag,zu,date from info where zu='A' and date<='" + DateTime.Now.ToString() + "' and flag='0' order by priority desc";
                    SqlConnection cn = new SqlConnection(conn);
                    SqlConnection cna = new SqlConnection(conna);
                    cn.Open();
                    SqlDataAdapter adapt = new SqlDataAdapter(sql, cn);
                    DataSet ds = new DataSet();
                    adapt.Fill(ds, "info");
                    if (ds.Tables[0].Rows.Count != 0)
                    {
                        string sqlid = "Update info set flag='1' where";
                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        DataTable dataTable = new DataTable();
                        dataTable.Columns.Add("id", typeof(int));
                        dataTable.Columns.Add("mobile", typeof(string));
                        dataTable.Columns.Add("bianhao", typeof(string));
                        for (int i = 1; i <= ds.Tables[0].Rows.Count; i++)
                        {
                            sendid += " send_id=" + ds.Tables[0].Rows[i - 1][0].ToString() + " or";
                            DataRow dataRow = dataTable.NewRow();
                            dataRow["id"] = "1";
                            dataRow["mobile"] = ds.Tables[0].Rows[i - 1][1].ToString();
                            dataRow["bianhao"] = ds.Tables[0].Rows[i - 1][2].ToString();
                            dataTable.Rows.Add(dataRow);
                        }
                        try
                        {
                            SqlBulkCopy copy = new SqlBulkCopy(conncopy);
                            copy.DestinationTableName = "info";
                            copy.WriteToServer(dataTable);
                            cna.Close();
                            sqlid += sendid.Substring(0, sendid.Length - 3);
                            SqlCommand com = new SqlCommand(sqlid, cn);
                            int ss = com.ExecuteNonQuery();
                            sqlid = "Update info set flag='1' where";
                        }
                        catch (Exception ex)
                        {
                            sqlid = "Update info set flag='1' where";
                            sqlid += sendid.Substring(0, sendid.Length - 3);
                            SqlCommand com = new SqlCommand(sqlid, cn);
                            com.ExecuteNonQuery();
                            sqlid = "Update info set flag='1' where";
                            cn.Close();
                        }
                        cna.Close();
                        cn.Close();
                    }
                    cn.Close();
                }
                catch (Exception ex)
                {
                    WriteDebugEntry(ex.ToString());
                }
            }
    这样同样的三个线程(因为要提交给三台不同的服务器),就是在莫一时刻出现锁死的情况!请教下代码哪方面有问题?   高手指点下谢谢
    2012年8月7日 8:38

答案

全部回复