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

发布于 2024-09-01 09:15:17 字数 2322 浏览 1 评论 0原文

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 ​​预期的类、委托、 枚举、接口或 结构 C:\Documents 和 设置\Pavan\我的文档\Visual 工作室 2008\Projects\CopyDatabase\CopyDatabase\Synchronize.cs 17 23 CopyDatabase

错误 2 名称“SQLConfigDataSource” 当前不存在 上下文 C:\Documents 和 设置\Pavan\我的文档\Visual 工作室 2008\Projects\CopyDatabase\CopyDatabase\Synchronize.cs 67 13 CopyDatabase

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀中猫帐中妖 2024-09-08 09:15:17

如果没有其余代码的上下文,我的 WAG 是您没有在 extern 和 click 处理程序周围声明一个类。

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

梨涡少年 2024-09-08 09:15:17

如果您只需要动态创建 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);     
   } 
  } 

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);     
   } 
  } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文