失业已久,这个小程序对我找工作机试意义重大,望各位帮忙  有一个sql2000的数据库,库名为test,里面有一个学生表,表里只有三个字段,分别为 姓名、学号、姓别      用vs2003生成一个页面,上面有三个textbox,页面下面有二个按钮,我想在三个textbox里输入内容后,按下面其中一个按钮,就会分别存入学生表的那三个字段里,按另外一个按钮,就会把表里的第一行内容显示在三个textbox中。   虽然对各位来说很简单,但对我学习和求职意义重大,请各位帮帮忙

解决方案 »

  1.   

    的确很简单
    按钮一的事件存textbox中的值
    按钮二事事件读取数据库的值赋给三个textbox的Text
      

  2.   

    查查书吧,这么简单的事随便找本书都能拼凑出来private SqlConnection cn = new SqlConnection();
    cn.ConnectionString = (@"Server=(local);uid=你的ID;pwd=你的密码;database=test") ;string  strSelect;
    SqlCommand cmdSelect;
    SqlDataReader dtrRead;strSelect="Select * from 学生表";
    cmdSelect = new SqlCommand( strSelect, cn );
    cn.Open();
    dtrRead = cmdSelect.ExecuteReader( CommandBehavior.SingleRow );if ( dtrRead.Read() ) 
    {
     Text1.Text = dtrRead["姓名"].ToString();
     Text2.Text = dtrRead["学号" ].ToString();
     Text3.Text = dtrRead["性别"].ToString();
    }dtrRead.Close();
    cn.Close();上面的是显示功能,下面插入功能简写啦,把它们都分别放进Button事件里面,东西自己声明啦
    cn.Open();
    strInsert="Insert 学生表(姓名,学号,性别) Values (@Name,@No,@Sex)";
    cmdInsert=new SqlCommand("strInsert",cn);
    cmdInsert.Parameters.Add("@Name",Text1.Text);
    cmdInsert.Parameters.Add("@No",Text2.Text);
    cmdInsert.Parameters.Add("@Sex",Text3.Text);
    cn.Close();
    吃饭不写啦................
      

  3.   

    ---------------------------保存begin-------------------
    string myExecuteQuery = "insert into table values ('" + textbox1.Text + "','" + textbox2.Text + "','" + textbox3.Text + "')";
    SqlConnection myConnection = new SqlConnection(connstr);//connstr是连库字符串
    SqlCommand myCommand = new SqlCommand(myExecuteQuery, myConnection);
    myCommand.Connection.Open();
    myCommand.ExecuteNonQuery();
    myConnection.Close();
    ---------------------------保存end------------------------
    ---------------------------取数begin--------------
    string myExecuteQuery = "select top 1 * from table";
    SqlConnection myConnection = new SqlConnection(connstr);//connstr是连库字符串
    SqlCommand myCommand = new SqlCommand(myExecuteQuery, myConnection);
    myCommand.Connection.Open();
    myConnection.Open();
    SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
    while(myReader.Read()) 
    {
       textbox1.Text = myReader.GetString(0);
       textbox2.Text = myReader.GetString(1);
       textbox3.Text = myReader.GetString(2);
    }
    myReader.Close();---------------------------取数end------------------------
      

  4.   

    插入语句.
    先获取TextBox的值.然后insert into table(name,sex,id) values(对应name的名字,对应sex的性别,对应id的编号)获取第一行的值:
    select top 1 * from table 就好了..
    然后将读取的数据赋予你对应的的textbox,例如,姓名的TextBox的id是name,则你可以这么写name.Text=你读取的记录如果理解上面的操作比较苦难,可以先了解SQL语句的插入,删除,选择,修改语法..然后再进行.NET的学习..不要急..有恒心就好
      

  5.   

    樓主﹐憑這一個簡單的例如來說明你能或熟悉.NET,只能騙外行。行業內要拿這樣的面試題目﹐我覺得只是考考剛畢業的學生
      

  6.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    namespace goodstudy
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.TextBox TextBox2;
    protected System.Web.UI.WebControls.TextBox TextBox3;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Button Button2;
    protected string strCon="integrated security=SSPI;data source=数据库名;persist security info=False;initial catalog=test"; private void Page_Load(object sender, System.EventArgs e)
    {

    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Button2.Click += new System.EventHandler(this.Button2_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)//插入记录
    {
        SqlConnection myCon=new SqlConnection(strCon);
    myCon.Open();
    string ins="insert into students(names,num,sex)values('"+this.TextBox1.Text+"',"+this.TextBox2.Text+",'"+this.TextBox3.Text+"')";
    SqlCommand myCom=new SqlCommand(ins,myCon);
    myCom.ExecuteNonQuery();
    } private void Button2_Click(object sender, System.EventArgs e)//显示记录
    {
    SqlConnection myCon=new SqlConnection(strCon);
    myCon.Open();
                string sel="select top 1 * from students";
    SqlCommand myCom=new SqlCommand(sel,myCon);
    SqlDataReader DR=myCom.ExecuteReader();
    if (DR.Read())
    {
    this.TextBox1.Text=DR["names"];
    this.TextBox2.Text=DR["num"];
    this.TextBox3.Text=DR["sex"];
    }
    }
    }
    }完全拷贝,,工程名称为goodstudy
      

  7.   

    就是,大家都是一步步混过来的,但是建议楼主买本书,系统学习一下。
    首先你建页面,在.net2003的IDE中简单,你会看到.就有提示,在frontpage中麻烦。
    在页面上拖入3个textbox和2个button
    在后台页面的page_load()中
    SqlConnection CN = new SqlConnection();
    CN.ConnectionString = "data source=land;initial catalog=wj;persist security info=False;user id=sa;Password='';";
    CN.Open();string  strSelect;//修改成你自己的data source和catalog
    SqlCommand cmdselect=new SqlCommand("insert whole(text,atextvalue,btextvalue,ctextvalue,dtextvalue,etextvalue,dtext,sqrname) values(@text,'LD',5,@c,@dd,@ee,@mingcheng,@sqrname)",CN);
    cmdselect.Parameters.Add("@text",text1.text);
    cmdselect.ExecuteNonQuery();
    CN.Close();
      

  8.   

    @xxx就是变量,cmdselect.Parameters.Add("@text",text1.text);这是给变量赋值,比如你的textbox的值,你有3个字段应该有3个变量,你把我的改一下就可以了。显示值你在页面上双击button来进入后台页面,这样就自动注册click事件了。
      

  9.   

    adam_xiu(咬鹅) 的代码最基本,也详细,不过用完连接后别忘记了关闭连接哟!
    在各个事件最后调用
    myCon.Close();
      

  10.   

    第一个按扭时,启动函数
    Private sub button1_chick(...)
      sql='insert into table() values()'
      updatesql(sql)
    End Sub第二个按扭Private sub button2_chick(...)
      sql=selete top 1 from table'
      ds=exesql(sql)
      text1.text=ds.table(0).row(0)(0)
      text2.text=ds.table(0).row(0)(1)
      text3.text=ds.table(0).row(0)(2)
    End Sub
      

  11.   

    to:cabxyz(cab)
    什么叫正确引导??你认为一个人在需要帮助的时候喜欢听别人在那说什么“你应该学习基础”之类的话吗???给他代码有什么不对??谁不是看代码过来的??至于给他代码后他是直接引用还是自己研究那是他的事。至少,给他代码算让他走出了第一步!
      

  12.   

    先谢谢帮助过我的朋友了,本不想麻烦各位了,但是我把(咬鹅)朋友的代码拷贝到了.cs文件后,新建一个项目名称为goodstudy,把数据库名改为我所在sql的名称  学生   后,最后把sql语句改为 string ins="insert into 学生(姓名,学号,姓别)values('"+this.TextBox1.Text+"',"+this.TextBox2.Text+",'"+this.TextBox3.Text+"')";编译报错  显示              this.TextBox1.Text=DR["names"];
    this.TextBox2.Text=DR["num"];
    this.TextBox3.Text=DR["sex"];
    这里出错了  错误为   无法将类型"object"隐式转换为"string"我把这三句去掉后编译通过,但第一个button按了后不能把数据存入sql  我的计算机 id为miralce   sql的帐号为sa 密码为1234   数据库名为test  表名为学生  字段为 
    姓名、学号、姓别   不知为何就是不能存入  都试了一个下午了
      

  13.   

    我新建的是asp.net web应用程序  语言是C#
      

  14.   

    this.TextBox1.Text=DR["names"].toString();
    this.TextBox2.Text=DR["num"].toString();
    this.TextBox3.Text=DR["sex"].toString();
      

  15.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    namespace goodstudy
    {
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.TextBox TextBox2;
    protected System.Web.UI.WebControls.TextBox TextBox3;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Button Button2;
    protected string strCon="integrated security=SSPI;data source=miralce;UserID=sa;Password=1234;persist security info=False;initial catalog=test"; private void Page_Load(object sender, System.EventArgs e)
    {

    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Button2.Click += new System.EventHandler(this.Button2_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)//插入记录
    {
        SqlConnection myCon=new SqlConnection(strCon);
    myCon.Open();
    string ins="insert into students(姓名,学号,姓别)values('"+this.TextBox1.Text+"',"+this.TextBox2.Text+",'"+this.TextBox3.Text+"')";
    SqlCommand myCom=new SqlCommand(ins,myCon);
    myCom.ExecuteNonQuery();
    myCon.close; } private void Button2_Click(object sender, System.EventArgs e)//显示记录
    {
    SqlConnection myCon=new SqlConnection(strCon);
    myCon.Open();
                string sel="select top 1 * from students";
    SqlCommand myCom=new SqlCommand(sel,myCon);
    SqlDataReader DR=myCom.ExecuteReader();
    if (DR.Read())
    {
            this.TextBox1.Text=DR["姓名"].toString();
    this.TextBox2.Text=DR["学号"].toString();
    this.TextBox3.Text=DR["姓别"].toString();
    myCon.close;
    }
    }
    }
    }
    注意:data source="服务器名称"要的是你的SQL的名称;学号字段如果为string型将"+this.TextBox2.Text+"改为'"+this.TextBox2.Text+"'
      

  16.   

    很多谢(咬鹅) 大哥的帮忙,还是出现this.TextBox1.Text=DR["names"].toString();
    this.TextBox2.Text=DR["num"].toString();
    this.TextBox3.Text=DR["sex"].toString();
    的错误  错误为"object“并不包含对”tostring"的定义我的数据库里三个字段都为char类型的,因为设计表字段的时候没有string类型让我选择而且在三个textbox输入东西然后按了button1后还是不能向数据库里存入内容
      

  17.   

    this.TextBox1.Text=DR["names"].ToString();
    this.TextBox2.Text=DR["num"].ToString();
    this.TextBox3.Text=DR["sex"].ToString();你写不进数据库可能是因没有设置SQL的ASPNET权限!!
    还有,既然看见了错误提示就应该自己动手看看到底错在哪里!!
    其实这个错误就是一个大小写的问题,,如果你把.toString()删除的话从新打“.”就能看见提示应该为“ToString()”。
    代码我已经测试过了都能通过了!