标题:【求助】数据连接显示问题
只看楼主
浅水遨游
Rank: 2
等 级:论坛游民
帖 子:18
专家分:81
注 册:2011-1-7
得分:4 
以下是引用xiaxun在2011-1-6 15:05:41的发言:

大家好,
我在数据库表里面有3个字段:
id(自动生成),sno学号,sneme姓名,simfomation详细信息!



现在可以连接数据库了!

在dataGridView1里面只显示:sno和sneme,

然后当单击dataGridView1控件里面的时候,对应在一个txtbox1里面把
对应学生信息simfomation,显示出来!!!


怎么实现呀!

谢谢!


  private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
           string connString = @"Data Source=localhost;Initial Catalog=aa11;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
             = "select Booktext from tb_Book where Booktime=@Booktime  order by ID ASC";
            try
            {
         
               cmd.Parameters.AddWithValue("@Booktime", this.dataGridView1.SelectedCells[1].Value.ToString());

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        
            SqlDataReader sqldr = cmd.ExecuteReader();
            
            if (sqldr.Read())//到这里就直接为假,跳过去了,还怎么读取数据
            {
  
                txtBook.Text = sqldr[1].ToString();
            }
            else
            {
                MessageBox.Show("显示失败");
            }
            conn.Close();
        }
你表里都没见Booktext这个段吧。当然读不到啊。
2011-01-07 20:41
浅水遨游
Rank: 2
等 级:论坛游民
帖 子:18
专家分:81
注 册:2011-1-7
得分:0 
以下是引用xiaxun在2011-1-7 10:55:56的发言:

我不是从数据库控件dataGridView1里面读取数据,而是从数据库表里面直接读取放进 文本框!!
怎么实现呀!
  private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            string connString = @"Data Source=localhost;Initial Catalog=aa11;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
             = "select Booktext from tb_Book where Booktime=@Booktime  order by ID ASC";
            try
            {
              
                cmd.Parameters.AddWithValue("@Booktime", Convert.ToDateTime(this.dataGridView1.SelectedCells[1].Value.ToString()));
                cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            //cmd.Parameters.AddWithValue("@id", this.dataGridView1.CurrentCell.RowIndex.ToString());
            SqlDataReader sqldr = cmd.ExecuteReader();
            table2.Clear();
            table2.Load(sqldr);

            SqlDataReader sqldr2 = cmd.ExecuteReader();
            if (sqldr2.Read())
            {

               
                // txtBook.Text = sqldr[1].ToString();
                txtBook.Text = table2.Rows[0][1].ToString();
            }
            else
            {
                MessageBox.Show("显示失败");
            }
            conn.Close();
            
        }
提示说没有声明变量@Booktime,我不是已经用带参数来赋值了吗
cmd.Parameters.AddWithValue("@Booktime", Convert.ToDateTime(this.dataGridView1.SelectedCells[1].Value.ToString()));
肯定是上面的句子错了。你看看该函数怎么用的。
2011-01-07 20:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-330651-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021881 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved