通过 Ado.net 连接将收缩命令发送到 Microsoft SQL 数据库文件

发布于 2024-08-26 20:56:01 字数 215 浏览 4 评论 0原文

如何对 ADO.NET 连接的数据库执行直接 SQL 命令? 我想发送一个DBCC SHRINKDATABASE到SQL服务器,以在大删除过程后压缩当前数据文件。函数ObjectContext::CreateQuery 在执行 DBCC 命令后返回解析器错误。是否有其他方法可以缩小数据库文件,或者有其他方法可以将 SQL 命令直接发送到 SQL Server?

How is it possible to execute a direct SQL command to an ADO.NET connected database?
I want to send a DBCC SHRINKDATABASE to the SQL server, to compress the current datafile after a big deletion process. The function ObjectContext::CreateQuery returns a parser error after the DBCC command. Is there any other way to shrink the database file, or another way to send the SQL command directly to the SQL Server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风月客 2024-09-02 20:56:01

我只是将其作为原始 SQL 发送:

using (SqlCommand command = new SqlCommand("DBCC SHRINKDATABASE(0)", connection))
{
    command.ExecuteNonQuery();
}

另一种方法是将 DBCC SHRINKDATABASE 放入存储过程中,然后从代码中调用该存储过程。

I'd just send this as raw SQL:

using (SqlCommand command = new SqlCommand("DBCC SHRINKDATABASE(0)", connection))
{
    command.ExecuteNonQuery();
}

Another way is to put the DBCC SHRINKDATABASE in a stored procedure and call the stored procedure from your code.

时光无声 2024-09-02 20:56:01

谢谢马克,我只需要弄清楚如何获取 SqlConnection。
这是完整的源代码(可以使用扩展方法写得更好一点):

    public ActionResult Optimize()
    {
        using (BXPartsEntities Entities = new BXPartsEntities())
        {

            System.Data.EntityClient.EntityConnection eConnection = Entities.Connection as System.Data.EntityClient.EntityConnection;

            eConnection.Open();

            var SqlConnection = eConnection.StoreConnection as SqlConnection;

            if (SqlConnection == null)
                throw new ArgumentException("StoreConnection shall be SQL Connection");

            using (SqlCommand command = new SqlCommand("DBCC SHRINKDATABASE(0)", SqlConnection))
            {
                command.ExecuteNonQuery();
            }

            eConnection.Close();
        }
        return Content("Done");
    }

thanks Mark, I just had to figure out how I can obtain the SqlConnection.
Here is the complete sourcecode (ok could be written a little bit nicer with an extension method):

    public ActionResult Optimize()
    {
        using (BXPartsEntities Entities = new BXPartsEntities())
        {

            System.Data.EntityClient.EntityConnection eConnection = Entities.Connection as System.Data.EntityClient.EntityConnection;

            eConnection.Open();

            var SqlConnection = eConnection.StoreConnection as SqlConnection;

            if (SqlConnection == null)
                throw new ArgumentException("StoreConnection shall be SQL Connection");

            using (SqlCommand command = new SqlCommand("DBCC SHRINKDATABASE(0)", SqlConnection))
            {
                command.ExecuteNonQuery();
            }

            eConnection.Close();
        }
        return Content("Done");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文