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

datagridview显示数据库内容出错

         if (comboBox1.Text == "编号")
            {
                string id = textBox3.Text;
                String sql2 = String.Format("select * from T_book where book_id= '{0}'", id);
                conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter(sql2, conn);
                SqlCommand command = new SqlCommand(sql2, conn);
                int num = Convert.ToInt32(command.ExecuteScalar());
                if (num > 0)
                {
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
                }
                else { MessageBox.Show("姓名有误!请重新输入!"); }

            }
            if (comboBox1.Text == "书名")
            {
                string book_name = textBox3.Text;
                String sql2 = String.Format("select * from T_book where book_name= '{0}'", book_name);
                conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter(sql2, conn);
                SqlCommand command = new SqlCommand(sql2, conn);
                int num = Convert.ToInt32(command.ExecuteScalar());
                if (num > 0)
                {
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
                }
                else { MessageBox.Show("姓名有误!请重新输入!"); }
            }
--------------------编程问答-------------------- --------------------编程问答-------------------- sql有问题,不知道你要得到什么

String sql2 = String.Format("select book_id as Num,* from T_book where book_id= '{0}'", id);

  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/ --------------------编程问答-------------------- 楼主你执行SQL返回的是一个DataSet的数据集啊,怎么能强制转为整型呢? --------------------编程问答-------------------- SQL的语句贴出来,类型转化错误的 --------------------编程问答-------------------- Convert.ToInt32(command.ExecuteScalar());数据集不能转化为整数
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,