如何使用C#为SQL Server创建DSN?

时间:2010-05-16 14:19:28

标签: c# sql-server-2005 dsn

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

}

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

有人可以帮我删除以下错误吗?

  

错误1预期的班级,代表,   枚举,界面或   struct C:\ Documents and   设置\ Pavan \ My Documents \ Visual   工作室   2008 \ Projects \ CopyDatabase \ CopyDatabase \ Synchronize.cs 17 23 CopyDatabase

  

错误2名称'SQLConfigDataSource'   在当前不存在   上下文C:\ Documents and   设置\ Pavan \ My Documents \ Visual   工作室   2008 \ Projects \ CopyDatabase \ CopyDatabase \ Synchronize.cs 67 13 CopyDatabase

2 个答案:

答案 0 :(得分:1)

如果没有其他代码用于上下文,我的WAG就是你没有在extern周围声明一个类并单击处理程序。

答案 1 :(得分:1)

如果只需要动态创建SQL Dsn,只需将此代码更改为此结构 -

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