http://community.csdn.net/Expert/topic/5702/5702089.xml?temp=.7177393我用
private void ExecuteInteral(SqlDbTranaction trans, sql)
{
SqlCommand cmd = trans.Connection.CreateCommand();
cmd.Transaction = trans;
cmd.CommandText = sql; 
cmd.ExecuteNonQuery();

这样的程序向数据库
user添加100条数据
然后里面有这样一段代码
string sql="select top 1 usercode from usertab order by id desc";
string usercode = .. //根据上面那一句得到usertab表中最后一条记录的usercode数据
usercode= GetMyStyle(usercode);
sql="insert into usertab ([name],[usercode]) value ('"+myname+"','"+ usercode+"')";像这样的话当程序在运行第二遍的时候
string sql="select top 1 usercode from usertab order by id desc";
string usercode = .. //根据上面那一句得到usertab表中最后一条记录的usercode
就出错了,提示:超时时间已到。在操作完成之前超时时间已过或服务器未响应。我自己的想法是
因为select top 1 usercode from usertab order by id desc,是得到最后一条的数据
但这里因为用了Transaction 
他第一遍的时候是添加了一和数据,但他还没有提交
然后第二遍的时候又去查询最后一条,但最后一条还没有提交
所以就出错了这里要怎么办

解决方案 »

  1.   

    我自己的想法是
    因为select top 1 usercode from usertab order by id desc,是得到最后一条的数据
    但这里因为用了Transaction 
    他第一遍的时候是添加了一和数据,但他还没有提交
    然后第二遍的时候又去查询最后一条,但最后一条还没有提交
    所以就出错了======不,你理解错了,你应该好好补习下,关于关系数据库事务处理的相关内容,包括与之相关的隔离级别没有提交, 是没有真正的写入表中, 但是写入了日志中, 对于同一连接的用户, 来说是可见的, 但是其他连接的用户是不可见的
    超时时间已到。在操作完成之前超时时间已过或服务器未响应
    ======
    可能是由于, 你的执行的总时间太长了, 100 条数据??? 你可以计算大, 从开始写入到出错,用了多久,
    默认应该是 15s 尝试,连接字符串中, 加入超时选项 Connection Timeout

    string connStr = "Connection Timeout=30;server=.;datasource"
      

  2.   

    sorry, 连接字符串的 Connection Timeout 是指尝试建立连接( Connection.Open ) 的超时时间,
    尝试设置Command.CommandTimeout = 30s;
      

  3.   

    主要他是在这一行里面就停死了
    select top 1 usercode from usertab order by id desc