如何使用C#为SQL Server创建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);
}
}
}
任何人都可以帮助我删除以下错误吗?
错误 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);
}
}
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有其余代码的上下文,我的 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.
如果您只需要动态创建 SQL Dsn,只需将此代码更改为以下结构--
If you need only to dynamically create SQL Dsn just change this code to this structure--