失业已久,这个小程序对我找工作机试意义重大,望各位帮忙 有一个sql2000的数据库,库名为test,里面有一个学生表,表里只有三个字段,分别为 姓名、学号、姓别 用vs2003生成一个页面,上面有三个textbox,页面下面有二个按钮,我想在三个textbox里输入内容后,按下面其中一个按钮,就会分别存入学生表的那三个字段里,按另外一个按钮,就会把表里的第一行内容显示在三个textbox中。 虽然对各位来说很简单,但对我学习和求职意义重大,请各位帮帮忙
解决方案 »
- 在asp.net中怎么使用代码兼容主流浏览器
- 得到DLL的参数及其类型
- ajax局部刷新的下拉框的省市取值问题
- 如何实现类似于Viso里面的生成各类图时的操作功能
- aspx中如何访问剪切板
- 服务器无法在发送 HTTP 标头之后修改 cookie
- 一个小问题,急急急急....HELPme help me ~~~乱码的一个问题
- 如何获取CPU序列号,硬盘序列号。
- ==========这个问题.net真的没办法解决吗?高手进来谈论谈论!
- 请问如何实现当鼠标在记录栏里移动,当前记录背景变色?
- 编译DLL问题!======新手提问!!-小妹在此表示感谢!!!
- 怎么在aspx页面上判断radioButtonList中哪一个被选中
按钮一的事件存textbox中的值
按钮二事事件读取数据库的值赋给三个textbox的Text
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();
吃饭不写啦................
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------------------------
先获取TextBox的值.然后insert into table(name,sex,id) values(对应name的名字,对应sex的性别,对应id的编号)获取第一行的值:
select top 1 * from table 就好了..
然后将读取的数据赋予你对应的的textbox,例如,姓名的TextBox的id是name,则你可以这么写name.Text=你读取的记录如果理解上面的操作比较苦难,可以先了解SQL语句的插入,删除,选择,修改语法..然后再进行.NET的学习..不要急..有恒心就好
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
首先你建页面,在.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();
在各个事件最后调用
myCon.Close();
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
什么叫正确引导??你认为一个人在需要帮助的时候喜欢听别人在那说什么“你应该学习基础”之类的话吗???给他代码有什么不对??谁不是看代码过来的??至于给他代码后他是直接引用还是自己研究那是他的事。至少,给他代码算让他走出了第一步!
this.TextBox2.Text=DR["num"];
this.TextBox3.Text=DR["sex"];
这里出错了 错误为 无法将类型"object"隐式转换为"string"我把这三句去掉后编译通过,但第一个button按了后不能把数据存入sql 我的计算机 id为miralce sql的帐号为sa 密码为1234 数据库名为test 表名为学生 字段为
姓名、学号、姓别 不知为何就是不能存入 都试了一个下午了
this.TextBox2.Text=DR["num"].toString();
this.TextBox3.Text=DR["sex"].toString();
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+"'
this.TextBox2.Text=DR["num"].toString();
this.TextBox3.Text=DR["sex"].toString();
的错误 错误为"object“并不包含对”tostring"的定义我的数据库里三个字段都为char类型的,因为设计表字段的时候没有string类型让我选择而且在三个textbox输入东西然后按了button1后还是不能向数据库里存入内容
this.TextBox2.Text=DR["num"].ToString();
this.TextBox3.Text=DR["sex"].ToString();你写不进数据库可能是因没有设置SQL的ASPNET权限!!
还有,既然看见了错误提示就应该自己动手看看到底错在哪里!!
其实这个错误就是一个大小写的问题,,如果你把.toString()删除的话从新打“.”就能看见提示应该为“ToString()”。
代码我已经测试过了都能通过了!