Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#用于sql脚本执行_C#_Sql - Fatal编程技术网

c#用于sql脚本执行

c#用于sql脚本执行,c#,sql,C#,Sql,我有两个表单,首先收集数据源、数据库名称、用户名和密码等详细信息。第二个表单收集要执行的sql脚本文件和要连接的数据库名称 我想要实现的是,我应该能够在所选数据库中执行sql脚本文件 第二种形式中使用的代码如下所示: private void comboBox1_TextChanged(object sender, EventArgs e) { string sel = comboBox1.SelectedItem.ToString(); //Here "sel" is the databas

我有两个表单,首先收集数据源、数据库名称、用户名和密码等详细信息。第二个表单收集要执行的sql脚本文件和要连接的数据库名称

我想要实现的是,我应该能够在所选数据库中执行sql脚本文件

第二种形式中使用的代码如下所示:

private void comboBox1_TextChanged(object sender, EventArgs e)
{
 string sel = comboBox1.SelectedItem.ToString(); //Here "sel" is the database selected
 if (sel == "master")
 {
  comboBox2.Items.Clear();
     //Selects a default file
  DirectoryInfo dinfo = new DirectoryInfo(@"D:\Testpgm");
  FileInfo[] Files = dinfo.GetFiles("master.sql", SearchOption.AllDirectories);
  foreach (FileInfo file in Files)
  {
   comboBox2.Items.Add(file.Name);
  }
 }
 else
 {
  comboBox2.Items.Clear();
     //Selects a default file
  DirectoryInfo dinfo = new DirectoryInfo(@"D:\Testpgm");
  FileInfo[] Files = dinfo.GetFiles("dbscript.sql", SearchOption.AllDirectories);
  foreach (FileInfo file in Files)
  {
  comboBox2.Items.Add(file.Name);
  }
 }
}
组合框2中使用的代码是:

private void comboBox2_TextChanged(object sender, EventArgs e)
{
 string textsel = comboBox2.SelectedItem.ToString(); //textsel is used to select the sql file selected
 if (textsel == "master.sql")
 {
  richTextBox1.Clear();
     //Read the selected sql file and display it in richtextbox
  System.IO.StreamReader myFile = new System.IO.StreamReader(@"D:\Testpgm\master.sql");
  string textentry = myFile.ReadToEnd();
  richTextBox1.AppendText(textentry);
 }
 else
 {
  richTextBox1.Clear();
     //Read the selected sql file and display it in richtextbox
System.IO.StreamReader myFile = new System.IO.StreamReader(@"D:\Testpgm\dbscript.sql");
  string textentry = myFile.ReadToEnd();
  richTextBox1.AppendText(textentry);
 }
}
现在,我想连接到我在组合框1中选择的数据库,然后单击按钮,执行组合框2中选择的sql文件中包含的sql脚本

这可能吗?我怎样才能做到这一点

任何评论都会非常有用,请使用

using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
你可以做如下的事情

SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
这可能吗?我怎样才能做到这一点

如果同时要执行数据库和脚本文件,则可以执行yes

首先,需要获取所选数据库的连接字符串并建立连接。然后可以使用SMO对象执行批处理查询

string connectionString = "YourConnectionString";
//make a sql connection   
using (var sqlConnection = new SqlConnection(connectionString))
    {
        var serverConnection = new ServerConnection(sqlConnection);
        var server = new Server(serverConnection);
        server.ConnectionContext.ExecuteNonQuery(textentry); 
    }

<强>注:如果没有批处理状态,则应考虑使用正常查询。因为使用smo对象可能会遇到很多问题

,但是脚本会在选定的数据库中执行吗?因为有些脚本不能在主数据库中执行。我有两个脚本,一个用于主数据库,另一个用于其他数据库。如果我选择了主数据库,它应该在主数据库中执行。如果我选择了任何其他的,那么它应该在我选择的数据库中执行。这是否可能,通过上面的代码?@VysakhVenugopal您需要连接到您选择的数据库。根据所选数据库生成连接字符串所有数据库的连接字符串都相同。我有一个登录到master的凭据。我通常使用它来执行脚本。因此,这里我也将使用相同的,但需要执行脚本,在组合框1中指定数据库。如果您对所有用户具有相同的凭据,那么您所要做的就是替换连接字符串中的数据库名称。