需要将DataTable返回类型转换为字符串 [英] Need to convert the DataTable return type to string

查看:250
本文介绍了需要将DataTable返回类型转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数可以将具有主键的表的列表返回到数据表中,但是现在需要的是在字符串返回类型中获取表列表。



我的方法如下:

  public DataTable GetAllPrimaryKeyTables 
(string localServer,string userName,string password,string selectedDatabase )
{

//创建datatable
DataTable dtListOfPrimaryKeyTables = new DataTable(tableNames);

SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder();
objConnectionString.DataSource = localServer; ;
objConnectionString.UserID = userName;
objConnectionString.Password = password;
objConnectionString.InitialCatalog = selectedDatabase;

//查询以选择主键表。
string selectPrimaryKeyTables = @SELECT
TABLE_NAME
AS
TABLES
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE
CONSTRAINT_TYPE ='PRIMARY KEY'
ORDER BY
TABLE_NAME;

//将您的SqlConnection和SqlCommand放入使用块!
使用(SqlConnection sConnection = new SqlConnection(objConnectionString.ConnectionString))
使用(SqlCommand sCommand = new SqlCommand(selectPrimaryKeyTables,sConnection))
{
try
{
//创建dataadapter对象
SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectPrimaryKeyTables,sConnection);

//填写datatable - 不需要打开连接,SqlDataAdapter将全部执行
//(并在完成后再次关闭)
sDataAdapter.Fill(dtListOfPrimaryKeyTables);

}
catch(Exception ex)
{
//所有异常都在EventLog中处理和写入。
EventLog log = new EventLog(Application);
log.Source =MFDBAnalyser;
log.WriteEntry(ex.Message);
}
}

//将数据表返回给调用者
return dtListOfPrimaryKeyTables;
}

但是现在我想在下面的函数中调用这个逻辑...我已经尝试了,但没有完成。

  public class PrimaryKeyChecker:IMFDBAnalyserPlugin 
{
public string RunAnalysis (string ConnectionString)
{
returnstring;
}
}

我需要将函数的returnt类型调整为string类型和在RunAnalysis方法中涵盖的整个逻辑



你们可以帮助我!!!

解决方案

  using System.Linq; 
...

public string GetAllPrimaryKeyTableNames(string localServer,string userName,string password,string selectedDatabase){
DataTable table = GetAllPrimaryKeyTables(localServer,userName,password,selectedDatabase);

return string.Join(,,table.AsEnumerable()。选择(r => r.ItemArray [0])ToArray());
}

这将返回一个包含用逗号分隔的表名的字符串。 p>

编辑



我在您的问题中看到有您的方法名为RunAnalysis。随意更改我的答案中的方法名称,无论您需要什么,以及参数。重要的部分是string.Join与LINQ的使用。


I have a function to return the list of tables having primary key in a datatable, but now the need is to get the table list in the string return type.

My method is as follows:

public DataTable GetAllPrimaryKeyTables 
   (string localServer, string userName, string password, string selectedDatabase)
{

    // Create the datatable 
    DataTable dtListOfPrimaryKeyTables = new DataTable("tableNames");

    SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder();
    objConnectionString.DataSource = localServer; ;
    objConnectionString.UserID = userName;
    objConnectionString.Password = password;
    objConnectionString.InitialCatalog = selectedDatabase;

    // Query to select primary key tables.
    string selectPrimaryKeyTables = @"SELECT 
                                           TABLE_NAME
                                          AS
                                           TABLES
                                        FROM 
                                           INFORMATION_SCHEMA.TABLE_CONSTRAINTS
                                       WHERE 
                                           CONSTRAINT_TYPE = 'PRIMARY KEY'
                                    ORDER BY
                                           TABLE_NAME";

    // put your SqlConnection and SqlCommand into using blocks! 
    using(SqlConnection sConnection = new SqlConnection(objConnectionString.ConnectionString))
    using(SqlCommand sCommand = new SqlCommand(selectPrimaryKeyTables, sConnection))
    {
        try
        {
            // Create the dataadapter object 
            SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectPrimaryKeyTables, sConnection);

            // Fill the datatable - no need to open the connection, the SqlDataAdapter will do that all by itself  
            // (and also close it again after it is done) 
            sDataAdapter.Fill(dtListOfPrimaryKeyTables);

        }
        catch(Exception ex)
        {
            //All the exceptions are handled and written in the EventLog. 
            EventLog log = new EventLog("Application");
            log.Source = "MFDBAnalyser";
            log.WriteEntry(ex.Message);
        }
    }

    // return the data table to the caller 
    return dtListOfPrimaryKeyTables;
}

But now I want this logic to be called in the function below...I have tried but it is not done.

public class PrimaryKeyChecker : IMFDBAnalyserPlugin
{
    public string RunAnalysis(string ConnectionString)
    {
        return "string";
    }
}

I need to adjust the returntype of the function to string type and the whole logic to be covered in the RunAnalysis method

Would you guys please help me!!!

解决方案

using System.Linq;
...

public string GetAllPrimaryKeyTableNames(string localServer, string userName, string password, string selectedDatabase) {
    DataTable table = GetAllPrimaryKeyTables(localServer, userName, password, selectedDatabase);

    return string.Join(",", table.AsEnumerable().Select(r => r.ItemArray[0]).ToArray());
}

This will return a string containing your table names separated by a comma.

EDIT

I see in your question you have your method named RunAnalysis. Feel free to change the method name in my answer to whatever you need it to be, as well as the parameters. The important part is the string.Join usage with LINQ.

这篇关于需要将DataTable返回类型转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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