当前位置:编程学习 > C#/ASP.NET >>

为什么查询结果只返回了一项

我是初学者,做个查询的功能,用一个GridView控件,和一个Textbox控件,一个Button控件。查询的时返回的只有一项,比如我查TABLE1中的NAME项,GridView只显示了NAME这一项,其他表项末显示。不知道 是不是存储过程的问题?把代码贴出来,高手帮忙看看吧。多谢了!
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
   
    public partial class WebForm1 : System.Web.UI.Page
    {
        string ConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();       
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string strqry = "select FruitName from Fruit where (FruitName='" + TextBox1.Text + "')";
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConnStr;
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = strqry;
            cmd.CommandType = CommandType.Text;
            conn.Open();
            this.GridView1.DataSource = cmd.ExecuteReader();
            this.GridView1.DataBind();
            conn.Close();

         
        }
}
存储过程:
ALTER PROCEDURE DataExchange
(
@FruitName nchar(100)
)
AS
SELECT * FROM Fruit
WHERE FruitName=@FruitName

RETURN
SQL2008的版本。VS2008! --------------------编程问答-------------------- 高手帮下忙呀。。。 --------------------编程问答-------------------- 没见到你用存储过程啊
你where条件筛选过后,有多条记录返回 啊
--------------------编程问答-------------------- 不好意思搞错 了。。。。自己看错了。。。 --------------------编程问答-------------------- 你本来就是条件查询  是不是就是一条数据 --------------------编程问答-------------------- cmd.CommandType = CommandType.Text;
改成
cmd.CommandType = CommandType.StoredProcedure;这才是调用存储过程 --------------------编程问答--------------------  请问你在哪用到的存储过程,
conn.ConnectionString = ConnStr;
  SqlCommand cmd = conn.CreateCommand();
  cmd.CommandText = strqry;
  cmd.CommandType = CommandType.Text
我看你代码是直接执行的 Sql语句, sql语句中指明了 where (FruitName='" + TextBox1.Text + "')";条件, 当然只返回符合条件的一条记录了
--------------------编程问答-------------------- 还有cmd.CommandText 要写存储过程的名字就行了。 --------------------编程问答-------------------- 你可以使用SqlDataAdapter ,给你改了改
 

   protected void Button1_Click(object sender, EventArgs e)
        {
            string strqry = "select FruitName from Fruit where (FruitName='" + TextBox1.Text + "')";
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConnStr;
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = strqry;
            cmd.CommandType = CommandType.Text;
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd.CommandText;
            DataSet ds = new DataSet();
            sda.Fill(ds);
            if (ds !=null && ds.Tables[0].Rows.Count > 0)
            {
                this.GridView1.DataSource = ds.Tables[0].DefaultView;
                this.GridView1.DataBind();
            } 
            conn.Close(); 

        }
--------------------编程问答-------------------- 这个自己研究下 就会明白了 --------------------编程问答-------------------- “GridView只显示了NAME这一项,其他表项末显示” lz是不是符合条件查询的记录的显示所有字段啊 

string strqry = "select * from Fruit where (FruitName='" + TextBox1.Text + "')";
--------------------编程问答-------------------- 自己检查下,你只查询了Name这一个字段 --------------------编程问答-------------------- sql查询在数据库中查询结果是多条? --------------------编程问答-------------------- select FruitName from Fruit where (FruitName='" + TextBox1.Text + "')

因为你只查了FruitName 
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,