'.NET'에 해당되는 글 2건
- 2009.02.16 VSTO에서 메세지 서브클래싱..
- 2009.02.16 SQL Server 2005 Database Backup and Restore using C# and .NET 2.0
VSTO에서 메세지 서브클래싱..
오피스 2007의 '리본X UI'에서 OfficeMenu(좌측 상단의 동그란 MS마크의 메뉴) 의 동작을 runtime 시에 동적으로 disable 시키고 싶었는데 그게 안되더라. 는 것이었다.
애초부터 안되는거 아니냐고? 설마! 그랬으면 내가 이런 일말의 희망을 가지고 이틀동안 인터넷을 떠돌아 다녔을리가 없다.
실제로 다음과 같이 리본 메뉴 스키마를 작성하면 disable 시킬수가 있다.
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load">
<ribbon>
<officeMenu>
<button idMso="FileNew" enabled ="false"/>
</officeMenu>
<tabs>
<tab idMso="TabAddIns">
<group id="MyGroup"
label="My Group">
<toggleButton id="toy" label="연습" size="large" onAction="OnToggle" imageMso="HappyFace"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
근데.. 왠지 될거 같지 않은가? 왠지 모르게.. 저걸 읽어서 UI에 변화를 줄수 있다면 반대로 변화도 줄수 있을거야. MS가 나보다는 똑똑하니까, 분명히 무슨 함수라던지 속성이라던지가 있을꺼야.. 이러면서 이틀간 인터넷에서 사경을 헤멨(응?)..
근데, 암만 찾아봐도 dynamic하게 변경은 못하는것 같더라. 그래서 또 생각했다. 그러면 저 '리본 메뉴 스키마'를 2개(enable/disable)만들어 둬서 reload하면 될꺼야. 또 뒤졌다... 분명히 뭔가 있기는 있다. 그런데 이해가 안되더라.
.NET 환경에서 작성된 내용도 아닌거 같고..(난 영어가 시러염!!!) 어쨌든 오늘도 그렇게 무료히 시간을 죽이고 있었더니..
invalidate라는 함수가 눈에 띄었다. (사실 어제부터 눈에 거슬렸으나.. 정확한 용법이 없으니 이게 뭘 하는지 모르겠는거라..)
MSDN을 뒤졌다. 오예~ 예제가 있다. 우선 살짝쿵 훑었다. 뭐 별거 없네.. 하고 닫으려는 순간 눈에 밟히는 문장이 있었으니..
[Instead of the Save dialog box appearing, the custom dialog box appears, as shown in Figure 2.]...
disable이 안된다면 내가 원하는대로 돌리면 되지(아놔.. '돼지' 야? '되지' 야?)..!!
그래서 결과 코드는 다음과 같이..
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load">
<!-- QAT(Quick Access Toolbar) 부분 -->
<commands>
<command idMso="FileSave" onAction="MySave" />
</commands>
<ribbon startFromScratch="false">
<!-- OfficeMenu 부분 -->
<officeMenu>
<button idMso="FileSave" enabled ="true" onAction ="MySave"/>
</officeMenu>
<tabs>
<tab idMso="TabAddIns">
<group id="MyGroup"
label="My Group">
<toggleButton description="this is test" id="toy" label="연습" size="large" onAction="OnToggle" imageMso="HappyFace"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Public Sub MySave(ByVal control As Office.IRibbonControl, ByRef cancelDefault As Boolean)
If (repurposing) Then
System.Windows.Forms.MessageBox.Show("로컬 컴퓨터에 저장할수 없음.")
cancelDefault = True
Else
cancelDefault = False
End If
End Sub
#Region "Smile Toggle Button Action"
Public Sub OnToggle(ByVal control As Office.IRibbonControl, ByVal isPressed As Boolean)
If (isPressed) Then
ReportTaskPane.Visible = True
Else
ReportTaskPane.Visible = False
End If
repurposing = isPressed
ribbon.Invalidate()
End Sub
#End Region
referenced from: http://msdn2.microsoft.com/en-us/library/bb462633.aspx
SQL Server 2005 Database Backup and Restore using C# and .NET 2.0
Introduction
The following article describes accessing a SQL Server 2005 database backup and restoring it programmatically using C#.NET 2.0 and SMO. This article provides coding samples to perform the task.
SQL Server Management Objects (SMO) is a collection of objects that are designed for programming all aspects of managing Microsoft SQL Server.
The following namespaces can be used to access SQL Server 2005 programmatically:
Microsoft.SqlServer.management
Microsoft.SqlServer.Management.NotificationServices
Microsoft.SqlServer.Management.Smo
Microsoft.SqlServer.Management.Smo.Agent
Microsoft.SqlServer.Management.Smo.Broker
Microsoft.SqlServer.Management.Smo.Mail
Microsoft.SqlServer.Management.Smo.RegisteredServers
Microsoft.SqlServer.Management.Smo.Wmi
Microsoft.SqlServer.Management.Trace
Pre-Requisite
You need to reference the following namespaces before using this code:
- Microsoft.SqlServer.Management.Smo;
- Microsoft.SqlServer.Management.Common;
I used these two class to perform the backup and restore operations:
- Microsoft.SqlServer.Management.Smo.Backup
- Microsoft.SqlServer.Management.Smo.Restore
For more information, regarding these two class, check MSDN:
Backup database
public void BackupDatabase(String databaseName, String userName,
String password, String serverName, String destinationPath)
{
Backup sqlBackup = new Backup();
sqlBackup.Action = BackupActionType.Database;
sqlBackup.BackupSetDescription = "ArchiveDataBase:" +
DateTime.Now.ToShortDateString();
sqlBackup.BackupSetName = "Archive";
sqlBackup.Database = databaseName;
BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath, DeviceType.File);
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
Database db = sqlServer.Databases[databaseName];
sqlBackup.Initialize = true;
sqlBackup.Checksum = true;
sqlBackup.ContinueAfterError = true;
sqlBackup.Devices.Add(deviceItem);
sqlBackup.Incremental = false;
sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;
sqlBackup.FormatMedia = false;
sqlBackup.SqlBackup(sqlServer);
}
Restore Database
public void RestoreDatabase(String databaseName, String filePath,
String serverName, String userName, String password,
String dataFilePath, String logFilePath)
{
Restore sqlRestore = new Restore();
BackupDeviceItem deviceItem = new BackupDeviceItem(filePath, DeviceType.File);
sqlRestore.Devices.Add(deviceItem);
sqlRestore.Database = databaseName;
ServerConnection connection = new ServerConnection(serverName, userName, password);
Server sqlServer = new Server(connection);
Database db = sqlServer.Databases[databaseName];
sqlRestore.Action = RestoreActionType.Database;
String dataFileLocation = dataFilePath + databaseName + ".mdf";
String logFileLocation = logFilePath + databaseName + "_Log.ldf";
db = sqlServer.Databases[databaseName];
RelocateFile rf = new RelocateFile(databaseName, dataFileLocation);
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName, dataFileLocation));
sqlRestore.RelocateFiles.Add(new RelocateFile(databaseName+"_log", logFileLocation));
sqlRestore.ReplaceDatabase = true;
sqlRestore.Complete += new ServerMessageEventHandler(sqlRestore_Complete);
sqlRestore.PercentCompleteNotification = 10;
sqlRestore.PercentComplete +=
new PercentCompleteEventHandler(sqlRestore_PercentComplete);
sqlRestore.SqlRestore(sqlServer);
db = sqlServer.Databases[databaseName];
db.SetOnline();
sqlServer.Refresh();
}
The portion of code uses full backup features. If you want, you can perform incremental and differential backup as well.
Updates: June 8, 2008
In order to use this code, your SQL Server authentication mode needs to be
configured as Mixed Mode authentication. If you use Windows Authentication, then
you need to modify the ServerConnection
:
SqlConnection sqlCon = new SqlConnection ("Data Source=Bappi; Integrated Security=True;");
ServerConnection connection = new ServerConnection(sqlCon);
Modify the ServerConnection
portion of both code samples using
this code in order to use Windows Security.
Conclusion
As this code uses the SQL Server 2005 backup/restore facility, the code follows the rules of SQL Server 2005 while backing up or restoring databases.