gpt4 book ai didi

c# - 从 RESTORE LABELONLY 操作中检索值

转载 作者:太空宇宙 更新时间:2023-11-03 23:05:10 24 4
gpt4 key购买 nike

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

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 中的查询结果为:

enter image description here

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

最佳答案

MSDN说到 RESTORE LABELONLY 操作:

Result Sets

The result set from RESTORE LABELONLY consists of a single row with this information.

MediaName nvarchar(128) Name of the media.

...

所以你必须使用ExecuteReader方法并处理返回的 reader 中的列(或将其加载到 DataTable 并执行相同操作)。

关于c# - 从 RESTORE LABELONLY 操作中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41505604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com