如果有一个方法/// <summary> 
/// 增加库室 
/// </summary> 
/// <param name="libraryplace">库室表实体</param> 
/// <returns></returns> 
public bool AddLibraryPlace(LibraryPlace libraryplace) 

StringBuilder strSql = new StringBuilder(100);
strSql.Append("INSERT INTO [LibraryPlace]( "); 
strSql.Append("[SchoolID],[LibraryName], [LibraryAddress] )"); 
strSql.Append("VALUES ("); 
strSql.Append("@SchoolID,@LibraryName, @LibraryAddress ) "); 
DbParameter[] parms = new SqlParameter[3]; 
parms[0] = DbHelper.ParaInstance("@LibraryName", (DbType)SqlDbType.NVarChar, 50, libraryplace.LibraryName, ParameterDirection.Input); 
parms[1] = DbHelper.ParaInstance("@LibraryAddress", (DbType)SqlDbType.NVarChar, 100, libraryplace.LibraryAddress, ParameterDirection.Input); 
parms[2] = DbHelper.ParaInstance("@SchoolID", (DbType)SqlDbType.Int, 4, libraryplace.SchoolID, ParameterDirection.Input); 
flag = DbHelper.ExecuteNonQuery(strSql.ToString(), CommandType.Text, DbHelper.GuanBar_Book_ConnString, parms); 
return flag; 

如果多人同时访问这个方法
会出现strSql数据错乱的情况吗?

解决方案 »

  1.   

    在方法外定义StringBuilder strSql
    然后在方法内这样使用/// <summary> 
    /// 增加库室 
    /// </summary> 
    /// <param name="libraryplace">库室表实体</param> 
    /// <returns></returns> 
    public bool AddLibraryPlace(LibraryPlace libraryplace) 

    strSql = new StringBuilder(100);
    strSql.Append("INSERT INTO [LibraryPlace]( "); 
    strSql.Append("[SchoolID],[LibraryName], [LibraryAddress] )"); 
    strSql.Append("VALUES ("); 
    strSql.Append("@SchoolID,@LibraryName, @LibraryAddress ) "); 
    DbParameter[] parms = new SqlParameter[3]; 
    parms[0] = DbHelper.ParaInstance("@LibraryName", (DbType)SqlDbType.NVarChar, 50, libraryplace.LibraryName, ParameterDirection.Input); 
    parms[1] = DbHelper.ParaInstance("@LibraryAddress", (DbType)SqlDbType.NVarChar, 100, libraryplace.LibraryAddress, ParameterDirection.Input); 
    parms[2] = DbHelper.ParaInstance("@SchoolID", (DbType)SqlDbType.Int, 4, libraryplace.SchoolID, ParameterDirection.Input); 
    flag = DbHelper.ExecuteNonQuery(strSql.ToString(), CommandType.Text, DbHelper.GuanBar_Book_ConnString, parms); 
    return flag; 
    } 和直接在方法内声明有区别么/// <summary> 
    /// 增加库室 
    /// </summary> 
    /// <param name="libraryplace">库室表实体</param> 
    /// <returns></returns> 
    public bool AddLibraryPlace(LibraryPlace libraryplace) 

    StringBuilder strSql = new StringBuilder(100);
    strSql.Append("INSERT INTO [LibraryPlace]( "); 
    strSql.Append("[SchoolID],[LibraryName], [LibraryAddress] )"); 
    strSql.Append("VALUES ("); 
    strSql.Append("@SchoolID,@LibraryName, @LibraryAddress ) "); 
    DbParameter[] parms = new SqlParameter[3]; 
    parms[0] = DbHelper.ParaInstance("@LibraryName", (DbType)SqlDbType.NVarChar, 50, libraryplace.LibraryName, ParameterDirection.Input); 
    parms[1] = DbHelper.ParaInstance("@LibraryAddress", (DbType)SqlDbType.NVarChar, 100, libraryplace.LibraryAddress, ParameterDirection.Input); 
    parms[2] = DbHelper.ParaInstance("@SchoolID", (DbType)SqlDbType.Int, 4, libraryplace.SchoolID, ParameterDirection.Input); 
    flag = DbHelper.ExecuteNonQuery(strSql.ToString(), CommandType.Text, DbHelper.GuanBar_Book_ConnString, parms); 
    return flag;