我正在将大量行插入数据库并尝试在其上建立主键。如果我立即创建表并建立一个键,即使使用 SQLBulkCopy 命令,插入数据的时间也是 10 倍。所以这不是一个可行的选择。我现在要做的是插入数据,全部插入后,使用 SMO 创建主键。问题是,即使连接字符串中的超时设置为 0,我仍然在 alter() 命令上收到超时异常。关于如何解决这个问题的任何想法?
connectionString.ConnectTimeout = 0;
ServerConnection scon = null;
using (SqlConnection conn = new SqlConnection(connectionString.ConnectionString))
{
    conn.Open();
    try
    {
        scon = new ServerConnection(conn);
        Console.WriteLine("Server Connection Timeout: " + scon.ConnectTimeout);
        Server serv = new Server(scon);
        Database db = serv.Databases[connectionString.InitialCatalog];
        Table table = db.Tables[tableName];
        Index i = new Index(table, "pk_" + table.Name);
        i.IndexKeyType = IndexKeyType.DriPrimaryKey;
        foreach (String s in PrimaryKey)
        {
            i.IndexedColumns.Add(new IndexedColumn(i, s.Trim()));
        }
        table.Indexes.Add(i);
        table.Alter();
        scon.Disconnect();
    }
    finally
    {
        conn.Close();
    }
}