Creating a database with filestream using (C#) SMO in DotNet 3.5.

The following code will help you to do the creating database with filestream.

Here am using the SMO from Visual Studio Dot Net 3.5. Smo object is very useful to do the sqlserver manipulations from code end. However we could create a db with filestream. Just go thru the following code and use it.

using Microsoft.SqlServer.Management.Smo;

string dbName = "DBName";
string dbServerMachineName = "MachineName";

string mdfFile = mdfDirectory+@"\" + dbName + ".mdf";
string ldfFile = ldfDirectory + @"\" + dbName + "_log.ldf";
string FilesDir = FilesDirectory +@"\" + dbName;

Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(dbServerMachineName);

Database database = new Database(server, dbName);
//Adding Primary FileGroup
database.FileGroups.Add(new FileGroup(database, "PRIMARY"));
//Adding FileStream FileGroup
database.FileGroups.Add(new FileGroup(database, "FileStreamTest", true));
//Adding Files for Primary FileGroup
DataFile dtPrimary = new DataFile(database.FileGroups["PRIMARY"], "PriValue", mdfFile);
dtPrimary.Size = 77.0 * 1024.0;
dtPrimary.GrowthType = FileGrowthType.KB;
dtPrimary.Growth = 1.0 * 1024.0;
database.FileGroups["PRIMARY"].Files.Add(dtPrimary);
//Adding Files for FileStream FileGroup
database.FileGroups["FileStreamTest "].Files.Add(new DataFile(database.FileGroups["FileStreamTest "], " FileStreamTest Values", FilesDir));
//Adding LogFile
LogFile logFile = new LogFile(database, "PriValue_log", ldfFile);
logFile.Size = 7.0 * 1024.0;
logFile.GrowthType = FileGrowthType.Percent;
logFile.Growth = 10.0;
database.LogFiles.Add(logFile);
//Create database.
database.Create();
database.Refresh();

...S.VinothkumaR.

How to restore a DataBase from .bak file using (C#) SMO in DotNet 3.5?

The following code will help you to do this restore processing.


using Microsoft.SqlServer.Management.Smo;

string backupFile="Backupfile";//.bak file

string dbName = "DBName";

string dbServerMachineName = "MachineName";

Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(dbServerMachineName);

Database database = new Database(server, dbName);

//If Need

database.Create();

database.Refresh();

//Restoring

Restore restore = new Restore();

restore.NoRecovery = false;

restore.Action = RestoreActionType.Database;

BackupDeviceItem bdi = default(BackupDeviceItem);

bdi = new BackupDeviceItem(backupFile, DeviceType.File);

restore.Devices.Add(bdi);

restore.Database = dbName;

restore.ReplaceDatabase = true;

restore.PercentCompleteNotification = 10;

restore.SqlRestore(server);

database.Refresh();

database.SetOnline();

server.Refresh();


...S.VinothkumaR.

How to taking backup a database using (c#) DotNet framework 3.5? Or using SMO?

How to taking backup a database using (c#) DotNet framework 3.5? Or using SMO?

Add the following reference in your project for using SMO through “Add Reference”.

- Microsoft.SqlServer.ConnectionInfo

- Microsoft.SqlServer.Management.Sdk.Sfc

- Microsoft.SqlServer.Smo

- Microsoft.SqlServer.SmoExtended

- Microsoft.SqlServer.SqlEnum


Now, you can use the following code for taking backup of given database using SMO.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using Microsoft.SqlServer.Management.Smo;

string MachineName="Your MachineName";

string DBName="Your DBName";

string backupName = "tempBackup";

Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(MachineName);

Database db = server.Databases[DBName];

//To Avoid TimeOut Exception

server.ConnectionContext.StatementTimeout = 60 * 60;

RecoveryModel recoverymodel = db.DatabaseOptions.RecoveryModel;

//Define a Backup object variable.

Backup backup = new Backup();

backup.CompressionOption = BackupCompressionOptions.On;

//Specify the type of backup, the description, the name, and the database to be backed up.

backup.Action = BackupActionType.Database;

backup.BackupSetDescription = "Full backup of " + DBName + ".";

backup.BackupSetName = backupName;

backup.Database = DBName;

//Declare a BackupDeviceItem

BackupDeviceItem bdi = default(BackupDeviceItem);

bdi = new BackupDeviceItem(@"C:\ " + backupName + ".bak", DeviceType.File);

//Add the device to the Backup object.

backup.Devices.Add(bdi);

//Set the Incremental property to False to specify that this is a full database backup.

backup.Incremental = false;

//Set the expiration date.

System.DateTime backupdate = new System.DateTime();

backupdate = new System.DateTime(2008, 10, 5);

backup.ExpirationDate = backupdate;

//Specify that the log must be truncated after the backup is complete.

backup.LogTruncation = BackupTruncateLogType.Truncate;

//Run SqlBackup to perform the full database backup on the instance of SQL Server.

backup.SqlBackup(server);

//Remove the backup device from the Backup object.

backup.Devices.Remove(bdi);

That's it...


...S.VinothkumaR.

How to write entry in EventViewer using C#?

Hi all,

Just copy the following method in your project and call that method. It will write an entry in your EventViewer.

public void testEventLogEntry()

{

if (!System.Diagnostics.EventLog.SourceExists("MyTest.WinService"))

System.Diagnostics.EventLog.CreateEventSource(

"MyTest.WinService", "Application");

System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog();

eventLog.Source = "MyTest.WinService";

eventLog.WriteEntry("Test");

}


...S.VinothkumaR.

How to get BitmapSource from System.Drawing.Bitmap in C# 3.5

public BitmapSource getBitmapSourceFromBitmap(Bitmap bmp)

{

BitmapSource returnSource = null;

try

{

returnSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),

IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

}

catch { returnSource = null; }

return returnSource;

}



...S.VinothkumaR.

Getting Bitmap from Base64String in C#

Use the following code for getting Bitmap from Base64String.

public Bitmap getBitmapFromBase64String(string base64Value)

{

Bitmap img = null;

byte[] imgData = Convert.FromBase64String(base64Value);

using (MemoryStream ms = new MemoryStream())

{

ms.Write(imgData, 0, imgData.Length);

img = (Bitmap)Image.FromStream(ms, true);

ms.Close();

ms.Dispose();

}

return img;

}


....S.VinothkumaR.

Displaying an Image in an aspx page as OutputStream using C#

Dear Viewers,

Here am going to give a solution to display an image in an aspx page as OutputStream by using c#.

First of all we need to remove all code in that aspx except the line of page tag (i.e., <%@ Page Language="C#")

In Page_Load

Do the code behind as follows,

Response.ContentType = "image/jpeg";
System.Drawing.Image img = System.Drawing.Image.FromFile(@"C:\test.jpg");
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

Yes, this the way to save image in aspx page as OutputStream.

...S.VinothkumaR.

Getting Images from FTP using C#

Hi all,

Here the sample for getting an Image from ftp file path using C#.

using System.Net;

string path = "ftp://dev-ftp.test.com/" + folderName + "\\" + fileName;

FtpWebRequest ftp;

ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));

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

ftp.Method = WebRequestMethods.Ftp.DownloadFile;

using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())

{

using (Stream responseStream = response.GetResponseStream())

{

using (FileStream fs = new FileStream(localPath+fileName, FileMode.Create))

{

byte[] buffer = new byte[102400];

int read = 0;

do

{

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

fs.Write(buffer, 0, read);

fs.Flush();

} while (!(read == 0));

fs.Flush();

fs.Close();

response.Close();

responseStream.Close();


Hence we can save an image in to our local file path from ftp file path.

That's it....

...S.VinothkumaR.