我一直在绞尽脑汁想弄清楚如何在 C# 中使用 SMO 从 Table 执行 SELECT 并将该值返回到字符串项。
我看过多篇关于如何在 C# 中运行 SQL 脚本的帖子,这不是我想做的。这是我到目前为止的代码
public static void GetDealerInfo()
{
Server databaseServer = new Server(dbServer);
try
{
databaseServer.ConnectionContext.LoginSecure = dbSecure;
databaseServer.ConnectionContext.Login = dbUser;
databaseServer.ConnectionContext.Password = dbPass;
databaseServer.ConnectionContext.Connect();
sDealerName = databaseServer.ConnectionContext.ExecuteWithResults("USE DATABASE Select DataValue from TABLE where KEYField = 'DealershipName'").ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
if (databaseServer.ConnectionContext.IsOpen)
{
databaseServer.ConnectionContext.Disconnect();
}
}
}
我还有一个名为 sDealerName 的字符串,这是我想要提取的地方,我得到的只是 sDealerName = System.Data.DataSet
谁能指出我正确的方向?
更新:
这是让它运行的代码,或者至少是对我有用的代码
try
{
databaseServer.ConnectionContext.LoginSecure = dbSecure;
databaseServer.ConnectionContext.Login = dbUser;
databaseServer.ConnectionContext.Password = dbPass;
databaseServer.ConnectionContext.DatabaseName = dbDatabase;
databaseServer.ConnectionContext.Connect();
DataSet dsName = databaseServer.ConnectionContext.ExecuteWithResults("Select DataValue from ABSetup where KEYField = 'DealershipName'");
sDealerName = dsName.Tables[0].Rows[0][0].ToString();
DataSet dsNum = databaseServer.ConnectionContext.ExecuteWithResults("Select DataValue from ABSetup where KEYField = 'ABOfficeCID'");
sDealerNumber = dsNum.Tables[0].Rows[0][0].ToString();
}