zoukankan      html  css  js  c++  java
  • SQL2012之FileTable与C#的联合应用

    关于FileTable是什么,请猛击如下链接:http://technet.microsoft.com/zh-cn/library/ff929144(v=SQL.110).aspx;如您已知道,请跳过。

    关于如何启用FileTable功能,请猛击如下链接:http://technet.microsoft.com/zh-cn/library/gg509097.aspx

    关于如何创建、修改和删除FileTable,请猛击如下链接:http://technet.microsoft.com/zh-cn/library/gg509088.aspx

    欲了解更多FileTable特性及支持,请点击以上链接页面页底的相关任务部分(如图):

    在上面的截图中用红色框框圈出的部分,将是本文的重点部分。在新建FileTable后,右击并选择“浏览FileTable目录”,如下图:

    没错,也许您已想到,这就是所谓的FileTable支持Windows I/O API操作,这样我们就可以利用System.IO命名空间下的FileInfo类中的Move、Copyto等函数实现利用FileTable上传文件的功能。下面我们将实现如何在C#程序中实现这点:

    1.首先要获取到FileTable表的RootPath(也就是上图中圈出的网络路径),就这一点我们可以T-SQL中的函数FileTableRootPath(),比如说我建的FileTable名为MyFirstFileTable,那么T-SQL如下便可获取其RootPath:

    1 select FileTableRootPath('MyFirstFileTable') as Path

    2.RootPath获取后我们就可以在C#程序中实现了,如下代码:

     1         /// <summary>
     2         /// Upload files by FileTable
     3         /// </summary>
     4         /// <param name="strFilePath">the path of target file</param>
     5         public void UploadbyFileTable(string strFilePath)
     6         {
     7             if (!File.Exists(strFilePath))
     8                 return;
     9             FileInfo file = new FileInfo(strFilePath);
    10             string strDBConnection = "Server=your server name/IP,initial catalog=your database name,User id=your id,passord=your password";
    11             string strFileTableName = "your FileTable's name";
    12             string strRootPath = GetFileTableRootPath(strDBConnection, strFileTableName);
    13             if (File.Exists(Path.Combine(strRootPath, file.Name)))
    14             {
    15                 return;
    16             }
    17             file.MoveTo(Path.Combine(strRootPath, file.Name));
    18         }
    19 
    20         /// <summary>
    21         /// Get your Filetable's RootPath
    22         /// </summary>
    23         /// <param name="strDBConnection">your DB connection string</param>
    24         /// <param name="strFileTableName">your FileTable name</param>
    25         /// <returns>your Filetable's RootPath</returns>
    26         public string GetFileTableRootPath(string strDBConnection,string strFileTableName)
    27         {
    28             string strRootPath = string.Empty;
    29             try
    30             {
    31                 SqlConnection sqlCon = new SqlConnection(strDBConnection);
    32                 sqlCon.Open();
    33                 StringBuilder sb = new StringBuilder();
    34                 sb.AppendFormat("select FileTableRootPath('{0}') as [path]", strFileTableName);
    35                 SqlCommand sqlCmd = new SqlCommand(sb.ToString(), sqlCon);
    36                 SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
    37                 DataTable dt = new DataTable();
    38                 sqlDa.Fill(dt);
    39                 strRootPath = dt.Rows[0][0].ToString();
    40                 sqlDa.Dispose();
    41                 sqlCmd.Dispose();
    42                 sqlCon.Close();
    43             }
    44             catch(Exception e)
    45             {
    46                 throw e;
    47             }
    48             return strRootPath;
    49         }
    View Code

    这样就实现了利用FileTable上传文件的功能。到最后,您发现是不是很简单呢?
    欢迎您积极提出建议和疑问,我会尽自己的力量给您满意的答复,O(∩_∩)O谢谢

  • 相关阅读:
    英语阅读重点单词总结
    Redis 应用
    Python 列表[::-1]翻转
    golang数据类型
    golang变量
    k8s 容器控制台日志收集
    css显示模式
    css选择器
    css样式引入
    GIL锁
  • 原文地址:https://www.cnblogs.com/tongyinaocan/p/3216373.html
Copyright © 2011-2022 走看看