简体   繁体   English

使用流读取器捕获SQL Statemtnt C#返回的值

[英]Using stream reader to catch values returned by sql statemtnt c#

guys i have an SQL statement returning more than 1 value. 伙计们,我有一个返回超过1个值的SQL语句。 I am trying to use the StreamReader to get the values into an array as below 我正在尝试使用StreamReader将值放入数组,如下所示

 string sql = "select distinct COLUMN_NAME from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_NAME=' " + table + "' and CONSTRAINT_NAME like 'PK_%'";
            SqlConnection conn2 = new SqlConnection(cnstr.connectionString(cmbDatabase.Text));
            SqlCommand cmd_server2 = new SqlCommand(sql);
            cmd_server2.CommandType = CommandType.Text;
            cmd_server2.Connection = conn2;
            conn2.Open();

            //reader_sql = new StreamReader();
            SqlDataReader reader_sql = null;
            string[] colName = new string[200];
            reader_sql = cmd_server2.ExecuteReader();
            while (reader_sql.Read());

            for (int rr = 0; rr < 20; rr++)
            {
                colName[rr] = reader_sql["COLUMN_NAME"].ToString();
            }

It is not working, what am I doing wrong guys ? 它不起作用,我在做什么错的家伙?

You've got a stray ; 你流浪了; turning your while into a tight loop, so instead try: 将您的while变成一个紧缩循环,所以请尝试:

        while (reader_sql.Read())

        for (int rr = 0; rr < 20; rr++)
        {
            colName[rr] = reader_sql["COLUMN_NAME"].ToString();
        }

Perhaps you should remove the semicolon at the end of Read 也许您应该在阅读结束时删除分号

while (reader_sql.Read())
{
    for (int rr = 0; rr < 20; rr++)
       colName[rr] = reader_sql["COLUMN_NAME"].ToString();
}    

However, if your intention is to retrieve the columns belonging to the primary key, your code is wrong because you add 20 times the same primary key column, then repeat the same for the remaining columns ending with an array of 20 strings all equals to the last column in the primary key set. 但是,如果您打算检索属于主键的列,则您的代码是错误的,因为您将相同的主键列相加20倍,然后对其余的列重复相同的操作,以20个字符串组成的数组都等于主键集中的最后一列。 I think you should change your code to use a List(Of String) instead of a fixed length array and let the reader loop correctly on the primary key columns retrieved 我认为您应该更改代码以使用List(Of String)而不是固定长度的数组,并让阅读器在检索到的主键列上正确循环

List<string> pks = new List<string>();
while (reader_sql.Read())
{
    pks.Add(reader_sql["COLUMN_NAME"].ToString());
}    

EDIT: I have just noticed that your query contains a space before the table name. 编辑:我刚刚注意到您的查询表名称前包含一个空格。 The string concatenation then produces an invalid table name, the query is syntactically right but doesn't return any data 然后,字符串串联产生无效的表名,该查询在语法上正确,但不返回任何数据

string sql = "select distinct COLUMN_NAME from INFORMATION_SCHEMA.KEY_COLUMN_USAGE " + 
             "where TABLE_NAME='" + table + "' and CONSTRAINT_NAME like 'PK_%'";
                                ^ space removed here

And while you are at it, remove the string concatenation and use a parameterized query..... 在使用它的同时,删除字符串连接并使用参数化查询.....

string sql = "select distinct COLUMN_NAME from INFORMATION_SCHEMA.KEY_COLUMN_USAGE " + 
             "where TABLE_NAME=@tName and CONSTRAINT_NAME like 'PK_%'";
SqlCommand cmd_server2 = new SqlCommand(sql, connection);
connection.Open();
cmd_server2.Parameters.AddWithValue("@tName", table);

You get the exception because 你之所以例外,是因为

while (reader_sql.Read());

should be 应该

while (reader_sql.Read())
{
    for (int rr = 0; rr < 20; rr++)
     {
         colName[rr] = reader_sql["COLUMN_NAME"].ToString();
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM