我是初学者,做个查询的功能,用一个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!
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.StoredProcedure;这才是调用存储过程
conn.ConnectionString = ConnStr;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = strqry;
cmd.CommandType = CommandType.Text
我看你代码是直接执行的 Sql语句, sql语句中指明了 where (FruitName='" + TextBox1.Text + "')";条件, 当然只返回符合条件的一条记录了
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(); }