如何使用 C# 代码在 SQL Server 2008 R2 中启用文件流

发布于 2024-11-19 17:18:20 字数 390 浏览 3 评论 0原文

我为我的 wpf 应用程序创建了一个安装文件,它将在客户端计算机上安装 .net Framework 4 和 sql server 2008R2。

但是,在我的 C# 代码中运行 sql 脚本之前,我需要在我的计算机上的 Microsoft SQL Sever 2008 R2/配置工具中启用文件流。

我需要一种在 C# 代码中执行此操作的方法,而不是手动执行此操作。

我已尝试以下

操作

:从页面 http:// /www.mssqltips.com/tip.asp?tip=1838 但它不起作用。

谢谢并致以最诚挚的问候!

I have a setup file created for my wpf application which will install the .net framework 4 and sql server 2008R2 on a client machine.

However before running the sql script in my c# code I need to enable file stream in Microsoft SQL Sever 2008 R2/ Configuration tools on the my machine.

I need a way of doing it in c# code rather than doing it manually.

I have tried the following:

Enable FILESTREAM Feature Using Transact SQL (TSQL)

from page, http://www.mssqltips.com/tip.asp?tip=1838 but it didnt work.

Thanks and Best Regards!

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

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

发布评论

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

评论(2

盛夏已如深秋| 2024-11-26 17:18:21

通常,对于通过 .net 代码对 SQL Server 进行任何管理,您应该查看 SQL Server Management对象。它可能支持在数据库级别启用 FILESTREAM,但我不确定。

如果它有任何帮助,您至少可以创建一个启用新的 FILESTREAM FileGroup< /a> 通过 API。该 MSDN 页面上有一个示例,此外,如果您通过 google smo filestream 搜索,还可以找到一些博客示例。

Generally for any administration of SQL Server through .net code you should be looking at SQL Server Management Objects. It may support enabling FILESTREAM at the DB level, but I'm not sure.

If it's of any help you are however at least able to create a new FILESTREAM enabled FileGroup through the api. An example exists on that MSDN page, plus there are some blog samples out there if you google smo filestream.

我是男神闪亮亮 2024-11-26 17:18:20
  1. 调用此函数。

    private void EnableFileStream(string instanceName)
    {
        //sql 2008 - 计算机管理10,sql2012 - 计算机管理11
        字符串 SqlWmiPath = @"\\.\root\Microsoft\SqlServer\ComputerManagement10"; 
    
        string WqlSqlFs = "SELECT * FROM FileStreamSettings WHERE InstanceName='" + instanceName + "'";
    
        ManagementScope 范围 = new ManagementScope(SqlWmiPath);
        int arch = 环境.Is64BitOperatingSystem ? 64:32;
        scope.Options.Context.Add("__ProviderArchitecture", arch);
        范围.Connect();
    
        WqlObjectQuery wql = new WqlObjectQuery(WqlSqlFs);
        ManagementObjectSearcher 搜索器 = new ManagementObjectSearcher(范围, wql);
        ManagementObjectCollection moColl = searcher.Get();
    
        foreach(moColl 中的 ManagementObject m)
        {
            ManagementBaseObject param = m.GetMethodParameters("EnableFileStream");
            //0 - 关闭
            //1 - 启用事务sql
            //2 - 启用事务sql和流访问
            //3 - 启用transact sql和流访问以及远程流访问
            参数[“访问级别”] = 1; 
    
            param["共享名"] = 实例名;
            var output = m.InvokeMethod("EnableFileStream", param, null);
            如果(输出!= null)
            {
                uint retValue = (uint)output.GetPropertyValue("ReturnValue");
    
                //分析返回值
            }
        }
    }
    
  2. 重启instanceName对应的系统服务。
    如果instanceName是SQLEXPRESS,那么服务名称将为
    MSSQL$SQLEXPRESS。重新启动可以通过系统控制台或其他方式完成。

    3.1 sc 停止“MSSQL$SQLEXPRESS”

    3.2 等待停止(检查服务状态:sc query "MSSQL$SQLEXPRESS")..

    3.3 sc start "MSSQL$SQLEXPRESS"

  1. Call this function.

    private void EnableFileStream(string instanceName)
    {
        //sql 2008 - ComputerManagement10, sql2012 - ComputerManagement11
        string SqlWmiPath = @"\\.\root\Microsoft\SqlServer\ComputerManagement10"; 
    
        string WqlSqlFs = "SELECT * FROM FileStreamSettings WHERE InstanceName='" + instanceName + "'";
    
        ManagementScope scope = new ManagementScope(SqlWmiPath);
        int arch = Environment.Is64BitOperatingSystem ? 64 : 32;
        scope.Options.Context.Add("__ProviderArchitecture", arch);
        scope.Connect();
    
        WqlObjectQuery wql = new WqlObjectQuery(WqlSqlFs);
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, wql);
        ManagementObjectCollection moColl = searcher.Get();
    
        foreach (ManagementObject m in moColl)
        {
            ManagementBaseObject param = m.GetMethodParameters("EnableFileStream");
            //0 - off
            //1 - enable transact sql
            //2 - enable transact sql and stream acess
            //3 - enable transact sql and stream acess and remote stream access
            param["AccessLevel"] = 1; 
    
            param["ShareName"] = instanceName;
            var output = m.InvokeMethod("EnableFileStream", param, null);
            if (output != null)
            {
                uint retValue = (uint)output.GetPropertyValue("ReturnValue");
    
                //analyze return value
            }
        }
    }
    
  2. Restart system service corresponding to the instanceName.
    If instanceName is SQLEXPRESS, then service name will be
    MSSQL$SQLEXPRESS. Restarting can be done by system console, or by another way.

    3.1 sc stop "MSSQL$SQLEXPRESS"

    3.2 Wait until it is stopped (check service state: sc query "MSSQL$SQLEXPRESS")..

    3.3 sc start "MSSQL$SQLEXPRESS"

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