Thursday, October 11, 2007

Can't delete column from a table (SQL SERVER 2000)

I was working on Sql server 2000 which was migrated from older version to sql server 2000.

I have added a column to an existing table using the following query:

alter table test add col1 varchar(1)

And afterwards I wanted to delete that column. I have tried the following query for that

alter table [test] drop column [col1]

but its always giving me the following error message :

This query executed successfully.

'test' table

- Unable to modify table.

ODBC error: [Microsoft][ODBC SQL Server Driver][SQL Server]Line 2: Incorrect syntax near 'col1'.

 

Same error I was getting when I tried deleting the column from Enterprise Manager.

Resolution :

first run the following query & check the compatibility :

sp_dbcmptlevel databaseName

If the compatability level is 65, change it to 70 or 80 by using the following query:

sp_dbcmptlevel databaseName,70

 

Now the delete query will be executed without fail.

Calling functions in C#.NET dll from C++

Create a C#.NET class Library :
Paste the below code in the class file and build it.
---------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace strEncrypt
{
    public class clsEncrypt
    {
        public string EncryptString(string input)
        {
            try
            {
                System.Text.UnicodeEncoding encoding = new UnicodeEncoding();
                System.Security.Cryptography.SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();

                byte[] byteVal = encoding.GetBytes(input);
                byte[] byteencryptedPwd = sha1.ComputeHash(byteVal);
                sha1.Clear();
                string pwd = Convert.ToBase64String (byteencryptedPwd);

                try
                {
                    StreamWriter sw = new StreamWriter(@"C:\Password.txt");
                    sw.WriteLine(pwd);
                    sw.Close();
                }
                catch
                {
                    //Apart from returning the encrypted string, I am also writing the same to C:\Password.txt.
                }
                return pwd;
            }
            catch
            {
                return "EXCEPTION";
            }
        }
    }
}
---------------------------------------------------------------------------------

Now we will get a C#.NET dll and now let me explain you how to consume this in C++ application.

---------------------------------------------------------------------------------

Create a new project from Visual studio :

Project Types : Visual C++ -> Win32 -> Win32 Console application. Click on OK after entering the file name.

Now we will get Win32 Applicatio Wizard. Click on Next.

Select 'Console Application' and click on finish.

---------------------------------------------------------------------------------

Copy the dll into our C++ project folder.

Open 'Property Pages' of the project. From Common Properties -> References , click on 'Add new reference' and add the C#.NET dll reference.

From the same window  'Configuration Properties' ->

General :

Character Set : Not set

Common Language Runtime support - select 'Common Language Runtime Support (/clr)'

C/C++ : Debug Information format : Disabled

             Warning Level : Level1

             Detect 64 bit portability issues : No

---------------------------------------------------------------------------------

code for .cpp file

---------------------------------------------------------------------------------

#include <iostream>
#using <mscorlib.dll>
#using <strEncryptCS.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::Security::Cryptography;
using namespace std;
using namespace strEncrypt;

System::String^ CPPEncryptString(System::String^);

void main(int argc, char *argv[])
{
 
 System::String^ input;
 System::String^ output;
 Console::WriteLine("Enter String:");
 input=Console::ReadLine();
 output = CPPEncryptString(input);
 Console::WriteLine(output);
 Console::Read();
}

System::String^ CPPEncryptString(System::String^ input)
{
 clsEncrypt a;
 return a.EncryptString(input);
}

---------------------------------------------------------------------------------

Build this application. You will get an exe.

 

This is how we can access & use functions C#.NET dll in C++.