我將大量行插入數據庫並嘗試在其上建立主鍵。如果我立即創建表並建立密鑰,那麼即使使用SQLBulkCopy命令插入數據也需要10倍的時間。所以這不是一個可行的選擇。我現在要做的就是插入數據,並在插入數據後,使用SMO創建主鍵。問題是,即使連接字符串中的超時設置爲0,我仍然在alter()命令上獲取超時異常。任何想法如何解決這個問題?SMO中的連接超時
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();
}
}
是使用SMO的要求? – 2013-02-21 15:32:14
@MichaelPerrenoud爲了清晰和一致的代碼,我想。你想創建一個SQL腳本並運行它嗎? – dko 2013-02-21 15:39:09
你不需要conn.close(),你已經將連接封裝在Using語句中,連接將自動關閉。 – Derek 2013-02-21 15:42:02