public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// if (!IsPostBack)
// {
// this.bind();
// }
} public OracleConnection Getconnection()
{
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
OracleConnection myConn = new OracleConnection( myStr );
return myConn;
} //数据查询 protected void Button1_Click(object sender, EventArgs e)
{
if (this.TextBox1.Text != "")
{
OracleConnection myConn = Getconnection();
myConn.Open();
string sqlStr = "select * from SH.JIAOYIXINXI where Name=:Name";
OracleCommand myCmd = new OracleCommand(sqlStr, myConn);
myCmd.Parameters.Add("Name", OracleType.VarChar, 20).Value = this.TextBox1.Text.Trim();
OracleDataAdapter myDa = new OracleDataAdapter(myCmd);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
if (myDs.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = myDs;
GridView1.DataBind();
}
else
{
Response.Write("<script>alert('没有相关记录') </script>");
}
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
}
//数据绑定 protected void bind()
{
OracleConnection myconn = Getconnection();
myconn.Open();
string sqlstr = "select * from SH.KEHUXINXI";
OracleDataAdapter myda = new OracleDataAdapter(sqlstr,myconn);
DataSet myds = new DataSet();
myda.Fill(myds);
GridView2.DataSource = myds;
GridView2.DataKeyNames = new string[] { "姓名" };
GridView2.DataBind();
myds.Dispose();
myds.Dispose();
myconn.Close();
} //数据插入 protected void Button2_Click(object sender, EventArgs e)
{
if (this.TextBox2.Text != "")
{
OracleConnection myConn = Getconnection();
myConn.Open();
string sqlstr = "insert into SH.KEHUXINXI(姓名) values(" + this.TextBox2.Text.Trim() + ")";
OracleCommand myCmd = new OracleCommand(sqlstr, myConn);
myCmd.ExecuteNonQuery();
myConn.Close();
//this.bind();
Response.Write("<script>alert('插入成功!') </script>");
}
else
//this.bind();
Response.Write("<script>alert('插入失败!') </script>");
} //绑定Row编辑 protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
this.bind ();
} //数据更新
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int 姓名 = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value.ToString());
string CName = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[2].Controls[0] )).Text.ToString();
string sqlstr = "update SH.KEHUXINXI set 姓名='" + CName + "'" ;
OracleConnection myconn = new OracleConnection();
myconn.Open();
OracleCommand mycmd = new OracleCommand(sqlstr, myconn);
mycmd.ExecuteNonQuery();
mycmd.Dispose();
myconn.Close();
GridView2.EditIndex = -1;
this.bind(); }
}
哪位高人给我说一下为什么我的这个数据更新不了?是哪里有问题么?为什么插入语句都是正常的,而更新的时候就显示除非使用updatecommand,否则datasourse2不允许更新数据,满分求救!急,谢谢!
{
protected void Page_Load(object sender, EventArgs e)
{
// if (!IsPostBack)
// {
// this.bind();
// }
} public OracleConnection Getconnection()
{
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
OracleConnection myConn = new OracleConnection( myStr );
return myConn;
} //数据查询 protected void Button1_Click(object sender, EventArgs e)
{
if (this.TextBox1.Text != "")
{
OracleConnection myConn = Getconnection();
myConn.Open();
string sqlStr = "select * from SH.JIAOYIXINXI where Name=:Name";
OracleCommand myCmd = new OracleCommand(sqlStr, myConn);
myCmd.Parameters.Add("Name", OracleType.VarChar, 20).Value = this.TextBox1.Text.Trim();
OracleDataAdapter myDa = new OracleDataAdapter(myCmd);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
if (myDs.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = myDs;
GridView1.DataBind();
}
else
{
Response.Write("<script>alert('没有相关记录') </script>");
}
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
}
//数据绑定 protected void bind()
{
OracleConnection myconn = Getconnection();
myconn.Open();
string sqlstr = "select * from SH.KEHUXINXI";
OracleDataAdapter myda = new OracleDataAdapter(sqlstr,myconn);
DataSet myds = new DataSet();
myda.Fill(myds);
GridView2.DataSource = myds;
GridView2.DataKeyNames = new string[] { "姓名" };
GridView2.DataBind();
myds.Dispose();
myds.Dispose();
myconn.Close();
} //数据插入 protected void Button2_Click(object sender, EventArgs e)
{
if (this.TextBox2.Text != "")
{
OracleConnection myConn = Getconnection();
myConn.Open();
string sqlstr = "insert into SH.KEHUXINXI(姓名) values(" + this.TextBox2.Text.Trim() + ")";
OracleCommand myCmd = new OracleCommand(sqlstr, myConn);
myCmd.ExecuteNonQuery();
myConn.Close();
//this.bind();
Response.Write("<script>alert('插入成功!') </script>");
}
else
//this.bind();
Response.Write("<script>alert('插入失败!') </script>");
} //绑定Row编辑 protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
this.bind ();
} //数据更新
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int 姓名 = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value.ToString());
string CName = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[2].Controls[0] )).Text.ToString();
string sqlstr = "update SH.KEHUXINXI set 姓名='" + CName + "'" ;
OracleConnection myconn = new OracleConnection();
myconn.Open();
OracleCommand mycmd = new OracleCommand(sqlstr, myconn);
mycmd.ExecuteNonQuery();
mycmd.Dispose();
myconn.Close();
GridView2.EditIndex = -1;
this.bind(); }
}
哪位高人给我说一下为什么我的这个数据更新不了?是哪里有问题么?为什么插入语句都是正常的,而更新的时候就显示除非使用updatecommand,否则datasourse2不允许更新数据,满分求救!急,谢谢!
解决方案 »
- dll有什么工具反编译
- C# + WPF环境下,如何手动关闭Backgroundworker线程
- 安装软件捆绑
- IMAGELIST中用的图片,可以自己画出来吗?用啥工具?
- windows service中实现tcp监听,但不知道为什么就是不可以!!着急等待
- 请教页面源代码上是"charset=gb2312",为何GetEncoding("gb2312")仍然读出乱码?谢谢
- c#重启sql服务后 sqlcommand无法执行问题
- 我数据库有很多数据,大概2w条左右,我要用其他技术改进吗
- 如果某段代码要依据某个条件作出相应动作,那么这个条件是把它放数据库中作为一个表还是直接嵌入代码?
- 谁有c#下的语音聊天方面的有关控件?
- Smart Client发布问题
- C#如何定义结构体数组?
string sqlstr = "insert into SH.KEHUXINXI(姓名) values(" + this.TextBox2.Text.Trim() + ")";
OracleCommand myCmd = new OracleCommand(sqlstr, myConn);
myCmd.ExecuteNonQuery();然后你的更新语句这么写,也没有问题呀!也原因需要命令来执行:
string sqlstr = "update SH.KEHUXINXI set 姓名='" + CName + "'" ;
OracleConnection myconn = new OracleConnection();
myconn.Open();
OracleCommand mycmd = new OracleCommand(sqlstr, myconn);
mycmd.ExecuteNonQuery();