SMO中的连接超时

dko*_*dko 4 c# smo primary-key

我正在将大量行插入数据库并尝试在其上建立主键.如果我创建表并立即建立密钥,即使使用SQLBulkCopy命令,插入数据也需要10倍.所以这不是一个可行的选择.我现在要做的是插入数据,然后插入数据后,使用SMO创建主键.问题是我在alter()命令中不断获得超时异常,即使连接字符串中的超时设置为0.关于如何解决这个问题的任何想法?

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

dko*_*dko 5

显然,ServerConnection也有语句超时.SMO充满了这些隐藏的超时.包括SQLBulkCopy.但是,感谢@Derek指出这一点.答案是你必须设置StatementTimeout = 0.我现在正在测试它,但它似乎是答案.

如何设置CommandTimeout

http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.common.serverconnection.statementtimeout.aspx