August 2005 Archives

拷贝的SQL Server 7的恢复方法

  在SQL Server 7中由于MS重新设计了数据库文件的存储方式,取消了新建设备再建数据库这一繁琐的过程。新的存储格式,一个数据库包括两个文件,mdf数据库文件和ldf日志文件。所以我们在重装机器备份时可以


把你要备份的数据库的这两个文件拷贝出来,重新安装之后再恢复。

  在SQL Server中提供了这种恢复方式的存储过程。 

  1.sp_attach_db [@dbname =] dbname,[@filename1 =] filename_n

  给系统添加一个数据库,在dbname指定数据库名称,filename_n指定数据库的文件和日志文件。比如我有一个voogiya的库,停止SQL Server服务备份voogiya_data.mdf,voogiya_log.ldf,启动SQL server,删除掉这个库,然后再把这两上文件拷到sql server DATA目录中,在Query Analyzer中执行如下语句:

  EXEC sp_attach_db @dbname = Nvoogiya,
  @filename1 = Nd:\mssql7\data\voogiya_data.mdf,
  @filename2 = Nd:\mssql7\data\voogiya_log.ldf

  就会把这个库加入到SQL Server Group中.

  2.sp_attach_single_file_db [@dbname =] dbname,
  [@physname =] physical_name

  这个命令和上面的功能一样,在physical_name中只要写上据库的物理文件名就可以了,日志文件SQL server会重新建立。这个存储过程的运行要先执行下面的存储过程:

  sp_detach_db @dbname = dbname

  同样以上面的为例:

  EXEC sp_detach_db @dbname = voogiya
  EXEC sp_attach_single_file_db @dbname = voogiya,
  @physname = d:\mssql7\data\voogiya_data.mdf

  要注意执行以上存储过程的用户要在sysadmin中.

  以上方法在windows Nt 4.0,service pack5,sql server 7.0上运行通过。

  任何数据库系统都无法避免崩溃的状况,即使你使用了Clustered,双机热备……仍然无法完全根除系统中的单点故障,何况对于大部分用户来说,无法承受这样昂贵的硬件投资。所以,在系统崩溃的时候,如何恢复原有的宝贵数据就成为一个极其重要的问题了。

  在恢复的时候,最理想的情况就是你的数据文件和日志文件都完好无损了,这样只需要sp_attach_db,把数据文件附加到新的数据库上即可,或者在停机的时候把所有数据文件(一定要有master等)都copy到原有路径下也行,不过一般不推荐这样的做法,sp_attach_db比较好,虽然麻烦许多。

  但是呢,一般数据库崩溃的时候系统是未必能有时间把未完成的事务和脏页等写入磁盘的,这样的情况sp_attach_db就会失败。那么,寄期望于DBA制定了一个良好的灾难恢复计划吧。按照你的恢复计划,还原最新的完全备份,增量备份或者事务日志备份,然后如果你的活动事务日志还能读得出来的话,恭喜你!你可以还原到崩溃前的状态。

   一般的单位都是没有专职的DBA的,如果没有可用的备份,更可能是最近一次备份的时间过于久远而导致不可接受的数据损失,而且你的活动事务日志也处于不可用的状态,那就是最麻烦的情况了。

  不幸的很的是,一般数据库崩溃都是由于存储子系统引起的,而这样的情况是几乎不可能有可用的日志用于恢复的。

  那么就只好试一下这些方案了。当然,是要求至少你的数据文件是存在的,要是数据文件、日志文件和备份都没有了的话,别找我,你可以到楼顶上去唱“神啊,救救我吧”。

  首先,你可以试一下sp_attach_single_file_db,试着恢复一下你的数据文件,虽然能恢复的可能性不大,不过假如这个数据库刚好执行了一个checkpoint的话,还是有可能成功的。

  如果你没有好到有摸彩票的手气,最重要的数据库没有像你期盼的那样attach上去,不要气馁,还是有别的方案的。

  我们可以试着重新建立一个log,先把数据库设置为emergency mode,sysdatabases的status为32768 就表示数据库处于此状态。


  不过系统表是不能随便改的,设置一下先
  Use Master
  Go
  sp_configure 'allow updates', 1
  reconfigure with override
  Go
  然后
  update sysdatabases set status = 32768 where name = ''

  现在,祈求满天神佛的保佑吧,重新建立一个log文件。成功的机会还是相当大的,系统一般都会认可你新建立的日志。如果没有报告什么错误,现在就可以松一口气了。

  虽然数据是恢复了,可是别以为事情就算完成了,正在进行的事务肯定是丢失了,原来的数据也可能受到一些损坏。

  先把SQL Server 重新启动一下,然后检查你的数据库吧。
  先设置成单用户模式,然后做dbcc
  sp_dboption '', 'single user', 'true'
  DBCC CHECKDB('')

  如果没有什么大问题就可以把数据库状态改回去了,记得别忘了把系统表的修改选项关掉。
update sysdatabases set status = 28 where name = '' --当然你的数据库状态可能不是这个,自己改为合适的值吧。也可以用sp_resetstatus
  go
  sp_configure 'allow updates', 0
  reconfigure with override
  Go

  checkdb的时候可能报告有一些错误,这些错误的数据你可能就只好丢弃了。

  checkdb有几种修复选项,自己看着用吧,不过最后你可能还是得REPAIR_ALLOW_DATA_LOSS,完成所有修复。

  chekcdb并不能完成所有的修复,我们需要更进一步的修复,用DBCC CHECKTABLE对每一个表做检查吧。

  表的列表可以用sysobjects里面得到,把OBJECTPROPERTY是IsTable的全部找出来检查一下吧,这样能够基本上解决问题了,如果还报告错误,试着把数据select into到另一张表检查一下。

  这些都做完了之后,把所有索引、视图、存储过程、触发器等重新建立一下。DBCC DBREINDEX也许可以帮你一些忙。

  上面提到的命令、对象在Books Online中均有详细说明,请注意参看。

1 部分:安装 Linux







本指南安装至少具有以下硬件配置的服务器:








  • 两个 800MHz Pentium III CPU


  • 512MB RAM


  • 两个 SCSI 主机适配器 (Ultra SCSI 160)


  • 八个 SCSI 磁盘驱动器 (2 x 9GB + 6 x 36GB)


  • 一个 100Base-T 以太网适配器


  • 一个 1000Base-T 以太网适配器


对磁盘进行配置,使每个 SCSI 主机适配器连接到一个 9GB 磁盘和三个 36GB 磁盘。







请注意,尽管这根本算不上是一个强大的设置,但即便是使用再低的设置也可以完成本指南介绍的大部分任务。基本数据库安装只需要一个 CPU512MB 内存和一个可用空间至少为 6.5GB 的磁盘驱动器(IDESCSI FireWire)。







现在,我们将逐步演示在服务器上安装 Linux 操作系统的过程。本文假设进行 Linux 系统全新安装(有别于升级),并假设服务器为 Oracle 所专用,且服务器上没有其他操作系统或数据。







Red Hat Enterprise Linux 4







Oracle 10g 经认证可以在不需要更新的情况下运行 Red Hat Enterprise Linux 4Advanced Server Enterprise Server)的基本版本。如果拥有更新 CD,则可以使用更新版本中的启动 CD 而非基本版本中的启动 CD 在安装过程中自动应用所有更新。Oracle 支持 Red Hat 的所有更新。







1.       使用第一张 CD 启动服务器。







o        您可能需要更改 BIOS 设置,以允许从 CD 启动。







2.       启动屏幕上出现时在底部显示 boot:







o        选择 Enter,从控制台上执行图形安装。(对于其他安装方法和选项,请参阅 Red Hat 安装指南。)







o        安装程序扫描硬件,短暂显示 Red Hat 闪屏,然后开始显示一系列屏幕提示。







3.       选择语言







o        接受默认值。







4.       配置键盘







o        接受默认值。







5.       欢迎屏幕







o        单击 Next







6.       配置鼠标







o        接受默认值。







7.       安装类型







o        选择 Custom







8.       设置磁盘分区







o        本文不介绍磁盘分区的详细方法,而是假设您熟悉磁盘分区方法。







(警告:对磁盘进行错误分区是删除硬盘上所有内容的最可靠、最快捷的方法之一。如果不确定如何分区,请先停下来,找人帮帮您,否则您将冒丢失数据的危险!)







本文使用以下分区方案(文件系统均为 ext3):

第一个控制器 (/dev/sda) 上的 9GB 磁盘包含以下分区,用于存放所有 Linux Oracle 软件:
- 100MB /boot
分区
-1,500MB
交换分区将此分区大小至少设置为系统 RAM 的两倍,但不要超过 2GB32 位系统不支持大于 2GB 的交换文件)。如果需要大于 2GB 的交换空间,则创建多个交换分区。
-7,150MB
根分区该分区将用于所有目录,包括 /usr/tmp/var/opt/home 等。这样做纯粹是为了讲解本指南而简化安装。更可靠的分区方案是将这些目录划分到单独的文件系统。

 

要序列化的对象的类:


[Serializable]
public class Person
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name=value;
}
}
public string Sex;
public int Age=31;
public Course[] Courses;

public Person()
{
}
public Person(string Name)
{
name=Name;
Sex="男";
}
}
[Serializable]
public class Course
{
public string Name;
[XmlIgnore]public string Description;
public Course()
{
}
public Course(string name,string description)
{
Name=name;
Description=description;
}


进行序列化及反序列化的测试类:


class Test
{
//序列化
public void Serialiaze()
{
Person c=new Person("cyj")
c.Courses=new Course[2];
c.Courses[0]=new Course("英语","交流工具")
c.Courses[1]=new Course("数学","自然科学")

XmlSerializer xs=new XmlSerializer(typeof(Person));
Stream stream = new FileStream("c:\\cyj.xml", FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
xs.Serialize(stream, c);
stream.Close();
}
//反序列化
public void Deserialize()
{
XmlSerializer xs=new XmlSerializer(typeof(Person));
Stream stream = new FileStream("c:\\cyj.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Person p=(Person)xs.Deserialize(stream);
Console.WriteLine(p.Name);
Console.WriteLine(p.Age.ToString());
Console.WriteLine(p.Courses.Length.ToString());
Console.Read();
}
}


格式化后Xml的文档内容为:
<?xml version="1.0"?>
<Person xmlns:xsd=http://www.w3.org/2001/XMLSchema    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <Sex>男</Sex>
  <Age>31</Age>
  <Courses>
    <Course>
      <Name>英语</Name>
    </Course>
    <Course>
      <Name>数学</Name>
    </Course>
  </Courses>
  <Name>cyj</Name>
</Person>



本文从两个方面讲述.NET中自定义光标的实现,部分是参考“孟子”前辈的资料,以示说明。 


    首先要明白一个知识点:光标的分类
    光标分为两大类,一是静态光标(*.cur),一是动态光标(*.ani)。这两类光标又有彩色和单像素之分。一般常见的静态光标多数是单像素的,.NET中可以直接支持这种光标。而对于彩色或动态光标则是有256色甚至更高像素组成的动画光标,在.NET中若要支持动态光标或彩色光标需要调用Win32 API实现。

    1:在.NET中利用资源文件实现自定义静态光标(以下为MSDN原文)


            // The following generates a cursor from an embedded resource.
            
     // To add a custom cursor, create or use an existing 16x16 bitmap
     //        1. Add a new cursor file to your project: 
     //                File->Add New Item->Local Project Items->Cursor File
     //        2. Select 16x16 image type:
     //                Image->Current Icon Image Types->16x16


     // --- To make the custom cursor an embedded resource  ---
            
     // In Visual Studio:
     //        1. Select the cursor file in the Solution Explorer
     //        2. Choose View->Properties.
     //        3. In the properties window switch "Build Action" to "Embedded"


     // On the command line:
     //        Add the following flag:
     //            /res:CursorFileName.Cur,Namespace.CursorFileName.Cur
     //        
     //        Where "Namespace" is the namespace in which you want to use the cursor
     //        and   "CursorFileName.Cur" is the cursor filename.


     // The following line uses the namespace from the passed-in type
     // and looks for CustomCursor.MyCursor.Cur in the assemblies manifest.
     // NOTE: The cursor name is acase sensitive.
     this.Cursor = new Cursor(GetType(), "MyCursor.Cur");


    此处需要注意的是在调用的时候,MyCursor.Cur为资源文件的名称,大小写区分。如下图所示:
  
    则资源文件名称应该为Cursor.block.cur,以命名空间的形式来访问资源。


    2:.NET中实现自定义彩色光标和动态光标
    以下为引用win32 api函数,用于创建自定义光标、设置光标等操作。


   [DllImport("user32.dll")]
   public static extern IntPtr LoadCursorFromFile( string fileName );
  
   [DllImport("user32.dll")]
   public static extern IntPtr SetCursor( IntPtr cursorHandle );
  
   [DllImport("user32.dll")]
   public static extern uint DestroyCursor( IntPtr cursorHandle );


    调用自定义光标则可以如下操作:
    Cursor myCursor = new Cursor(Cursor.Current.Handle);
  //dinosau2.ani为windows自带的光标:
  IntPtr colorCursorHandle = LoadCursorFromFil(@"C:\WINNT\Cursors\dinosau2.ani" );
  myCursor.GetType().InvokeMember("handle",BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetField,null,myCursor, new object [] { colorCursorHandle } );
  this.Cursor = myCursor;


    以上方式就是设定自定义光标的实现。当然不管是静态光标还是动态光标都需要自己设计之后方可引用。



 


C# WinForm编程中的一点小收获

 

一:Win Form登录机制的实现
    Main窗体为应用程式主窗体,Login为登录窗体。均为SDI窗体。
    两种实现方式如下:
        1、应用程式入口放在Login窗体,在Login窗体实现登录机制,验证通过则创建Main窗体的实例,并将自身隐藏。
        具体实现:
        ///Step1:验证登录
        ///Step2:通过
            this.hide(); 
            oMain.Show();
        虽然可以实现登录机制,但是Login窗体并没有释放掉,而是被隐藏掉,内存资源未有效利用。这种方式其实是不可取的。
        2、应用程式入口放在Main窗体,在Main函数中创建Login窗体的实例,Login窗体完成登录验证,返回Main窗体,程式继续执行。需要注意的是Login窗体只有验证通过时返回值才为DialogResult.OK,其余时返回DialogResult.None。这样在Main窗体就可以根据返回值判断是否创建Main窗体的实例。
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>

        [STAThread]
        static void Main()
        {
            frmLogin login = new frmLogin();
            login.ShowDialog();
            if(login.DialogResult.Equals(DialogResult.OK))
            {
                login.Close();
                Application.Run(new frmMain());
            }
        }  
二:利用ImageList作为Resource的载体
    Win Form的程式,外部文件是比较烦人的事情,这里采用Resource将外部图片文件加载到程式中。而ImageList是一个不错的选择,使用简单。
    具体实现:
        ///Step1:将图片在可是模式下加载到ImageList中。
        ///Step2:程式中可以采用this.imgLstResource.Images[index]的方式获取其中的Image对象。



.NET下INI配置文件操作类

 

using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;


namespace INIManage
{
 /// <summary>
 /// INIManage 的摘要说明
 /// 在http://www.allapi.net/ 上发现了一个VB.NET的INI文件操作类,下载了看了看,顺手改成了C#版的
 /// 你可以把它编译成dll在winform或webform中引用,也可以直接把代码拷到项目中使用
 /// 我没有进行逐项测试,所以可能有不对的地方,请酌情修改
 /// --------------丛兴滋(cncxz) 2005-08-23
 /// </summary>
 public class INIManage
 {


  #region" 引入相关dll "


  [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileIntA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
  private static extern int GetPrivateProfileInt(string lpApplicationName, string lpKeyName, int nDefault, string lpFileName);


  [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileSectionsNamesA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
  private static extern int GetPrivateProfileSectionsNames(byte[] lpszReturnBuffer, int nSize, string lpFileName);


  [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileStringA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
  private static extern int GetPrivateProfileString(string lpApplicationName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);


  [DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileStructA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
  private static extern int GetPrivateProfileStruct(string lpszSections, string lpszKey, byte[] lpStruct, int uSizeStruct, string szFile);
 
  [DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileSectionsA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
  private static extern int WritePrivateProfileSections(string lpAppName, string lpString, string lpFileName);


  [DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileStringA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
  private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);


  [DllImport("KERNEL32.DLL", EntryPoint="WritePrivateProfileStructA", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Ansi, ExactSpelling=true)]
  private static extern int WritePrivateProfileStruct(string lpszSections, string lpszKey, byte[] lpStruct, int uSizeStruct, string szFile);



  #endregion



  private string _Filename;    //INI文件名
  private string _Sections;     //INI文件中配置参数的组别片段
  private const int MAX_ENTRY = 32768; //最大字符数



  public INIManage(string strFile)
  {
   this.Filename = strFile;
  }


  #region" INI操作类的属性 "


  public string Filename
  {
   get
   {
    return this._Filename;
   }
   set
   {
    this._Filename = value;
   }
  }



  public string Sections
  {
   get
   {
    return this._Sections;
   }
   set
   {
    this._Sections = value;
   }
  }



  #endregion



  #region" INI操作类的Read相关方法 "
 
  // Read相关方法中的 DefaultValue是 在INI文件中找不到相关配置 时的返回值
  //ReadBoolean是读取bool类型的配置参数,ReadByteArray是读取 byte[]类型的配置参数
  //ReadInteger是读取int类型的配置参数。。。。依次类推


  public bool ReadBoolean(string Key)
  {
   return this.ReadBoolean(this.Sections, Key);
  }


  public bool ReadBoolean(string Key, bool DefaultValue)
  {
   return this.ReadBoolean(this.Sections, Key, DefaultValue);
  }


  public bool ReadBoolean(string Sections, string Key)
  {
   return this.ReadBoolean(Sections, Key, false);
  }


  public bool ReadBoolean(string Sections, string Key, bool DefaultValue)
  {
   return bool.Parse(this.ReadString(Sections, Key, DefaultValue.ToString()));
  }


  public byte[] ReadByteArray(string Key, int Length)
  {
   return this.ReadByteArray(this.Sections, Key, Length);
  }


  public byte[] ReadByteArray(string Sections, string Key, int Length)
  {
   byte[] buffer1;
   if (Length > 0)
   {
    try
    {
     byte[] buffer2 = new byte[(Length - 1) + 1];
     if (INIManage.GetPrivateProfileStruct(Sections, Key, buffer2, buffer2.Length, this.Filename) == 0)
     {
      return null;
     }
     return buffer2;
    }
    catch (Exception exception1)
    {
     ProjectData.SetProjectError(exception1);
     buffer1 = null;
     ProjectData.ClearProjectError();
     return buffer1;               
    }
   }
   else
   {
    return null;
   }
       
  }



  public int ReadInteger(string Key)
  {
   return this.ReadInteger(Key, 0);
  }


  public int ReadInteger(string Key, int DefaultValue)
  {
   return this.ReadInteger(this.Sections, Key, DefaultValue);
  }


  public int ReadInteger(string Sections, string Key)
  {
   return this.ReadInteger(Sections, Key, 0);
  }


  public int ReadInteger(string Sections, string Key, int DefaultValue)
  {
   int num1;
   try
   {
    num1 = INIManage.GetPrivateProfileInt(Sections, Key, DefaultValue, this.Filename);
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    num1 = DefaultValue;
    ProjectData.ClearProjectError();
    return num1;           
   }
   return num1;
  }


  public long ReadLong(string Key)
  {
   return this.ReadLong(Key, (long) 0);
  }


  public long ReadLong(string Key, long DefaultValue)
  {
   return this.ReadLong(this.Sections, Key, DefaultValue);
  }


  public long ReadLong(string Sections, string Key)
  {
   return this.ReadLong(Sections, Key, 0);
  }


  public long ReadLong(string Sections, string Key, long DefaultValue)
  {
   return long.Parse(this.ReadString(Sections, Key, DefaultValue.ToString()));
  }


  public string ReadString(string Key)
  {
   return this.ReadString(this.Sections, Key);
  }


  public string ReadString(string Sections, string Key)
  {
   return this.ReadString(Sections, Key, "");
  }


  public string ReadString(string Sections, string Key, string DefaultValue)
  {
   string text1;
   try
   {
    StringBuilder builder1 = new StringBuilder(MAX_ENTRY);
    int num1 = INIManage.GetPrivateProfileString(Sections, Key, DefaultValue, builder1, MAX_ENTRY, this.Filename);
    text1 = builder1.ToString();
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    text1 = DefaultValue;
    ProjectData.ClearProjectError();
    return text1;
         
   }
   return text1;
  }


  #endregion



  #region" INI操作类的Write相关方法 "
 
  public bool Write(string Key, bool Value)
  {
   return this.Write(this.Sections, Key, Value);
  }


  public bool Write(string Key, byte[] Value)
  {
   return this.Write(this.Sections, Key, Value);
  }


  public bool Write(string Key, string Value)
  {
   return this.Write(this.Sections, Key, Value);
  }


  public bool Write(string Key, int Value)
  {
   return this.Write(this.Sections, Key, Value);
  }


  public bool Write(string Key, long Value)
  {
   return this.Write(this.Sections, Key, Value);
  }


  public bool Write(string Sections, string Key, byte[] Value)
  {
   bool flag1;
   try
   {
    flag1 = INIManage.WritePrivateProfileStruct(Sections, Key, Value, Value.Length, this.Filename) != 0;
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    flag1 = false;
    ProjectData.ClearProjectError();
    return flag1;          
   }
   return flag1;
  }


  public bool Write(string Sections, string Key, bool Value)
  {
   return this.Write(Sections, Key, Value.ToString());
  }


  public bool Write(string Sections, string Key, int Value)
  {
   bool flag1;
   try
   {
    flag1 = INIManage.WritePrivateProfileString(Sections, Key, Value.ToString(), this.Filename) != 0;
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    flag1 = false;
    ProjectData.ClearProjectError();
    return flag1;          
   }
   return flag1;
  }


  public bool Write(string Sections, string Key, long Value)
  {
   return this.Write(Sections, Key, Value.ToString());
  }


  public bool Write(string Sections, string Key, string Value)
  {
   bool flag1;
   try
   {
    flag1 = INIManage.WritePrivateProfileString(Sections, Key, Value, this.Filename) != 0;
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    flag1 = false;
    ProjectData.ClearProjectError();
    return flag1;          
   }
   return flag1;
  }


  #endregion



  #region" INI操作类的Delete相关方法 "


  public bool DeleteKey(string Key)
  {
   bool flag1;
   try
   {
    flag1 = INIManage.WritePrivateProfileString(this.Sections, Key, null, this.Filename) != 0;
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    flag1 = false;
    ProjectData.ClearProjectError();
    return flag1;           
   }
   return flag1;
  }


  public bool DeleteKey(string Section, string Key)
  {
   bool flag1;
   try
   {
    flag1 = INIManage.WritePrivateProfileString(Sections, Key, null, this.Filename) != 0;
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    flag1 = false;
    ProjectData.ClearProjectError();
    return flag1;          
   }
   return flag1;
  }


  public bool DeleteSections(string Section)
  {
   bool flag1;
   try
   {
    flag1 = INIManage.WritePrivateProfileSections(Sections, null, this.Filename) != 0;
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    flag1 = false;
    ProjectData.ClearProjectError();
    return flag1;          
   }
   return flag1;
  }


  #endregion



  public ArrayList GetSectionsNames()
  {
   int num1;
   ArrayList list1 = new ArrayList();
   byte[] buffer1 = new byte[MAX_ENTRY];
   int num2 = 0;
   try
   {
    num1 = INIManage.GetPrivateProfileSectionsNames(buffer1, MAX_ENTRY, this.Filename);
   }
   catch (Exception exception1)
   {
    ProjectData.SetProjectError(exception1);
    ProjectData.ClearProjectError();
    return list1;         
   }
   ASCIIEncoding encoding1 = new ASCIIEncoding();
   if (num1 > 0)
   {
    string text1 = encoding1.GetString(buffer1);
    num1 = 0;
    num2 = -1;
    while (true)
    {
     num1 = text1.IndexOf('\0', (int) (num2 + 1));
     if (((num1 - num2) == 1) || (num1 == -1))
     {
      return list1;
     }
     try
     {
      list1.Add(text1.Substring(num2 + 1, num1 - num2));
     }
     catch (Exception exception2)
     {
      ProjectData.SetProjectError(exception2);
      ProjectData.ClearProjectError();
     }
     num2 = num1;
    }
   }
   return list1;
  }



 }
}

真正的Java学习从入门到精通

| 3 Comments

一、 工具篇JDK (Java Development Kit)
JDK是整个Java的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工具和Java基础的类库(rt.jar)。不论什么Java应用服务器实质都是内置了某个版本的JDK。因此掌握JDK是学好Java的第一步。最主流的JDK是Sun公司发布的JDK,除了Sun之外,还有很多公司和组织都开发了自己的JDK,例如IBM公司开发的JDK,BEA公司的Jrocket,还有GNU组织开发的JDK等等。其中IBM的JDK包含的JVM(Java Virtual Machine)运行效率要比Sun JDK包含的JVM高出许多。而专门运行在x86平台的Jrocket在服务端运行效率也要比Sun JDK好很多。但不管怎么说,我们还是需要先把Sun JDK掌握好。
1、 JDK的下载和安装

JDK又叫做J2SE(Java2 SDK Standard Edition),可以从Sun的Java网站上下载到,http://java.sun.com/j2se/downloads.html ,JDK当前最新的版本是J2SDK1.4.2,建议下载该版本的JDK,下载页面在这里:http://java.sun.com/j2se/1.4.2/download.html。下载好的JDK是一个可执行安装程序,默认安装完毕后会在C:\Program Files\Java\目录下安装一套JRE(供浏览器来使用),在C:\j2sdk1.4.2下安装一套JDK(也包括一套JRE)。然后我们需要在环境变量PATH的最前面增加java的路径C:\j2sdk1.4.2\bin。这样JDK就安装好了。

2、 JDK的命令工具

JDK的最重要命令行工具:


java: 启动JVM执行class
javac: Java编译器
jar: Java打包工具
javadoc: Java文档生成器
这些命令行必须要非常非常熟悉,对于每个参数都要很精通才行。对于这些命令的学习,JDK Documentation上有详细的文档。

二、 JDK Documentation

Documentation在JDK的下载页面也有下载连接,建议同时下载Documentation。Documentation是最最重要的编程手册,涵盖了整个Java所有方面的内容的描述。可以这样说,学习Java编程,大部分时间都是花在看这个Documentation上面的。我是随身携带的,写Java代码的时候,随时查看,须臾不离手。

三、 应用服务器(App Server)

App Server是运行Java企业组件的平台,构成了应用软件的主要运行环境。当前主流的App Server是BEA公司的Weblogic Server和IBM公司的Websphere以及免费的Jboss,选择其中一个进行学习就可以了,个人推荐Weblogic,因为它的体系结构更加干净,开发和部署更加方便,是Java企业软件开发人员首选的开发平台。下面简要介绍几种常用的App Server:

1、 Tomcat

Tomcat严格意义上并不是一个真正的App Server,它只是一个可以支持运行Serlvet/JSP的Web容器,不过Tomcat也扩展了一些App Server的功能,如JNDI,数据库连接池,用户事务处理等等。Tomcat被非常广泛的应用在中小规模的Java Web应用中,因此本文做一点下载、安装和配置Tomcat的介绍:

Tomcat是Apache组织下Jakarta项目下的一个子项目,它的主网站是:http://jakarta.apache.org/tomcat/ ,Tomcat最新版本是Tomcat4.1.27,软件下载的连接是:http://www.apache.org/dist/jakarta/tomcat-4/binaries/ 。

下载Tomcat既可以直接下载zip包,也可以下载exe安装包(个人建议zip更干净些),不管哪种情况,下载完毕安装好以后(zip直接解压缩就可以了)。需要设置两个环境变量:


JAVA_HOME=C:\j2sdk1.4.2
CATALINA_HOME=D:\tomcat4 (你的Tomcat安装目录)

这样就安装好了,启动Tomcat运行CATALINA_HOME\bin\startup.bat,关闭Tomcat运行shutdown.bat脚本。Tomcat启动以后,默认使用8080端口,因此可以用浏览器访问http://localhost:8080来测试Tomcat是否正常启动。

Tomcat提供了两个Web界面的管理工具,URL分别是:

http://localhost:8080/admin/index.jsp

http://localhost:8080/manager/html

在启用这两个管理工具之前,先需要手工配置一下管理员用户和口令。用一个文本工具打开CATALINA_HOME\conf\tomcat-users.xml这个文件,加入如下几行:
<role rolename="manager"/>
<role rolename="admin"/>
<user username="robbin" password="12345678" roles="admin,manager,tomcat"/>

这样用户“robbin”就具备了超级管理员权限。重新启动Tomcat以后,你就可以使用该用户来登陆如上的两个管理工具,通过Web方式进行Tomcat的配置和管理了。

2、 BEA Weblogic

Weblogic可以到BEA的网站上免费注册之后下载到最新的Weblogic8.1企业版,License可以免费使用1年时间,其实这已经完全足够了。Weblogic的下载连接:http://commerce.bea.com/index.jsp,.../edocs.bea.com/ 。

3、 IBM Webshpere

Websphere同样可以下载到免费的试用版本,到IBM的developerWorks网站可以看到Websphere试用产品的下载和相关的Websphere的资料,developerWorks中文网站的连接是:http://www-900.ibm.com/developerWorks/cn/wsdd/ ,Websphere的下载连接:http://www7b.software.ibm.com/wsdd/...WASsupport.html 。

4、 Jboss

Jboss是免费开源的App Server,可以免费的从Jboss网站下载:http://www.jboss.org/index.html,然...n.com/idea.html

1.


abstract class Name {


    private String name;


    public abstract boolean isStupidName(String name) {}


}


 


大侠们,这有何错误?


答案: 错。abstract method必须以分号结尾,且不带花括号。


 


2.


public class Something {


    void doSomething () {


        private String s = "";


        int l = s.length();


    }


}


 


有错吗?


答案: 错。局部变量前不能放置任何访问修饰符 (private,public,和protected)。final可以用来修饰局部变量 (final如同abstract和strictfp,都是非访问修饰符,strictfp只能修饰class和method而非variable)。


 


3.


abstract class Something {


    private abstract String doSomething ();


}


 


这好像没什么错吧?


答案: 错。abstract的methods不能以private修饰。abstract的methods就是让子类implement(实现)具体细节的,怎么可以用private把abstract method封锁起来呢? (同理,abstract method前不能加final)。


 


4.


public class Something {


    public int addOne(final int x) {


        return ++x;


    }


}


 


这个比较明显。


答案: 错。int x被修饰成final,意味着x不能在addOne method中被修改。


 


5.


public class Something {


    public static void main(String[] args) {


        Other o = new Other();


        new Something().addOne(o);


    }


   


    public void addOne(final Other o) {


        o.i++;


    }


}


 


class Other {


    public int i;


}


 


和上面的很相似,都是关于final的问题,这有错吗?


答案: 正确。在addOne method中,参数o被修饰成final。如果在addOne method里我们修改了o的reference (比如: o = new Other();),那么如同上例这题也是错的。但这里修改的是o的member vairable (成员变量),而o的reference并没有改变。


 

读取文本文件到数据库

| 1 Comment

--**************************************
-- Name: Read a text file into SQL SERVER
--
-- Description:Using BULK INSERT TO READ
--             A TEXT FILE into SQL SERVER and ELIMINATING
--             THE USE of another text editor.
-- By: Eli Leiba
--
-- Inputs:@filename sysname (filename includes path)
--
-- Returns:a recordset of the file lines in QUERY analyzer
--
-- Assumes:Usage :
   EXEC sp_readTextFile 'c:\autoexec.bat'
--
--This code is copyrighted and has limited warranties.
--Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=359&lngWId=5
--for details.
--**************************************
--    


CREATE PROC sp_readTextFile @filename sysname
as
     BEGIN
     SET nocount ON
     CREATE TABLE #tempfile (line varchar(8000))
     EXEC ('bulk INSERT #tempfile FROM "' + @filename + '"')
     SELECT * FROM #tempfile
     DROP TABLE #tempfile
 END
 GO


//////////////////////////////////////////////////////////////////////////



/// 程序:屏幕取色



/// 功能:动态获取当前屏幕中光标所在位置的颜色



/// 作者:黎波



/// 网名:upto(阿球)



/// 邮箱:itfun@163.com



/// 日期:2004331



//////////////////////////////////////////////////////////////////////////



 

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Drawing.Imaging;

using System.Runtime.InteropServices;

 

namespace LiBo.ColorPicker

{

    /// <summary>

    /// Form1 的摘要说明。

    /// </summary>

    public class Form1 : System.Windows.Forms.Form

    {

        // 桌面工作区的尺寸

        Size workingArea;

        // Form 的初始位置和在左下角,右下角的位置

        Point formLoc, ptLeftBottom, ptRightBottom;

 

        private System.Windows.Forms.Label lblColor;

        private System.Windows.Forms.TextBox txtArgb;

        private System.Windows.Forms.Timer tmr;

        private System.Windows.Forms.ToolTip tip;

        private System.ComponentModel.IContainer components;

 

        public Form1()

        {

            InitializeComponent();

 

            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;

            this.StartPosition = Form