A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张迁 中级黑马   /  2013-5-11 14:10  /  1672 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

单击按钮钮实现数据库查询操作,可是运行没有反应求大牛帮助
  1. protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.         conn.Open();
  4.         String str = "select * from NCP_info where NCP_name = '" + TextBox1.Text.ToString() + "'";
  5.         try
  6.         {
  7.             SqlDataAdapter da = new SqlDataAdapter(str , conn);
  8.             DataSet ds = new DataSet();
  9.             da.Fill(ds);
  10.             GridView1.DataSource = ds.Tables[0];
  11.          
  12.         }
  13.         catch (SystemException ee)
  14.         {
  15.             Response.Write("<script>alert('错误!"+ee.Message +"');</script>");
  16.         }
  17.         finally {
  18.             conn.Close();
  19.             conn.Dispose();
  20.         }
  21.     }
复制代码

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

9 个回复

倒序浏览
da.fill(ds,"一个命名的表明")    可以改成
da.Fill(ds,"info");

GridView1.DataSource = ds.Tables[0];
这就可以了哇!!!
以上格式为
da.Fill(ds,"   表名字  ");
dgv.DataSource=ds.Tables[0];或者dgv.DataSource=ds.Tables["  表名字  "] ;    注意,里面的标点要英文的,

protected void Button1_Click(object sender, EventArgs e)

02.    {

03.        conn.Open();

04.        String str = "select * from NCP_info where NCP_name = '" + TextBox1.Text.ToString() + "'";

05.        try

06.        {

07.            SqlDataAdapter da = new SqlDataAdapter(str , conn);

08.            DataSet ds = new DataSet();

09.            da.Fill(ds);

10.            GridView1.DataSource = ds.Tables[0];

11.         

12.        }

13.        catch (SystemException ee)

14.        {

15.            Response.Write("<script>alert('错误!"+ee.Message +"');</script>");

16.        }

17.        finally {

18.            conn.Close();

19.            conn.Dispose();

20.        }

21.    }

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

回复 使用道具 举报
GridView1.databind();
回复 使用道具 举报
赵利斌 发表于 2013-5-11 14:23
da.fill(ds,"一个命名的表明")    可以改成
da.Fill(ds,"info");

名字不是随便起的吗??
回复 使用道具 举报
是随便起的
只要你自己清楚就行了么   
回复 使用道具 举报
GridView1.DataSource = ds.Tables[0];
指定数据源后,要重新绑定改一下:
GridView1.DataBind();
回复 使用道具 举报
张迁 中级黑马 2013-5-12 00:22:02
7#
宋兴征 发表于 2013-5-11 23:42
GridView1.DataSource = ds.Tables[0];
指定数据源后,要重新绑定改一下:
GridView1.DataBind(); ...

还是不行啊,
回复 使用道具 举报
张迁 中级黑马 2013-5-12 00:23:31
8#
宋兴征 发表于 2013-5-11 23:42
GridView1.DataSource = ds.Tables[0];
指定数据源后,要重新绑定改一下:
GridView1.DataBind(); ...

这是全部代码,运行空白
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;

  12. public partial class Default5 : System.Web.UI.Page
  13. {
  14.     SqlConnection conn = Db.createCon();
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.         shuaxin();
  18.     }
  19.     protected void Button1_Click(object sender, EventArgs e)
  20.     {
  21.         conn.Open();
  22.         String str = "select * from NCP_info where NCP_name = '" + TextBox1.Text.ToString() + "'";
  23.         try
  24.         {
  25.             SqlDataAdapter da = new SqlDataAdapter(str , conn);
  26.             DataSet ds = new DataSet();
  27.             da.Fill(ds, "NCP_info");
  28.             GridView1.DataSource = ds.Tables[0];
  29.             GridView1.DataBind();
  30.          
  31.         }
  32.         catch (SystemException ee)
  33.         {
  34.             Response.Write("<script>alert('错误!"+ee.Message +"');</script>");
  35.         }
  36.         finally {
  37.             conn.Close();
  38.             conn.Dispose();
  39.         }
  40.     }


  41.     public void shuaxin()
  42.     {
  43.         try
  44.         {
  45.             SqlDataAdapter da = new SqlDataAdapter("select * from NCP_info", conn);
  46.             DataSet ds = new DataSet();
  47.             da.Fill(ds, "NCP_info");
  48.             GridView1.DataSource = ds.Tables[0];
  49.             GridView1.DataBind();
  50.              }
  51.              catch (SystemException e)
  52.              {
  53.                  Response.Write("<script>alert('错误!"+e.Message +"');</script>");
  54.              }
  55.              finally {
  56.                  if (conn.State == ConnectionState.Open) {
  57.                      conn.Close();
  58.                      conn.Dispose();
  59.                  }
  60.              }
  61.         }
  62.     }
复制代码
回复 使用道具 举报
SqlConnection conn = Db.createCon();这里面怎么写的啊,明显没有数据源啊
string sqlcon = @"server=;database=test;Integrated Security=true";
SqlConnection conn = new SqlConnection(sqlcon);
这个东西那
回复 使用道具 举报
引用石国庆 的。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马