使用sqlConnectionStringBuilder来处理 Initial Catalog 设置插入的额外值:
1 System.Data.SqlClient.SqlConnectionStringBuilder builder
= new System.Data.SqlClient.SqlConnectionStringBuilder(); 2 builder["Data Source"] = "(local)"; 3 builder["integrated Security"] = true; 4 builder["Initial Catalog"] = "AdventureWorks;NewValue=Bad"; 5 string connectionString = builder.ConnectionString;
其中,;NewValue=Bad就是注入的其他字符串,如果不加处理直接运行,会出错。这种情况下,就可以使用builder构造链接字符串。