3 Декабрь 2008 г.

How to install (uninstall) Windows service programmatically

First, You should add reference to System.Configuration.Install

Install service:

            string ServiceFileName = @"c:\myservice.exe";
            System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller();
            Installer.Path = ServiceFileName;
            Installer.UseNewContext = true;
            Installer.Install(null);
            Installer.Commit(null);

Uninstall service:

            string ServiceFileName = @"c:\myservice.exe";
            System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller();
            Installer.Path = ServiceFileName;
            Installer.UseNewContext = true;
            Installer.Uninstall(null);

22 Август 2008 г.

SQL Server 2005: how to publish trigger from assembly

1. Reconfigure server state for CLR assembly use

sp_configure 'clr enabled', 1;
GO
RECONFIGURE;
GO


2. Enable TRUSTWORTHY property of database


ALTER DATABASE database_name SET TRUSTWORTHY ON


3. Register assembly in database


CREATE ASSEMBLY assembly_name
FROM assembly_full_path
WITH PERMISSION_SET = UNSAFE;
GO


4.  Create trigger  on the table
USE database_name
IF EXISTS (SELECT * FROM sys.triggers WHERE parent_class = 0 AND name = 'trigger_name') DROP TRIGGER trigger_name ON DATABASE
GO
CREATE TRIGGER trigger_name ON table_name AFTER INSERT, UPDATE, DELETE
AS
EXTERNAL NAME assembly_name.trigger_class_name.trigger_method
GO

20 Август 2008 г.

Trigger in SQL Server Error: MSDTC on server is unavailable

I found decision here:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=54705

---

On the server where the trigger resides, you need to turn the MSDTC service on. You can this by clicking START > SETTINGS > CONTROL PANEL > ADMINISTRATIVE TOOLS > SERVICES. Find the service called 'Distributed Transaction Coordinator' and RIGHT CLICK (on it and select) > Start.

Test the trigger and see if it works. If it still does not work, wrap you trigger in the following transaction code (found below in bold):


SET XACT_ABORT ON
BEGIN DISTRIBUTED TRANSACTION

-- Put all queries in here (SELECT, INSERT, UPDATE, and DELETE)
select * from [SERVER2].[DBASE].[OWNER].[TABLENAME]
update [SERVER2].[DBASE].[OWNER].[TABLENAME]
set [column] = value
where [condition(s)]

COMMIT TRANSACTION
SET XACT_ABORT OFF

24 Июль 2008 г.

How generate CRM4 proxy-class

Example of command file:

cd "C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin"

wsdl /out:c:\CrmProxy.cs /n:YourCrmService http://localhost/MSCRMServices/2007/CrmServiceWsdl.aspx?wsdl

23 Июль 2008 г.

Execute T-SQL script from C# code

if (filename.Length > 0)
{
try
{
// Open the file to read from.
using (StreamReader sr = File.OpenText(filename))
{
string queryString = sr.ReadToEnd().Replace("GO", "");

using (SqlConnection connection = new SqlConnection(ASSettings.ConnectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
}
MessageBox.Show("Script executed.", "Result", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error script execution!\n" + ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

17 Июль 2008 г.

Repair DBF structure (if damaged file header)

SET TABLEVALIDATE TO 3
CD "C:\Repair\"

OPEN DATABASE osago EXCLUSIVE
VALIDATE DATABASE TO FILE before.txt

lnDBF = ADIR(laDBF,"*.dbf")
FOR i = 1 TO lnDBF
  TRY
  USE (laDBF[i,1]) EXCLUSIVE
  CATCH
  SET TABLEVALIDATE TO 0
  USE (laDBF[i,1]) EXCLUSIVE

  MESSAGEBOX("Table "+laDBF[i,1]+" could not be opened. "+CHR(13)+;
  "Please delete any damaged records (Ctrl-T). "+CHR(13)+;
  "Press Ctrl-W when finished",16)

  BROWSE
  SET TABLEVALIDATE TO 3
  FINALLY
  PACK
  REINDEX
  ENDTRY
NEXT
VALIDATE DATABASE RECOVER TO FILE after.txt

30 Июнь 2008 г.

CRM 4: user-side call jscript event handler for CRM-entity

// CRM 4.0 SDK Example
// Define the soapBody for the WhoAmI request.
// Difference from Microsoft CRM 3.0 is requirement to call method GenerateAuthenticationHeader() in SOAP Envelope

var soapBody = "";
soapBody += "";
soapBody += "";
soapBody += "
";
//Wrap the Soap Body in a soap:Envelope.
var soapXml = "soapXml += "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" ";
soapXml += "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ";
soapXml += "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
soapXml += GenerateAuthenticationHeader();
soapXml += soapBody;
soapXml += "
";
var SERVER_URL = "http://crm4.company.ru:5555";
// Create the XMLHTTP object for the execute method.
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
xmlhttp.open("POST", SERVER_URL + "/mscrmservices/2007/crmservice.asmx", false);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
//Send the XMLHTTP object.
xmlhttp.send(soapXml);
// Create an XML object to parse the results.
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(xmlhttp.responseXML.xml);
// Get the user's ID.
var userid = xmlDoc.getElementsByTagName("UserId")[0].childNodes[0].nodeValue;
alert(userid);