Sunday 13 May 2012

Db Connection mysql

----------------------------------------------------------
using (NpgsqlConnection npgSqlCon = new NpgsqlConnection(connectionString))
{
npgSqlCon.Open();
using (NpgsqlCommand npgsqlCmd = new NpgsqlCommand(query, npgSqlCon))
{
try
{
npgsqlCmd.Parameters.Add(new NpgsqlParameter("icu", NpgsqlDbType.Integer));
npgsqlCmd.Parameters[0].Value = 1;
npgsqlCmd.Prepare();
npgsqlCmd.ExecuteNonQuery();
}
--------------------------------------------------
public class FTPCLient
{

private string ftpIP, ftpLogin, ftpPAssword;

private FTPCLient() { }
public FTPCLient(string ip, string login, string pwd)
{

string _ip = "";
IPHostEntry Host = Dns.GetHostEntry(ip);
try
{
foreach (var addr in Host.AddressList)
{
if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
_ip = addr.ToString();
}
}
}
catch
{
_ip = "";
}
if (!string.IsNullOrEmpty(_ip))
ftpIP = _ip;
else
ftpIP = ip;
ftpLogin = login;
ftpPAssword = pwd;


}

public void deleteFromFTP(string targetPath)
{
var req = (FtpWebRequest)WebRequest.Create(ftpIP + "/" + targetPath);
req.Proxy = null;
req.Credentials = new NetworkCredential(ftpLogin, ftpPAssword);
req.UsePassive = true;
req.UseBinary = true;
req.KeepAlive = false;
req.Method = WebRequestMethods.Ftp.DeleteFile;
req.GetResponse().Close(); // Perform the deletion
}



public void UploadToFTP(string localPath, string targetPath)
{

string[] pathSep = targetPath.Split(new char[] { '/' });
StringBuilder stbNewPath = new StringBuilder();
foreach (string s in pathSep)
{
if (!s.Contains("."))
{
if (stbNewPath.Length < 0)
stbNewPath.Append(s);
else
{
stbNewPath.Append("/" + s);
}
}
}

if (stbNewPath.Length > 0)
{
if (!this.CheckDirectory(stbNewPath.ToString()))
{
MakeDirectory(stbNewPath.ToString());
}
}

string s1 = localPath;
byte[] releaseData = File.ReadAllBytes(s1);
FileStream h=File.OpenRead(localPath);

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpIP + "/" + targetPath);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(ftpLogin, ftpPAssword);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
Stream reqStream = request.GetRequestStream();
int uploadDataCount = releaseData.Length;

int beginOffset = 0;
while (uploadDataCount > 0)
{
if (uploadDataCount >= 2048)
reqStream.Write(releaseData, beginOffset, 2048);
else
reqStream.Write(releaseData, beginOffset, uploadDataCount);
beginOffset += 2048;
uploadDataCount -= 2048;
}
reqStream.Close();
}


public void UploadToFTPString(string data, string targetPath)
{

string[] pathSep = targetPath.Split(new char[] { '/' });
StringBuilder stbNewPath = new StringBuilder();
foreach (string s in pathSep)
{
if (!s.Contains("."))
{
if (stbNewPath.Length < 0)
stbNewPath.Append(s);
else
{
stbNewPath.Append("/" + s);
}
}
}

if (stbNewPath.Length > 0)
{
if (!this.CheckDirectory(stbNewPath.ToString()))
{
MakeDirectory(stbNewPath.ToString());
}
}

Byte[] releaseData = File.ReadAllBytes(data);// ms.ToArray();


FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://" + ftpIP + "/" + Uri.EscapeDataString( targetPath ));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(ftpLogin, ftpPAssword);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
using (Stream reqStream = request.GetRequestStream())
{
int uploadDataCount = releaseData.Length;

int beginOffset = 0;
while (uploadDataCount > 0)
{
if (uploadDataCount >= 2048)
reqStream.Write(releaseData, beginOffset, 2048);
else
reqStream.Write(releaseData, beginOffset, uploadDataCount);
beginOffset += 2048;
uploadDataCount -= 2048;
}
reqStream.Close();
}
}


public void DownloadFromFTP(string localPath, string targetPath)
{

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpIP + "/" + targetPath);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(ftpLogin, ftpPAssword);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream ftpStream = response.GetResponseStream();

long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
MemoryStream memory = new MemoryStream();
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
memory.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}

ftpStream.Close();
memory.Close();
response.Close();
byte[] filedata = memory.ToArray();

File.WriteAllBytes(localPath, filedata);
}



private bool CheckDirectory(string dirName)
{

try
{
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(string.Format("ftp://{0}/{1}", ftpIP, dirName));
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(ftpLogin, ftpPAssword);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
return true;
}
}
catch (WebException ex)
{
if (ex.Response != null)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
return false;
}
}
}
return false;
}



public bool MakeDirectory(string dirName)
{

try
{
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(string.Format("ftp://{0}/{1}", ftpIP, dirName));
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential(ftpLogin, ftpPAssword);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
return true;
}
}
catch (WebException ex)
{
if (ex.Response != null)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
return false;
}
}
}
return false;
}
}
}
-------------------------------------------------
public void NewFTPFolder(string newSiteName)
{
try
{
string ftpaddress = "";
string ftpusername = "";
string ftppwd = "";
FTPCLient ftpClient = new FTPCLient(ftpaddress,ftpusername,ftppwd);
string folderPath = "hello";
DirectoryInfo srcDirectoryInfo = new DirectoryInfofolderPath
string ftppth = "/";
CopyFilesToFTP(srcDirectoryInfo, ftppth, ftpClient);
}
catch (Exception error)
{
Log.Error(error.Message);
}
}

--------------------------------
public void CopyFilesToFTP(DirectoryInfo srcDirectoryInfo, string ftppath, FTPCLient ftpClient)
{
try
{
foreach (FileInfo fi in srcDirectoryInfo.GetFiles())
{
string targetFtpPath = ftppath + "/" + fi.Name;
ftpClient.UploadToFTPString(fi.FullName, targetFtpPath);
}
foreach (DirectoryInfo di in srcDirectoryInfo.GetDirectories())
{
string subdirftppath = ftppath + "/" + di.Name;
ftpClient.MakeDirectory(subdirftppath);
CopyFilesToFTP(di, subdirftppath, ftpClient);
}
}
catch (Exception error)
{
Log.Error(error.Message);
}
}
}
----------------------------------------------

No comments:

Post a Comment