简体   繁体   English

SQL查询未获取所有数据C#/ ASP.net

[英]SQL query not getting all the data C#/ASP.net

My SQL-query is not getting information from multiple columns. 我的SQL查询没有从多个列中获取信息。 For testing I'm trying with 2, but I also tried getting all the data, both times it only gives me the first column it finds. 为了进行测试,我尝试使用2,但是我也尝试获取所有数据,但两次都只给我找到的第一列。

string connectionString = SqlDataSource1.ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
string insert = "SELECT [ProductName], [productSpace]  FROM [Products] WHERE [ProductId] = '" + NoDupes[z] + "'";

SqlCommand cmd1 = new SqlCommand(insert, connection); 
cmd1.Connection.Open();
Response.Write(cmd1.ExecuteScalar().ToString());

cmd1.Connection.Close();

From SqlCommand.ExecuteScalar method SqlCommand.ExecuteScalar方法

Executes the query, and returns the first column of the first row in the result set returned by the query. 执行查询,并返回查询返回的结果集中第一行的第一列。 Additional columns or rows are ignored. 其他列或行将被忽略。

If you want to return multiple values from your query, you need to use ExecuteReader method . 如果要从查询中返回多个值,则需要使用ExecuteReader方法

It is not clear what you want to write but as an example; 目前尚不清楚您要编写什么内容,仅举一个例子。

using(SqlDataReader reader = cmd1.ExecuteReader())
{
    while (reader.Read())
    {
        Response.Write(reader[0].ToString()); //This writes first column values for your all rows.
    }
}

SqlDataReader.Read method reads your query result rows by rows . SqlDataReader.Read方法 按行读取查询结果

You should use using statement to dispose your SqlConnection and SqlCommand as I did for SqlDataReader . 您应该像使用SqlDataReader一样使用using语句来处理SqlConnectionSqlCommand Like; 喜欢;

using(SqlConnection connection = new SqlConnection(connectionString))
using(SqlCommand cmd1 = connection.CreateCommand())
{
    ...
    ...
    connection.Open();
    using(SqlDataReader reader = cmd1.ExecuteReader())
    {
        while (reader.Read())
        {
            Response.Write(reader[0].ToString()); //This writes first column values for your all rows.
        }
    }
}

And more important, you should always use parameterized queries . 更重要的是,您应该始终使用参数化查询 This kind of string concatenations are open for SQL Injection attacks. 这类字符串连接对SQL注入攻击开放。

For example; 例如;

string insert = @"SELECT ProductName, productSpace FROM Products
                  WHERE ProductId = @id";
SqlCommand cmd1 = new SqlCommand(insert, connection);
cmd1.Parameters.AddWithValue("@id", NoDupes[z]);

As a final tip, you don't need to use square brackets [] for your all column or table names. 最后,您不需要在所有列或表名中使用方括号[] Use it if your database object name is reserved keyword or contain white space (of course both are not recomended). 如果您的数据库对象名称是保留关键字或包含空格(当然不建议同时使用两个空格),请使用它。

You need to use ExecuteReader and iterate through datareader 您需要使用ExecuteReader并通过datareader进行迭代

using (var dr= cmd.ExecuteReader())
    {
        while(dr.Read())
        {
            //Logic
        }
    }

ExecuteScalar executes the query, and returns the first column of the first row in the result set returned by the query. ExecuteScalar执行查询,并返回查询返回的结果集中第一行的第一列。 Additional columns or rows are ignored. 其他列或行将被忽略。

if u use 如果你用

ExecuteScalar():

will work with Non-Action Queries that contain aggregate functions. 将与包含聚合函数的非操作查询一起使用。 Return the first row and first column value of the query result. 返回查询结果的第一行和第一列的值。 Return type is object. 返回类型是对象。 Return value is compulsory and should be assigned to a variable of required type. 返回值是强制性的,应将其分配给所需类型的变量。

ExecuteReader():

will work with Action and Non-Action Queries (Select) Returns the collection of rows selected by the Query. 将与操作和非操作查询一起使用(选择)返回由查询选择的行的集合。 Return type is DataReader. 返回类型为DataReader。 Return value is compulsory and should be assigned to an another object DataReader. 返回值是强制性的,应将其分配给另一个对象DataReader。

SqlDataReader rdr= cmd1.ExecuteReader();
while (rdr.read())
{  
 Response.Write(rdr.GetValue().ToString().Trim());

}

try this 尝试这个

SqlDataReader reader = cmd1.ExecuteReader();
while (reader.Read())
{
     Response.Write((String.Format("{0}", reader[0]));
}

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

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