📄 sqlservices.cs
字号:
else
{
content = FixContent(content, database, null, sessionState, sessionStatetype);
}
sr = new StringReader(content);
sqlCmd = new SqlCommand(null, connection);
do {
bool run = false;
// Read a line from a file. If it's not a "GO", batch it up.
// It it's a "GO" (or the last line), send over all batched
// commands over to SQL.
cur = sr.ReadLine();
if (cur == null) {
run = true;
}
else {
if (string.Compare(cur.Trim(), "GO", true) == 0)
{
run = true;
}
else {
if (cmdText != null) {
cmdText += "\n";
}
cmdText += cur;
}
}
if (run & cmdText != null) {
sqlCmd.CommandText = cmdText;
try {
sqlCmd.ExecuteNonQuery();
}
catch (Exception e) {
SqlException sqlExpt = e as SqlException;
if (sqlExpt != null) {
int expectedError = -1;
if (cmdText.IndexOf("sp_add_category", StringComparison.Ordinal) > -1) {
expectedError = 14261; /* already exists */
}
else if (cmdText.IndexOf("sp_delete_job", StringComparison.Ordinal) > -1) {
expectedError = 14262; /* doesn't exists */
if (sessionState && !isInstall) {
throw new SqlExecutionException(SR.GetString(SR.SQL_Services_Error_Deleting_Session_Job),
server, database, file, cmdText, sqlExpt);
}
}
if (sqlExpt.Number != expectedError) {
throw new SqlExecutionException(
SR.GetString(SR.SQL_Services_Error_Executing_Command,
file, sqlExpt.Number.ToString(CultureInfo.CurrentCulture), sqlExpt.Message),
server, database, file, cmdText, sqlExpt);
}
}
}
cmdText = null;
}
} while (cur != null);
}
static void ApplicationServicesParamCheck(SqlFeatures features, ref string database) {
if (features == SqlFeatures.None) {
return;
}
if ((features & SqlFeatures.All) != features) {
throw new ArgumentException(SR.GetString(SR.SQL_Services_Invalid_Feature));
}
CheckDatabaseName(ref database);
}
private static void CheckDatabaseName(ref string database)
{
if (database != null) {
database = database.TrimEnd();
if (database.Length == 0)
throw new ArgumentException(SR.GetString(SR.SQL_Services_Database_Empty_Or_Space_Only_Arg));
database = RemoveSquareBrackets(database);
if (database.Contains("'") || database.Contains("[") || database.Contains("]"))
throw new ArgumentException(SR.GetString(SR.SQL_Services_Database_contains_invalid_chars));
}
if (database == null) {
database = DEFAULT_DB;
}
else {
database = "[" + database + "]";
}
}
public static string GenerateApplicationServicesScripts(bool install, SqlFeatures features, string database) {
string content;
StringBuilder sb = new StringBuilder();
ArrayList files;
ApplicationServicesParamCheck(features, ref database);
files = GetFiles(install, features);
foreach (string sqlFile in files) {
string fullpath = Path.Combine(HttpRuntime.AspInstallDirectory, sqlFile);
content = File.ReadAllText(fullpath);
sb.Append(FixContent(content, database, null, false, SessionStateType.Temporary));
}
return sb.ToString();
}
static string RemoveSquareBrackets(string database) {
if (database != null && database.StartsWith("[") && database.EndsWith("]"))
return database.Substring(1, database.Length-2);
return database;
}
static void EnsureDatabaseExists(string database, SqlConnection sqlConnection) {
SqlCommand cmd;
string databaseNoSquareBrackets = RemoveSquareBrackets(database);
// First, make sure the database exists
cmd = new SqlCommand("SELECT DB_ID(@database)", sqlConnection);
cmd.Parameters.Add(new SqlParameter("@database", databaseNoSquareBrackets));
object res = cmd.ExecuteScalar();
if (res == null || res == System.DBNull.Value) {
// The database doesn't even exist.
throw new HttpException(
SR.GetString(SR.SQL_Services_Error_Cant_Uninstall_Nonexisting_Database,
databaseNoSquareBrackets));
}
}
// Add/Remove all requested general features
static void SetupApplicationServices(
string server,
string user,
string password,
bool trusted,
string connectionString,
string database,
string dbFileName,
SqlFeatures features,
bool install )
{
SqlConnection sqlConnection = null;
ArrayList files;
ApplicationServicesParamCheck(features, ref database);
files = GetFiles(install, features);
try {
sqlConnection = GetSqlConnection(server, user, password, trusted, connectionString);
// If uninstall, make sure all the asp.net tables are empty
if (!install) {
EnsureDatabaseExists(database, sqlConnection);
string databaseNoSquareBrackets = RemoveSquareBrackets(database);
if (sqlConnection.Database != databaseNoSquareBrackets)
sqlConnection.ChangeDatabase(databaseNoSquareBrackets);
int itablesToCheck = 0;
for (int i=0; i < s_featureInfos.Length; i++)
if (((int)s_featureInfos[i]._feature & (int)features) == (int)s_featureInfos[i]._feature)
itablesToCheck |= s_featureInfos[i]._dataCheckBitMask;
SqlCommand cmd = new SqlCommand("dbo.aspnet_AnyDataInTables", sqlConnection);
cmd.Parameters.Add(new SqlParameter("@TablesToCheck", itablesToCheck));
cmd.CommandType = CommandType.StoredProcedure;
string table = null;
try {
table = cmd.ExecuteScalar() as string;
} catch (SqlException e) {
if (e.Number != 2812)
throw;
}
if (!string.IsNullOrEmpty(table))
throw new NotSupportedException(
SR.GetString(SR.SQL_Services_Error_Cant_Uninstall_Nonempty_Table,
table, database));
}
// Load and run the sql file for each feature
foreach (string sqlFile in files) {
ExecuteFile(sqlFile, server, database, dbFileName, sqlConnection, false, false, SessionStateType.Temporary);
}
}
finally {
if (sqlConnection != null) {
try {
sqlConnection.Close();
}
catch {
}
finally {
sqlConnection = null;
}
}
}
}
static void SessionStateParamCheck(SessionStateType type, ref string customDatabase) {
if (type == SessionStateType.Custom && String.IsNullOrEmpty(customDatabase)) {
throw new ArgumentException(
SR.GetString(SR.SQL_Services_Error_missing_custom_database), "customDatabase");
}
if (type != SessionStateType.Custom && customDatabase != null) {
throw new ArgumentException(
SR.GetString(SR.SQL_Services_Error_Cant_use_custom_database), "customDatabase");
}
CheckDatabaseName(ref customDatabase);
}
static void SetupSessionState(string server, string user, string password, bool trusted,
string connectionString, string customDatabase, SessionStateType type, bool install) {
SqlConnection sqlConnection = null;
SessionStateParamCheck(type, ref customDatabase);
try {
sqlConnection = GetSqlConnection(server, user, password, trusted, connectionString);
if (!install && type == SessionStateType.Custom) {
EnsureDatabaseExists(customDatabase, sqlConnection);
}
// Load and run the sql file for each feature
ExecuteSessionFile(install ? SESSION_STATE_INSTALL_FILE : SESSION_STATE_UNINSTALL_FILE,
server, customDatabase, null, sqlConnection, install, type);
}
finally {
if (sqlConnection != null) {
try {
sqlConnection.Close();
}
catch {
}
finally {
sqlConnection = null;
}
}
}
}
static string ConstructConnectionString(string server, string user, string password, bool trusted) {
string connectionString = null;
// Construct the connection string
if (String.IsNullOrEmpty(server)) {
throw new ArgumentNullException("server");
}
connectionString += "server=" + server;
if (trusted) {
connectionString += ";Trusted_Connection=true;";
}
else {
if (String.IsNullOrEmpty(user)) {
throw new ArgumentNullException("user");
}
connectionString += ";UID=" + user + ";" + "PWD=" + password + ";";
}
return connectionString;
}
static SqlConnection GetSqlConnection(string server, string user, string password,
bool trusted, string connectionString) {
SqlConnection sqlConnection;
if (connectionString == null) {
connectionString = ConstructConnectionString(server, user, password, trusted);
}
try {
sqlConnection = new SqlConnection(connectionString);
sqlConnection.Open();
}
catch (Exception e) {
sqlConnection = null;
throw new HttpException(
SR.GetString(SR.SQL_Services_Cant_connect_sql_database),
e);
}
return sqlConnection;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -