Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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# e将来我将使用“完整”SQL Server版本@Kaido,用户实例是我当时的想法。困扰我的是,我将如何编写从winforms应用程序到SQL Server的数据库附件。有什么建议吗?你能给我一个提示,如何从windows窗体(例如,使用ADO.NET)用_C#_Sql Server_Winforms_Ado.net_Sql Server Express - Fatal编程技术网

C# e将来我将使用“完整”SQL Server版本@Kaido,用户实例是我当时的想法。困扰我的是,我将如何编写从winforms应用程序到SQL Server的数据库附件。有什么建议吗?你能给我一个提示,如何从windows窗体(例如,使用ADO.NET)用

C# e将来我将使用“完整”SQL Server版本@Kaido,用户实例是我当时的想法。困扰我的是,我将如何编写从winforms应用程序到SQL Server的数据库附件。有什么建议吗?你能给我一个提示,如何从windows窗体(例如,使用ADO.NET)用,c#,sql-server,winforms,ado.net,sql-server-express,C#,Sql Server,Winforms,Ado.net,Sql Server Express,e将来我将使用“完整”SQL Server版本@Kaido,用户实例是我当时的想法。困扰我的是,我将如何编写从winforms应用程序到SQL Server的数据库附件。有什么建议吗?你能给我一个提示,如何从windows窗体(例如,使用ADO.NET)用C#代码实现这一点吗?谢谢。您必须执行存储过程来创建、附加和分离数据库。要在C#中执行此操作,您需要使用适当的连接字符串打开一个SqlConnection对象,创建一个SqlCommand对象,传递SqlConnection对象和附加或分离数据


e将来我将使用“完整”SQL Server版本@Kaido,用户实例是我当时的想法。困扰我的是,我将如何编写从winforms应用程序到SQL Server的数据库附件。有什么建议吗?你能给我一个提示,如何从windows窗体(例如,使用ADO.NET)用C#代码实现这一点吗?谢谢。您必须执行存储过程来创建、附加和分离数据库。要在C#中执行此操作,您需要使用适当的连接字符串打开一个SqlConnection对象,创建一个SqlCommand对象,传递SqlConnection对象和附加或分离数据库的存储过程的名称,向SqlCommand对象添加一些参数对象,并使用ExecuteOnQuery()执行SqlCommand。这里有一个完整的例子,大约在这一页的一半:谢谢你,罗伯特。你帮了我大忙。哇,这真让我高兴。其他人都在试图劝阻询问者不要尝试这个解决方案,而您只是随意地出现在一个简短、干净、简洁的代码列表中回答它。祝贺你,好先生,你赢得了我的选票。
// Add a reference to Microsoft.SqlServer.Smo
// Add a reference to Microsoft.SqlServer.ConnectionInfo
// Add a reference to Microsoft.SqlServer.SqlEnum

using Microsoft.SqlServer.Management.Smo;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;

public class SqlServerController
{

    private Server m_server = null;

    public SqlServerController(string server)
    {
        m_server = new Server(server);
    }

    public void AttachDatabase(string database, StringCollection files,
        AttachOptions options)
    {
        m_server.AttachDatabase(database, files, options);
    }

    public void AddBackupDevice(string name)
    {
        BackupDevice device = new BackupDevice(m_server, name);
        m_server.BackupDevices.Add(device);
    }

    public string GetServerVersion(string serverName)
    {
        return m_server.PingSqlServerVersion(serverName).ToString();
    }

    public int CountActiveConnections(string database)
    {
        return m_server.GetActiveDBConnectionCount(database);
    }

    public void DeleteDatabase(string database)
    {
        m_server.KillDatabase(database);
    }

    public void DetachDatabase(string database, bool updateStatistics, 
        bool removeFullTextIndex)
    {
        m_server.DetachDatabase(database, updateStatistics, removeFullTextIndex);
    }

    public void CreateDatabase(string database)
    {
        Database db = new Database(m_server, database);
        db.Create();
    }

    public void CreateTable(string database, string table, 
        List<Column> columnList, List<Index> indexList)
    {
        Database db = m_server.Databases[database];
        Table newTable = new Table(db, table);

        foreach (Column column in columnList)
            newTable.Columns.Add(column);

        if (indexList != null)
        {
            foreach (Index index in indexList)
                newTable.Indexes.Add(index);
        }

        newTable.Create();

    }

    public Column CreateColumn(string name, DataType type, string @default,
        bool isIdentity, bool nullable)
    {
        Column column = new Column();

        column.DataType = type;
        column.Default = @default;
        column.Identity = isIdentity;
        column.Nullable = nullable;

        return column;
    }

    public Index CreateIndex(string name, bool isClustered, IndexKeyType type,
      string[] columnNameList)
    {

        Index index = new Index();

        index.Name = name;
        index.IndexKeyType = type;
        index.IsClustered = isClustered;

        foreach (string columnName in columnNameList)
            index.IndexedColumns.Add(new IndexedColumn(index, columnName));

        return index;
    }

}
   <connectionstring>Data Source=.\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=True;Pooling=True</connectionstring>
  var connectionString = new SqlConnectionStringBuilder(connectionString);

  var serverConnection = new ServerConnection("DatabaseInstanceName in server");

  var serverInstance = new Server(serverConnection);

  if (serverInstance.Databases.Contains(connectionString.InitialCatalog))
      serverInstance.KillDatabase(connectionString.InitialCatalog);

  var db = new Database(serverInstance, connectionString.InitialCatalog);

  try
  {
     db.Create();
  }
  catch (SqlException ex)
  {
     throw;
  }