从SQL Server复制字符串中的列值 [英] copying column value from sql server in a string

查看:95
本文介绍了从SQL Server复制字符串中的列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

在这里,我编写了一个代码来复制字符串中的所有列值,但仅复制第一列值

 如果(rdr.HasRows)
               {
                   StringBuilder sb =  StringBuilder();
                   字符串 a;
                    while (rdr.Read())
                   {
                       sb.Append(((((span class ="code-keyword"> string )rdr [" ]).ToString());
                       sb.Append(" );
                       一个= sb.ToString();
                        for ( int  i =  0 ; i <  module_name_menustrip.Length; i ++)
                       {
                           module_name_menustrip [i] = a;
                           MessageBox.Show(module_name_menustrip [i]);
                           sb.Length =  0 ;
                           count1 ++;

                       }

                   } 




您能告诉我错误是什么吗?

解决方案

您为什么不在SQL Server本身上执行此操作?在这里查看如何在SQL中连接列值.

将列值作为逗号分隔的字符串 [ ^ ]

我不确定您要在这里做什么,您的代码没有多大意义.
1)您有一个循环,遍历了从数据库返回的所有行.
2)您读取行值,并将其与换行符一起添加到StringBuilder中.
3)然后将其转换为字符串,然后输入新的循环.
4)在此循环中,使用字符串加载数组的所有元素,并通过重置长度来终止StringBuilder.

为什么要使用StringBuilder?您的代码等效于:

 如果(rdr.HasRows)
{
     while (rdr.Read())
    {
         for ( int  i =  0 ; i <  module_name_menustrip.Length; i ++)
        {
            module_name_menustrip [i] =((字符串)rdr [" ]).ToString()+   \ n";
            MessageBox.Show(module_name_menustrip [i]);
            count1 ++;
        }
    }


您仅连接一个列值,即modname_vc.您需要做的是循环到数据读取器的所有列并获取它们的值.


hi to all!

here i wrote a code to copy all column value in a string but its copying only first column value

if (rdr.HasRows)
               {
                   StringBuilder sb = new StringBuilder();
                   string a;
                   while (rdr.Read())
                   {
                       sb.Append(((string)rdr["modname_vc"]).ToString());
                       sb.Append("\n");
                       a = sb.ToString();
                       for (int i = 0; i < module_name_menustrip.Length; i++)
                       {
                           module_name_menustrip[i] = a;
                           MessageBox.Show(module_name_menustrip[i]);
                           sb.Length = 0;
                           count1++;

                       }

                   }




can u please tell me whats the error

解决方案

Why don''t you do this at SQL Server itself? Look here on how to concat column values in SQL.

Get Column values as comma seperated string [^]


I''m not exactly sure what you are trying to do here, your code doesn''t make a lot of sense.
1) You have a loop, through all of the rows returned from your database.
2) You read the row value, and add it to the StringBuilder, along with a newline.
3) You then convert it to a string, and enter a new loop.
4) In this loop, you load all the elements of an array with the string, and kill the StringBuilder, by resetting the length.

Why have the StringBuilder at all? You code is the equivalent of:

if (rdr.HasRows)
{
    while (rdr.Read())
    {
        for (int i = 0; i < module_name_menustrip.Length; i++)
        {
            module_name_menustrip[i] = ((string)rdr["modname_vc"]).ToString() + "\n";
            MessageBox.Show(module_name_menustrip[i]);
            count1++;
        }
    }


You are only concatenating one column value, which is modname_vc. What you need to do is to loop to all the columns of your data reader and get their values.


这篇关于从SQL Server复制字符串中的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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