views:

212

answers:

1
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using Microsoft.SqlServer.Management.Common;

using Microsoft.SqlServer.Management.Smo;

using System.Data.SqlClient;

using Microsoft.Win32;

using System.Runtime.InteropServices;


[DllImport("ODBCCP32.dll")]

private static extern bool SQLConfigDataSource(IntPtr parent, int request, string driver, string attributes);



namespace CopyDatabase
{

public partial class Synchronize : Form
{

    public Synchronize()
    {
        InitializeComponent();
    }

    private void Synchronize_Load(object sender, EventArgs e)
    {
        Server srv = new Server();
        String[] s = { "master", "tempdb", "model", "msdb", "Resource", "distribution" };

        foreach (Database database in srv.Databases)
        {
            int flag = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (String.Compare(database.Name, s[i], true) == 0)
                {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
            {
                cmbSource.Items.Add(database.Name);
                cmbDest.Items.Add(database.Name);
            }
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
       string str = "SERVER=HOME\0DSN=MYDSN\0DESCRIPTION=MYDSNDESC\0DATABASE=DBServer\0TRUSTED_CONNECTION=YES";
        SQLConfigDataSource((IntPtr)0, 4, "SQL Server",str);
    }
}

}

Reference : http://social.msdn.microsoft.com/Forums/en-US/vscrystalreports/thread/441811b9-c4e9-4d15-97a3-7b92d2c9f318

Can anybody help me remove the following errors??

Error 1 Expected class, delegate, enum, interface, or struct C:\Documents and Settings\Pavan\My Documents\Visual Studio 2008\Projects\CopyDatabase\CopyDatabase\Synchronize.cs 17 23 CopyDatabase

.

Error 2 The name 'SQLConfigDataSource' does not exist in the current context C:\Documents and Settings\Pavan\My Documents\Visual Studio 2008\Projects\CopyDatabase\CopyDatabase\Synchronize.cs 67 13 CopyDatabase

+1  A: 

Without the rest of your code for context, my WAG is that you didn't declare a class around the extern and click handler.

Ants
i have posted my program again. I havent implemented much, i just started with the WinForm where i have the requirement to assign DSN dynamically to the database say 'DBServer', how can i declare a class around extern and click handler?. Can you help me out??
Pavan Kumar
Looks like your click handler is already in the class. Just move the your extern down about 10 lines so that it declared inside your Synchronize class.
Ants
i figured it out. i actually had to import the dll inside class. i declared with namespaces.Thanks.:)
Pavan Kumar