RJ.*_*RJ. 1 c# asp.net byte megabyte
基本上我正在创建一个复制应用程序,我只需要弄清楚数据库大小以及D:\驱动器上有多少可用空间.
如果数据库大小大于可用空间,那么我需要提醒用户.
这是我到目前为止:
首先,我找出D驱动器中有多少可用空间.
DriveInfo di = new DriveInfo(@"D:\");
if (di.IsReady)
{
freeSpace = di.TotalFreeSpace;
}
Run Code Online (Sandbox Code Playgroud)
然后我得到了我要复制的数据库的大小:
dbSize = Database.GetDatabaseSize(ddlPublisherServer.Text, ddlPublisherDatabase.Text);
Run Code Online (Sandbox Code Playgroud)
这是获取数据库大小的方法.我不知道是否有更好的方法来做到这一点,但是大小带有"MB"字符串,所以我需要删除它.
public static long GetDatabaseSize(string server, string database)
{
string finalConnString = Properties.Settings.Default.rawConnectionString.Replace("<<DATA_SOURCE>>", server).Replace("<<INITIAL_CATALOG>>", database);
using (SqlConnection conn = new SqlConnection(finalConnString))
{
using (SqlCommand cmd = new SqlCommand("sp_spaceused", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
cmd.ExecuteNonQuery();
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataSet ds = new DataSet())
{
da.Fill(ds);
var spaceAvailable = ds.Tables[0].Rows[0][1].ToString();
string freeSpace = spaceAvailable.Remove(spaceAvailable.Length - 3, 3);
return Convert.ToInt64(freeSpace);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我现在的问题是 -
如何将字节转换为兆字节,以便我可以比较db大小和磁盘可用空间?
这就是我所拥有的,但它是兆字节和字节,所以我需要在这里进行转换.
if (dbSize > freeSpace)
{
ClientScript.RegisterStartupScript(this.GetType(), "Insufficient Space", "alert('The database size is greater than the available space on the drive. Please make some room for the database in D drive of the subscriber server.');", true);
}
Run Code Online (Sandbox Code Playgroud)
字节数到兆字节= Bytes / (1024 * 1024)
兆字节到字节=Megabytes * (1024 * 1024.0)
一定要考虑整数除法,因此1024.0使用浮点数.
| 归档时间: |
|
| 查看次数: |
3219 次 |
| 最近记录: |