将DataTable转换为字符串类型

发布于 2024-10-06 18:35:37 字数 5328 浏览 6 评论 0原文

我有一个像这样的函数

       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'
                                                     AND
                                                       TABLE_NAME <> 'dtProperties'
                                                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;



            }

然后我想将返回类型作为字符串而不是DataTable,所以我这样做了..

  public string GetAllPrimaryKeyTables(string ConnectionString)
        {
            string result = string.Empty;

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

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

                    // 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);
                    using(StringWriter sw = new StringWriter())
                    {
                        dtListOfPrimaryKeyTables.WriteXml(sw);
                        result = sw.ToString();
                    }
                }
                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 result;
        }

但这并没有返回它应该返回的字符串...

你能指导我指出需要修改的地方吗?

I have a function like this

       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'
                                                     AND
                                                       TABLE_NAME <> 'dtProperties'
                                                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;



            }

Then I wanted to get the return type as string and not as DataTable so I did this..

  public string GetAllPrimaryKeyTables(string ConnectionString)
        {
            string result = string.Empty;

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

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

                    // 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);
                    using(StringWriter sw = new StringWriter())
                    {
                        dtListOfPrimaryKeyTables.WriteXml(sw);
                        result = sw.ToString();
                    }
                }
                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 result;
        }

But this is not returning the string which it should ...

Can you direct me to the point that required modifications.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

王权女流氓 2024-10-13 18:35:37

使用 StringBuilder 而不是 StringWriter,然后您可以使用 WriteWriteLine 方法。

Instead of a StringWriter, use a StringBuilder, then you can use the Write and WriteLine methods.

攒一口袋星星 2024-10-13 18:35:37

如果您想要一个逗号分隔的列表,您可以使用如下所示的内容。

private string GetList(ds)
{
    var result = string.Empty;
    for(int i = 0; i < ds.Rows.Count; i++)
    {
         result += ds.Row[i][0] + ",";
    }
    result = result.Trim(',');
    return result;
}

希望这有帮助。

If you want a comma seperated list, you could use something like below.

private string GetList(ds)
{
    var result = string.Empty;
    for(int i = 0; i < ds.Rows.Count; i++)
    {
         result += ds.Row[i][0] + ",";
    }
    result = result.Trim(',');
    return result;
}

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文