的大小,我知道如何在SQL改變大小的數據庫(在SQL Server 2005 Express版本)如何更改數據庫
ALTER DATABASE Accounting
MODIFY FILE
(NAME = 'Accounting',
SIZE = 25)
如何改變大小的數據庫期運用C#?
的大小,我知道如何在SQL改變大小的數據庫(在SQL Server 2005 Express版本)如何更改數據庫
ALTER DATABASE Accounting
MODIFY FILE
(NAME = 'Accounting',
SIZE = 25)
如何改變大小的數據庫期運用C#?
通過ExecuteNonQuery
命令提交DDL:
mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"ALTER DATABASE Accounting MODIFY FILE (NAME = 'Accounting', SIZE = 25) ";
mySqlConnection.Open();
int result = mySqlCommand.ExecuteNonQuery();
mySqlConnection.Close();
類似的例子可以在這裏找到(顯示爲快照隔離相關的問題,但ideais基本相同):
下面的例子會給你一個更好的概述。定義參數後,您可以傳遞一些不必要的東西,這些東西必須是靜態的。使用using
將確保一切都妥善處理/關閉(和/或重新使用,如果有必要)。
public const string sqlDataConnectionDetails = "Data Source=YOUR-SERVER;Initial Catalog=YourDatabaseName;Persist Security Info=True;User ID=YourUserName;Password=YourPassword";
public static void ChangeDatabaseSize(int databaseSize) {
const string preparedCommand = @"
ALTER DATABASE Accounting
MODIFY FILE
(NAME = 'Accounting', SIZE = @size)
";
using (var varConnection = SqlConnectOneTime(sqlDataConnectionDetails))
using (SqlCommand sqlWrite = new SqlCommand(preparedCommand, varConnection)) {
sqlWrite.Parameters.AddWithValue("@size", databaseSize);
sqlWrite.ExecuteNonQuery();
}
}
這是一種支持方法,可以在每次要寫入/讀取數據庫時輕鬆建立連接。
public static SqlConnection SqlConnectOneTime(string varSqlConnectionDetails) {
SqlConnection sqlConnection = new SqlConnection(varSqlConnectionDetails);
try {
sqlConnection.Open();
} catch {
DialogResult result = MessageBox.Show(new Form {
TopMost = true
}, "No connection to database. Do you want to retry?", "No connection (000001)", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
if (result == DialogResult.No) {
if (Application.MessageLoop) {
// Use this since we are a WinForms app
Application.Exit();
} else {
// Use this since we are a console app
Environment.Exit(1);
}
} else {
sqlConnection = SqlConnectOneTime(varSqlConnectionDetails);
}
}
return sqlConnection;
}