How to create DSN for SQL Server using C#?

删除回忆录丶 提交于 2019-12-13 05:20:25

问题


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:


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




回答2:


If you need only to dynamically create SQL Dsn just change this code to this structure--

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 System.Data.SqlClient;  
using Microsoft.Win32;  
using System.Runtime.InteropServices;   


namespace CopyDatabase 
{  
 public partial class Synchronize : Form 
 {      
   [DllImport("ODBCCP32.dll")]  
   private static extern bool SQLConfigDataSource(IntPtr parent, int request, string
   driver, string attributes);    

   public Synchronize()     
   {         
     InitializeComponent();     
   }      

   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);     
   } 
  } 


来源:https://stackoverflow.com/questions/2844074/how-to-create-dsn-for-sql-server-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!