2009/04/02

c# Replace字串

public static string Replease(string originalStr, string repleasStr, string newStr)
{
int pos = originalStr.IndexOf(repleasStr);
int len = repleasStr.Length;
if (pos != -1)
{
string strLeft = originalStr.Substring(0, pos);
string strRight = originalStr.Substring(pos + len);
return strLeft + newStr + strRight;

}
else
return originalStr;

}

c# 慣用的命名法


識別字種類習慣的命名原則範例
常數使用英文大寫字母和底線"_"符號MAX_SIZE, PI
變數使用英文小寫字母開頭,如果是2個英文字組成,第2個之後的英文字以大寫開頭size, userName
函數使用英文小寫字母開頭,甘果是2個英文組成,其化英文字使用大寫開頭pressButton, scrollScreen

c# 格式字元符號


格式字元符號說明
C使用貨幣格式顯示數值資料,例如:$123456
Dn使用十進位來顯示數值資料,之後的n是位數
E使用科學符號顯示數值資料
Fn使用指定小數部份的位數為n來顯示數值資料
G使用一般格式來顯示數值資料
N使用千分位來顯示數值資料,例如:987,654,321.0
X使用十六進位來顯示數值資料

c# 將string array轉成文字

public static string[] StringToLines(string inString)
{
string[] org = inString.Split('\n');
int count = org.Length - 1;
string[] outString = new string[count];

try
{
for (int i = 0; i < count; i++)
{
if (org[i].IndexOf('\r') == -1)
org[i] = org[i];
else
org[i] = org[i].Remove(org[i].IndexOf('\r'));
//int j = org[i].IndexOf('\r');

outString[i] = org[i];
}
}
catch
{
return null;
}

return outString;
}

c# 建立空的StreamReader

public static StreamReader GetEmptyStreamReader()
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.Write("");
sw.Flush();
StreamReader sr = new StreamReader(ms);
return sr;
}

c# 取得檔案列表

// Dir folder
public bool FileList(string folderName)
{
try
{
DirectoryInfo dirinfo = new DirectoryInfo(folderName);

FileInfo[] sortList = dirinfo.GetFiles();
string[] dirList = new string[sortList.Length];
string[] fileName = new string[sortList.Length];


for (int i = 0; i < sortList.Length; i++)
{
dirList[i] = sortList[i].Name.ToString();
fileName[i] = sortList[i].FullName.ToString();


}

FullFileName = fileName;
FileName = dirList;

return true;
}
catch (Exception ex)
{
_errorMessage = ex.ToString();
return false;
}

}

C# 存成文字檔

// Save file name
public static bool FileSaved(string fileName, string inputString)
{
try
{
System.IO.File.AppendAllText(fileName, inputString);
return true;
}
catch (Exception ex)
{
_errorMessage = ex.ToString();
return false;
}

}

C# 搬移檔案

// Move file
public bool FileMove(string orgFileName, string newFileName)
{
try
{
System.IO.File.Move(orgFileName, newFileName);
return true;
}
catch (Exception ex)
{
_errorMessage = ex.ToString();
return false;
}
}

C# 取得文字檔中的內容

// Get file content string
public string FileContent(string fileName)
{

StreamReader stmRdr = new StreamReader(fileName);
string line = stmRdr.ReadLine();
string sLine = null;

while (line != null)
{
if (line.Length != 0)
sLine = sLine + line + '\n';
line = stmRdr.ReadLine();
}
stmRdr.Close();
return sLine;
}

c# FTP的使用 -- 修改檔名(mv)

// Reanme file or remove file
public bool FtpRename()
{
if (this.InputValidation())
{
try
{
Uri UriFtpMove = new Uri("ftp://" + serverhost + ":" + serverport + dirpath + remotefile);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(UriFtpMove);
request.Method = WebRequestMethods.Ftp.Rename;
request.RenameTo = renamefile;
request.Credentials = new NetworkCredential(username, password);

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
returnmsg = response.BannerMessage;
returncode = response.StatusCode.ToString();

return true;
}
catch (Exception ex)
{
returnmsg = ex.ToString();
returncode = "0";
return false;
}
}
else
{
throw new ArgumentException(returnmsg);

}

}

c# FTP的使用 -- 取得FTP Server的檔案列表(ls)

// Get file list
public string[] FtpDir()
{
string Result = "";
if (this.InputValidation())
{
try
{
Uri UriFtpDir = new Uri("ftp://" + serverhost + ":" + serverport + dirpath + filter);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(UriFtpDir);
request.Method = WebRequestMethods.Ftp.ListDirectory;


request.Credentials = new NetworkCredential(username, password);

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

StreamReader MyStreamReader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
returnmsg = response.BannerMessage;
returncode = response.StatusCode.ToString();

Result = MyStreamReader.ReadToEnd();
}
catch (Exception ex)
{
returnmsg = ex.ToString();
returncode = "0";
throw new ArgumentException(returnmsg);
}

}
else
{
throw new ArgumentException(returnmsg);
}
return Strings.StringToLines(Result);
}

c# FTP的使用 -- 取得FTP Server的檔案(get)

// Get ftp file content
//public StreamReader FtpGet()
public bool FtpGet()
{
FileStream fileStream = null;
Stream responseStream = null;

if (this.InputValidation())
{
try
{
Uri UriFtpGet = new Uri("ftp://" + serverhost + ":" + serverport + dirpath + remotefile);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(UriFtpGet);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);


FtpWebResponse response = (FtpWebResponse)request.GetResponse();

responseStream = response.GetResponseStream();

returnmsg = response.BannerMessage;
returncode = response.StatusCode.ToString();

// Write into file
if (localfile == null)
localfile = Path.GetFileName(request.RequestUri.AbsolutePath);

fileStream = File.Create(localPath + @"\" + localfile);
byte[] buffer = new byte[1024];

int bytesRead;

while (true)
{

bytesRead = responseStream.Read(buffer, 0, buffer.Length);

if (bytesRead == 0)
break;

fileStream.Write(buffer, 0, bytesRead);

}
responseStream.Close();
fileStream.Close();

return true;

}
catch (Exception ex)
{
returnmsg = ex.ToString();
returncode = "0";
return false;
}
}
else
{
throw new ArgumentException(returnmsg);

}

}

2009/03/31

ora-12154 tns could not resolve the connect identifier specified

今天裝了二次Oracle Client,第一次失敗,第二次成功
接著我設定組態,也成功連結到資料庫

但在使用SQL/Plus以及 PL/SQL的時候出現:
ora-12154 tns could not resolve the connect identifier specified

上網搜尋後,也有很多人遇到相同的問題,我依方法進行確認
發現我環境變數path是:
d:\oracle11g\client_1\bin;d:\oracle11g\client_2\bin;...
修改成
d:\oracle11g\client_1;d:\oracle11g\client_1\bin;...

就可以使用SQL/Plus 和 PL/SQL 了