如何在控制台应用程序中提供地图网络驱动器路径 [英] How to give map network drive path in console application?

查看:67
本文介绍了如何在控制台应用程序中提供地图网络驱动器路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个控制台应用程序来备份Sql数据库,并将其存储到给定的路径,

当我将此路径作为本地驱动器时,它是完美的,但是当我给地图网络路径时驱动路径,意味着我想在其他网络电脑中保存我的备份,它显示错误



无法打开备份设备

' Z:\Smart_Tracker_03_01_2014_12_44_21_F_1_0.bak。操作系统

错误3(系统找不到指定的路径。)。备份数据库

异常终止。




以下是我的代码:



 Success_backup = Execute_Query_Master_Database(  BACKUP DATABASE + DatabaseName +   TO DISK =' + Backup_FileName +   '); 
Success_Alter_DB = Execute_Query_Master_Database( ALTER DATABASE + DatabaseName + SET MULTI_USER);




public static bool Execute_Query_Master_Database( string strQuery)
{
SqlConnection Conn = ;
SqlCommand _command;

{

string connectionstring = @ 数据源= + servername + ;初始目录= 'master'; User Id =' + UserName + '; Password =' + PassWord + ';
Conn = new SqlConnection(connectionstring);

Conn.Open();
_command = new SqlCommand();
_command.Connection = Conn;
_command.CommandType = CommandType.Text;
_command.CommandText = strQuery;
_command.CommandTimeout = 0 ;



_command.ExecuteNonQuery();

return true ;


}
catch (例外情况)
{

控制台.WriteLine( 函数中出错 - Execute_Scaler_Master_Database());
Console.WriteLine( 错误消息如下:);
Console.WriteLine( ================= ==========);
Console.WriteLine(ex.Message);
Console.WriteLine();

return false ;
}
最后
{
_command = null ;
Conn.Close();

}
}



  请注意,我传递了6个参数







  string  [] args =  new   string  [ 6 ]; 
args [ 0 ] = VARUN- PC; // Sql Name
args [ 1 ] = Smart_Tracker; // 数据库名称
args [ 2 ] = sa; // 用户名
args [ 3 ] = admin @ 12; // 密码
args [ 4 ] = F; // 备份类型F表示此处完全备份
args [ 5 ] = @ Z:; // 路径存储数据库的位置,此处为地图驱动器

解决方案

您需要在系统上映射驱动器(而不是通过代码)。


您需要提供完整的位置。与 \\\\\\\\\\\\\\\\\\\\\

I made one console application which take backup of Sql database, and store it to given path,
It works perfect when i give this path as local Drive, But when i give path to map network drive path, means i want to save my backup in other network pc, it show error

Cannot open backup device
'Z:\Smart_Tracker_03_01_2014_12_44_21_F_1_0.bak'. Operating system
error 3(The system cannot find the path specified.). BACKUP DATABASE
is terminating abnormally.


Below is my code:

Success_backup = Execute_Query_Master_Database("BACKUP DATABASE " + DatabaseName + " TO DISK = '" + Backup_FileName + "' ");
Success_Alter_DB = Execute_Query_Master_Database("ALTER DATABASE " + DatabaseName + "  SET MULTI_USER ");
              



    public static  bool Execute_Query_Master_Database(string strQuery)
        {
            SqlConnection Conn = null;
            SqlCommand _command;
           
            {

                string connectionstring = @"Data Source=" + servername + ";Initial Catalog='master';User Id='" + UserName + "';Password='" + PassWord + "'";
                Conn = new SqlConnection(connectionstring);

                Conn.Open();
                _command = new SqlCommand();
                _command.Connection = Conn;
                _command.CommandType = CommandType.Text;
                _command.CommandText = strQuery;
                _command.CommandTimeout = 0;

              

                _command.ExecuteNonQuery();

                return true;

               
            }
            catch (Exception ex)
            {
              
                Console.WriteLine("Error occured in Function - Execute_Scaler_Master_Database()");
                Console.WriteLine("Error Message as below : ");
                Console.WriteLine("===========================");
                Console.WriteLine(ex.Message);
                Console.WriteLine();

                return false;
            }
            finally
            {
                _command = null;
                Conn.Close();
             
            }
        }


Kindly noted I pass 6 arguments




string[] args = new string[6];
           args[0] = "VARUN-PC"; //Sql Name
           args[1] = "Smart_Tracker"; //DataBase name
           args[2] = "sa"; // user Name
           args[3] = "admin@12"; // Password
           args[4] = "F"; // Backup type "F"mean full Backup here
           args[5] = @"Z:";  // path where to store database, here map drive

解决方案

You need to map the drive on the system (not via code).


You will need to provide complete location. As in \\serverName\Folder.


这篇关于如何在控制台应用程序中提供地图网络驱动器路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆