C# SMO Select from Database

旧城冷巷雨未停 提交于 2019-12-24 19:08:28

问题


I have been racking my brain trying to figure out how to execute a SELECT from Table using SMO in C# and returning that value to a string item.

I have seen multiple posts of how I can run a SQL script from within C# which is not what I want to do. Here is the code I have so far

 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();
            }
        }
    }

I also have a string called sDealerName which is where I want to pull, all I am getting is sDealerName = System.Data.DataSet

Can anyone point me in the correct direction?

UPDATE:

Here is the code to get it going or at least what worked for me

 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();
        }

回答1:


Change your code to:

DataSet ds = databaseServer.ConnectionContext.ExecuteWithResults("Select DataValue from TABLE where KEYField = 'DealershipName'");

The "USE DATABASE;", first, you may not need it. Second it, if you mean "USE MyDatabaseName;" , try it with a semi colon after the name.

More important to your question : then do a

Console.Writeline (ds.GetXml );

You'll then "see" the DataSet, the DataTable, the row inside the DataTable from which to "pluck" your scalar value.

string value = string.Empty;

if(null!=ds) {
if(null!=ds.Tables) {
if(ds.Tables.Count > 0) {
if(null!=ds.Tables[0].Rows) {
if(ds.Tables[0].Rows.Count > 0) {
if(null!=ds.Tables[0].Rows[0].Columns){
if(ds.Tables[0].Rows[0].Columns.Count > 0)
{
value = ds.Tables[0].Rows[0].Columns[0].Value;
}}}}}}}

"Count" may be "Length", I'm going from memory.

My code is untested from memory, so take it with a grain of salt.




回答2:


You're calling ToString() on the object instance which is why you're getting the fullly qualified type name.

The value you're looking for will be inside a DataTable object within the DataSet. Run you're code again and break on the sDealerName line. Then using the magnifying glass tool click on that to open the dataset viewer and you'll be able to figure the rest out from there.



来源:https://stackoverflow.com/questions/16468347/c-sharp-smo-select-from-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!