Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从RESTORE LABELONLY操作中检索值_C#_Sql_Sql Server_Sql Server 2012 - Fatal编程技术网

C# 从RESTORE LABELONLY操作中检索值

C# 从RESTORE LABELONLY操作中检索值,c#,sql,sql-server,sql-server-2012,C#,Sql,Sql Server,Sql Server 2012,我要检索以下查询的结果值: RESTORE LABELONLY FROM DISK = 'C:\folder\db-backup.bak'; 在做了一些搜索之后,我尝试: string connection = @"data source=.\SQLExpress;initial catalog=DB;persist security info=False;user id=sa;password=12356;MultipleActiveResultSets=True;App=EntityFra

我要检索以下查询的结果值:

RESTORE LABELONLY FROM DISK = 'C:\folder\db-backup.bak';
在做了一些搜索之后,我尝试:

string connection = @"data source=.\SQLExpress;initial catalog=DB;persist security info=False;user id=sa;password=12356;MultipleActiveResultSets=True;App=EntityFramework";
ExecuteSQL(connection , @"RESTORE LABELONLY FROM DISK = 'C:\folder\db-backup.bak'");
并且,ExecuteSQL是:

private void ExecuteSQL(string ConnString, string sqlText)
    {
        string result = "";
        using (SqlConnection sqlCon = new SqlConnection(ConnString))
        {
            sqlCon.Open();
            using (SqlCommand sqlCom = sqlCon.CreateCommand())
            {
                sqlCom.CommandType = CommandType.Text;
                sqlCom.CommandText = sqlText;
                sqlCom.CommandTimeout = 0;
                try
                {
                    string str = Convert.ToString(sqlCom.ExecuteScalar());
                }
                catch (Exception e)
                {
                    result = e.Message;
                }
            }
        }
    }
但是。。str总是空的

SQL Server Management Studio中的查询结果为:

我想要的是如何检索MediaSetID字段?

介绍了
仅还原标签操作:

结果集

RESTORE Label的结果集仅包含一行 这个信息

MediaName nvarchar(128)媒体的名称


因此,您必须使用方法并处理返回的列(或将其加载到
DataTable
并执行相同的操作)。

您应该使用
ExecuteReader()
,因为您所说的命令返回非标量值。根据您发布的SSMS结果图像,
RESTORE
命令的第一列值为
NULL
,因此可能会得到空字符串

sqlCom.ExecuteReader()

@Eugebe Podskal非常感谢你的帮助