比如:  
textBox1绑定了Customer表的Number字段.  
textBox2绑定了Customer表的ID字段.  
 
如何实现根据输入的Number查询ID呢?  
 
小弟是菜鸟请各位大大指点

解决方案 »

  1.   

    thisConnection = DataBaseInit(@"Server=localhost;Integrated Security=True;" +
                            "Database=DataBase");
                zcAdapter = new SqlDataAdapter("SELECT * FROM Table", thisConnection);
                thisDataSet = new DataSet();
                zcAdapter.Fill(thisDataSet, "Table");
                
                foreach (DataRow thisRow in thisDataSet.Tables["name"].Rows)
                {
                      if(thisRow["ID"]==textBox2.text)
                         messageBox.show(thisRow["ID"].ToString());
    }
    thisConnection.Close();
      

  2.   

    这是SqlSever版本的.有问题的话, 告诉我用的什么数据库, 我帮你写段.
      

  3.   

    2楼大大那个会出错...我是菜鸟基本不会改啊这个是我的:private void button1_Click(object sender, System.EventArgs e)
    {
             SqlConnection mySqlConnection =
    new SqlConnection("server=7C432EE094B64A0;database=jam;integrated security=SSPI");
    SqlCommand mySqlCommand =mySqlConnection.CreateCommand();
    mySqlCommand.CommandText=
    " SELECT CustomerName " +
    " FROM Customers " +
    " WHERE CustomerID = '"+textBox1.Text+"'"; mySqlConnection.Open();
    SqlDataReader mySqlDataReader=mySqlCommand.ExecuteReader();
    if(mySqlDataReader.Read())     
    {   
             textBox2.Text = mySqlDataReader["CustomerName"].ToString();   
    }   
             mySqlDataReader.Close();   
    mySqlConnection.Close();    }我的程序是这样.输入ID可以查询出Name.可是不能在textbox中修改数据库.所以想用绑定textBox到表上修改.但是不会操作.请指点
      

  4.   

    thisConnection = new SqlConnection(@"server=7C432EE094B64A0;database=jam;integrated security=SSPI");
                zcAdapter = new SqlDataAdapter(" SELECT CustomerName " +
                                            " FROM Customers " +
                                            " WHERE CustomerID = '"+textBox1.Text+"'",thisConnection);
                thisDataSet = new DataSet();
                zcAdapter.Fill(thisDataSet, "name");
                foreach (DataRow thisRow in thisDataSet.Tables["name"].Rows)
                {
                     if(thisRow["ID"]==textBox2.text) //如果是这个
                         thisRow["Name"]=textBox1.text;
                }
                zcAdapter.Updata();
                thisConnection.Close();
    ...
      

  5.   

    这样其实还麻烦,何不这样?在TextBox1中输入ID,点击"查询",查询出Name写入TextBox2...然后对TextBox2进行修改,点击"更新",将新的Name写回数据库...这样行吗?如果需要这个的代码,请说明...
      

  6.   

    还是有错误啊:名称"thisConnection"在类或命名空间XX.Form1中不存在;
    名称"zcAdapter"在类或命名空间XX.Form1中不存在
    名称"thisDataSet"在类或命名空间XX.Form1中不存在
    找不到类型或命名空间名称"thisDataSet"(是否缺少using指令或程序集引用)
    找不到类型或命名空间名称"zcAdapter"(是否缺少using指令或程序集引用)
    找不到类型或命名空间名称"thisConnection"(是否缺少using指令或程序集引用)这个是我的using指令
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;
      

  7.   

    回liujia_0421() 我的初衷就是这个..可是表述不出来.我要代码.麻烦你了
      

  8.   

    这写的一个,测试是通过的,你根据你的情况改一下吧,如果有问题再问我...private void button1_Click(object sender, EventArgs e)
            {
                //查询
                SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("select * from studentDetails where sno=@Sno", con);
                    cmd.Parameters.AddWithValue("@Sno", this.textBox1.Text);
                    SqlDataReader sdr = cmd.ExecuteReader();
                    if (sdr.Read())
                    {
                        this.textBox2.Text = sdr["sname"].ToString();
                    }
                    sdr.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    con.Close();
                }
            }        private void button2_Click(object sender, EventArgs e)
            {
                //更新
                SqlConnection con = new SqlConnection("server=.;database=student;uid=sa;pwd=0421");
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("update studentDetails set sname=@Sname where sno=@Sno", con);
                    cmd.Parameters.AddWithValue("@Sno", this.textBox1.Text.Trim());
                    cmd.Parameters.AddWithValue("Sname", this.textBox2.Text.Trim());
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    con.Close();
                }
            }
      

  9.   

    我的是一个表是studentDetails,有几个字段sno,sname都是字符串类型的...
      

  10.   

    如果还有问题,可以加我的MSN...
    [email protected]
      

  11.   

    private SqlConnection thisConnection;
            private SqlDataAdapter zcAdapter;
            private DataSet thisDataSet;
    加上这些声明.
      

  12.   

    “System.Data.SqlClient.SqlParameterCollection”并不包含对“AddWithValue”的定义有个错误..这是怎么改啊
      

  13.   

    你的是VS2003的吧..换成Add就行了...
      

  14.   

    换成ADD果然行了.运行的时候,点击查询按钮出现:"第一行 sno附近有语法错误"..
      

  15.   

    TO:运行的时候,点击查询按钮出现:"第一行 sno附近有语法错误"..你根据你的表来看,我的表中字段名和你的不一样的..sno是我的表中的一个字段,在你那肯定有问题的..代码我自己测试过,应该不会有问题的..