DbProviderFactory provider = DbProviderFactories.GetFactory(...); Public DbCommand GetCommand(sring strSql) { using(DbConnection conn = provider.CreateConnection()) { conn.connection = connectionString; conn.Open(); try { DbCommand cmd=provider.CreateCommand(); cmd.Connection = conn; cmd.CommandText= strSql; cmd.CommandType=CommandType.Text; return cmd; } catch(DbException e) { conn.Close(); conn.Dispose(); throw new Exception(e.Message); } } }
在使用时 DbCommand cmd= DbHelper.GetCommand(“update…..”); cmd的Connecion属性报引发了System.InvalidOperationException”类型的异常
解决方案
40
去掉using就行