[英]How to change size of database
I know how to change size database on SQL (in SQL Server 2005 Express Edition) 我知道如何在SQL上更改大小数据库(在SQL Server 2005 Express Edition中)
ALTER DATABASE Accounting
MODIFY FILE
(NAME = 'Accounting',
SIZE = 25)
How to change size database useing C#? 如何使用C#更改大小数据库?
Submit the DDL via an ExecuteNonQuery
command: 通过ExecuteNonQuery
命令提交DDL:
mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"ALTER DATABASE Accounting MODIFY FILE (NAME = 'Accounting', SIZE = 25) ";
mySqlConnection.Open();
int result = mySqlCommand.ExecuteNonQuery();
mySqlConnection.Close();
Similar examples can be found here (showing issues related to snapshot isolation, but the ideais basically the same): 可以在此处找到类似的示例(显示与快照隔离有关的问题,但是思想基本相同):
http://msdn.microsoft.com/en-us/library/tcbchxcb.aspx http://msdn.microsoft.com/en-us/library/tcbchxcb.aspx
Following example will give you a better overview. 以下示例将为您提供更好的概述。 Having defined parameters you can pass some stuff that not necessary has to be static. 定义了参数后,您可以传递一些不必要的东西,这些东西不必是静态的。 Using using
will make sure everything is disposed/closed properly (and or reused if necessary). 使用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();
}
}
This is supporting method that makes it easy to establish connection everytime you want to write/read something to database. 这是一种支持方法,使您每次想向数据库写入/读取内容时都可以轻松建立连接。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.