我将大量行插入数据库并尝试在其上建立主键。如果我立即创建表并建立密钥,那么即使使用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