比如我现在鹅一个数据库test,进而有一张表student,表里面有四个列,分别为sno,sname,ssex,scity
我想实现,通过四个textBox分别对应一个列值向表里增加一条记录,并显示整张表在dataGrid里
如何实现
我想实现,通过四个textBox分别对应一个列值向表里增加一条记录,并显示整张表在dataGrid里
如何实现
解决方案 »
- c#用鼠标画固定比例剪切矩形框
- winform 连接外网oracle问题
- 怎样往datagridview中填充数据啊?
- vs2008 express 不能在创建项目时,选择版本,怎么办?
- LINK : fatal error LNK1104: cannot open file "msado15.dll"
- SQL连接字符串的问题在线等
- 又是这个问题,String or binary data would be truncated. The statement has been termina
- C# 用chart control画个饼图但是运行后显示一个红色的叉。不知道为什么
- 请问:Remoting中,订阅事件的时候为啥要出System.Reflection.TargetInvocationException 附代码
- ASPX.CS文件.里如何实现键盘监听
- ClickOnce 发布问题
- 关于timer的问题,求解!
至于显示整张表写一条SQL查询,返回DataSet或者DataReader,然后绑定即可。
另外就是你的四个列的属性插入的时候保持属性一致
字符型不要往数值型插入
另外也可以用SqlHelper
string str="insert youTab(sno,sname,ssex,scity) value('"+this.textbox1.text+"','"+this.textbox2.text+"',"'"+this.textbox3.text+"',"'"+this.textbox.text4+"')"
private string connectionString = "workstation id=XX;Integrated Security=SSPI;database=XX,user id=XX .....";
this.myConnection = new SqlConnection(connectionString);
myConnection.Open();
SqlCommand tempSqlCommand = new SqlCommand(Str,this.myConnection);
tempSqlCommand.ExecuteNonQuery();//返回数据库中影响的行数
myConnection.Close();
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(this.strSQL,this.myConnection);
this.ds.Clear();
this.da.Fill(ds,tempTableName);
//绑定dataGrid1
this.dataGrid1.DataSource = tempTableName;
................
protected System.Data.OracleClient.OracleConnection conn=new OracleConnection((="Provider=MSDAORA.1;Password=;User ID=;Data Source=test;Persist Security Info=True";);
Protected System.Data.OracleClient.OracleCommand comm=new OracleCommand();
protected System.Data.DataSet ds=new DataSet();
string strNo=text1.text.Tostring().trim();
string strName=text2.text.Tostring().trim();
string strSex=text3.text.Tostring().trim();
string strCity=text4.text.Tostring().trim();
string sql="insert into student(sno,sname,ssex,scity)values('"+strNo+"','"+strName+"','"+strSex+"','"+strCity+"');
comm.Connection=conn;
comm.CommandText=sql;
comm.ExecuteNonQuery();
SqlConnection conn=new SqlConnection("连接数据库的字符串");
string sql="用INSERT INTO 命令";
SqlCommand cmd=new SqlCommand(sql,conn);
conn.open();
cmd.ExecuteNoney()
conn.close();
OleDbDataAdapter da = new OleDbDataAdapter(this.str,conn);
this.da.Fill(ds,tempTableName);
this.dataGrid1.DataSource = tempTableName;
前台:(四个文本框,1个按钮,1个datagrid)
<asp:TextBox ID="txtNo" runat="server"></asp:TextBox>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSex" runat="server"></asp:TextBox>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox><br />
<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="添加" Width="99px" />
<asp:DataGrid ID="DataGrid1" runat="server"></asp:DataGrid>后台:(即按钮的单击事件) protected void btnAdd_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("连接字符串");
con.Open();
string strSql="insert into student(sno,sname,ssex,scity) values(@sno,@sname,@ssex,@scity)";
SqlCommand cmd = new SqlCommand(strSql, con);
//设置SQL所需参数
cmd.Parameters.Add("@sno", SqlDbType.VarChar, 10);
cmd.Parameters.Add("@sname", SqlDbType.VarChar, 10);
cmd.Parameters.Add("@ssex", SqlDbType.VarChar, 2);
cmd.Parameters.Add("@scity", SqlDbType.VarChar, 20);
//参数赋值
cmd.Parameters["@sno"].Value = txtNo.Text;
cmd.Parameters["@sname"].Value = txtName.Text;
cmd.Parameters["@ssex"].Value = txtSex.Text;
cmd.Parameters["@scity"].Value = txtCity.Text;
//执行语句插入数据库
cmd.ExecuteNonQuery();
//生成datareader绑定到datagrid
strSql="select * from student";
cmd.CommandText=strSql;
SqlDataReader dtr = cmd.ExecuteReader();
DataGrid1.DataSource = dtr;
DataGrid1.DataBind();
dtr.Close();
con.Close();
}