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

就写了六行简单到弱爆的代码运行了没反应不知道怎么回事

放了个textbox,想运行程序后能在这个textbox里显示出统计次数的结果的,但是一无所获。sql代码在数据库里执行的时候是能成功统计出结果的。。求解原因。。。


protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection("server=localhost;database=jtgl;user=sa;password=sa");
        SqlCommand cmd = new SqlCommand("select count(bltype) from ajd where bltype=1", cn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "ajd");
        this.TextBox1.Text = ds.Tables[0].Rows[0][0].ToString(); 
    } --------------------编程问答-------------------- select count(bltype) from ajd where bltype='1' 最后要加单引号的吧  --------------------编程问答-------------------- <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>

SqlConnection cn = new SqlConnection("server=(local);database=jtgl;user=sa;password=sa");
--------------------编程问答-------------------- SqlConnection cn = new SqlConnection("Data Source==(local);Database=jtgl;Uid=sa;Pwd=sa");
--------------------编程问答-------------------- 既然你的SQL是可以直接得到正确结果的,那么bltype是整形是确定无误的了吧。
那么LZ确定你的SqlConnection 连接串写对了吗? --------------------编程问答--------------------
引用 1 楼 evilfellow 的回复:
select count(bltype) from ajd where bltype='1' 最后要加单引号的吧


这个无所谓的貌似 --------------------编程问答--------------------
引用 3 楼 net5354 的回复:
SqlConnection cn = new SqlConnection("Data Source==(local);Database=jtgl;Uid=sa;Pwd=sa");



好像不行,还是没反应 --------------------编程问答--------------------
引用 4 楼 cheniwantyou 的回复:
既然你的SQL是可以直接得到正确结果的,那么bltype是整形是确定无误的了吧。
那么LZ确定你的SqlConnection 连接串写对了吗?



当然写对了,其他里面也有用到的 --------------------编程问答--------------------  SqlDataAdapter da = new SqlDataAdapter(cmd,cn);
--------------------编程问答-------------------- 看错了。那里没问题。

server=.;uid=sa;pwd=sa;database=jtgl; --------------------编程问答-------------------- SqlConnection cn = new SqlConnection("server=.;database=jtgl;uid=sa;pwd=sa");
  SqlCommand cmd = new SqlCommand("select count(*) from ajd where bltype=1", cn);

this.TextBox1.Text = cmd.ExecuteScalar().ToString() --------------------编程问答-------------------- cn.Open(); --------------------编程问答-------------------- +1
引用 11 楼 wo_shi_cai_niao_a 的回复:
cn.Open();
--------------------编程问答-------------------- select count(bltype) as ct from ajd where bltype=1 --------------------编程问答-------------------- 看了下。
1、cn.Open();
2、cmd.ExecuteScalar()
楼主去看下基础吧。。数据库没有打开如何执行呢。
返回第一行第一列使用:SqlCommand 的ExecuteScalar方法就行了。何必那么麻烦呢。 --------------------编程问答-------------------- DataAdapter不需要手动打开数据库吧? --------------------编程问答-------------------- sql代码在数据库里执行的时候是能成功统计出结果.
.
.
....那么应该是你赋值给textbox出问题了,断点调试下. --------------------编程问答-------------------- <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>

最重要是你没有自动回传吧 如果调试数据库链接 绑定没有问题那肯定是这里的问题了
--------------------编程问答--------------------  设置回执属性  AutoPostBack=True 要不然textbox无法自动发送 --------------------编程问答-------------------- 为什么不些个按钮事件呢。 --------------------编程问答-------------------- 数据库没打开啊 --------------------编程问答-------------------- 好像是数据库没有打开的! --------------------编程问答-------------------- 都是强人啊 --------------------编程问答-------------------- SqlDataAdapter  不需要显式打开吧  --------------------编程问答-------------------- cn.open() --------------------编程问答-------------------- 应该是这个原因,楼主的连接字符串写的没有问题

引用 2 楼 net5354 的回复:
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>

SqlConnection cn = new SqlConnection("server=(local);database=jtgl;user=sa;password=sa");
--------------------编程问答-------------------- TextChanged事件好像是当textbox改变时才执行吧,直接使用好像不好使吧,为什么不加个button呢,点击button直接给textbox赋值不就行了吗 --------------------编程问答--------------------
引用 11 楼 wo_shi_cai_niao_a 的回复:
cn.Open();

+1 --------------------编程问答-------------------- 求个单值至于用DATASET么?ExecuteScalar多简单。
例子:
SqlConnection cn = new SqlConnection("server=(local);database=northwind;uid=sa;pwd=123");
            SqlCommand cmd = cn.CreateCommand();
            string str = "select count(*) from orders";
            cmd.CommandText = str;
            cn.Open();
            string sum = cmd.ExecuteScalar().ToString();
            cn.Close();
            textBox1.Text = sum;

另外补充一下,使用SqlDataAdapter 不需要显式打开,你原本打开的,它会保持打开,你原本关闭的,它会自动打开,然后在自动关闭。 --------------------编程问答-------------------- 楼主,你SqlConnection Open了吗?你不Open怎么能行??? --------------------编程问答--------------------
引用楼主 djay1126 的回复:
放了个textbox,想运行程序后能在这个textbox里显示出统计次数的结果的,但是一无所获。sql代码在数据库里执行的时候是能成功统计出结果的。。求解原因。。。


protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnec……


建议你去看看sqlcommand和sqldataadapter的两种写法,应该为
 
 SqlConnection cn = new SqlConnection("server=localhost;database=jtgl;user=sa;password=sa");
  //SqlCommand cmd = new SqlCommand("select count(bltype) from ajd where bltype=1",cn);
  SqlDataAdapter da = new SqlDataAdapter("select count(bltype) from ajd where bltype=1",cn);
  DataSet ds = new DataSet();
  da.Fill(ds, "ajd");
  this.TextBox1.Text = ds.Tables[0].Rows[0][0].ToString()
--------------------编程问答-------------------- 哦,还落下了cn.Open();
 SqlConnection cn = new SqlConnection("server=localhost;database=jtgl;user=sa;password=sa");
cn.Open();  
//SqlCommand cmd = new SqlCommand("select count(bltype) from ajd where bltype=1",cn);
SqlDataAdapter da = new SqlDataAdapter("select count(bltype) from ajd where bltype=1",cn);
DataSet ds = new DataSet();
da.Fill(ds, "ajd");
this.TextBox1.Text = ds.Tables[0].Rows[0][0].ToString()
--------------------编程问答-------------------- 应该不是数据库连接和语句的问题,是楼主把代码写到了TextBox1_TextChanged事件里的问题,如果你的是网页,那么这个事件根本就不会被激发,改为放到page_load里;如果是应用程序,那么需要在textbox1中随便输入一个东西才会激发这个事件。 --------------------编程问答--------------------
没看出来  不过adapter 是不用cn.open的 这点可以肯定 --------------------编程问答--------------------

//select count(bltype) from ajd where bltype=1
//这个返回单个值  最好别使用SqlDataAdapter  SqlCommand执行效果最好 
--------------------编程问答-------------------- AutoPostBack="True"这个是蛮重要的

还有就是连接打开 --------------------编程问答--------------------
引用 17 楼 fxy6781349 的回复:
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>

最重要是你没有自动回传吧 如果调试数据库链接 绑定没有问题那肯定是这里的问题了


+1 --------------------编程问答-------------------- SqlConnection cn = new SqlConnection("server=localhost;database=jtgl;user=sa;password=sa");
cn.open();
  SqlCommand cmd = new SqlCommand("select count(bltype) from ajd where bltype=1", cn);
  SqlDataAdapter da = new SqlDataAdapter(cmd);
  DataSet ds = new DataSet();
  da.Fill(ds, "ajd");
cmd.dispose();
da.dispose();
cn.close();

  this.TextBox1.Text = ds.Tables[0].Rows[0][0].ToString();   --------------------编程问答-------------------- 楼主 你想说明什么呢? --------------------编程问答-------------------- 有点意思,看看最后结果是怎样的。估计是网页并没有postback --------------------编程问答-------------------- cn.open();连接没打开。 --------------------编程问答-------------------- cn.open();
lz把代码写在点击事件里吧。。
TextBox1_TextChanged 这个事件谁知道你有没有触发他呀(楼上已经说过了)
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,