(vs2008+oracle) GridView新增记录的方法。

第一种,使用command直接插入

 

//使用command插入数据
 //使用command插入数据
            OracleConnection conn = new OracleConnection(constr);
            OracleCommand comm 
= new OracleCommand();
            comm.Connection 
= conn;
            
string comstr = "insert into products(productid,productname,unitprice,discontinued,categoryid) values" +
                
"(BBB.nextval,'" + TextBox2.Text + "'," + TextBox3.Text + "," + 0 + "," + DropDownList1.SelectedValue + ")";
            comm.CommandText 
= comstr;
            conn.Open();
            Int32 rowsAffected 
= comm.ExecuteNonQuery();
            conn.Close();

 

 

第二种,带参数的command插入数据

 

//使用command命令+参数
//使用command命令+参数
            OracleConnection conn = new OracleConnection(constr);
            OracleCommand comm 
= new OracleCommand();
            comm.Connection 
= conn;            
            
string comstr = "insert into products(productid,productname,unitprice,discontinued,categoryid) values" +
                
"(:productid,:productname,:unitprice,0,:categoryid)";
            comm.CommandText 
= comstr;
            comm.Parameters.AddWithValue(
":productid", TextBox1.Text);
            comm.Parameters.AddWithValue(
":productname", TextBox2.Text);
            comm.Parameters.AddWithValue(
":unitprice", TextBox3.Text);
            comm.Parameters.AddWithValue(
":categoryid", DropDownList1.SelectedValue);
            Response.Write(comm.CommandText);

            conn.Open();
            comm.ExecuteNonQuery();
            conn.Close();

 

 

第三种,使用oraclecommandbuilder插入数据:

 

//使用oracleCommandbuilder

            
//使用oracleCommandbuilder
            DataSet dstmp = new DataSet();
            OracleDataAdapter da 
= new OracleDataAdapter(strSQL, constr);
            OracleCommandBuilder cb 
= new OracleCommandBuilder(da);
            da.Fill(dstmp);
            DataRow dr 
= dstmp.Tables[0].NewRow();
            dr[
"productid"= Convert.ToDecimal(TextBox1.Text);
            dr[
"categoryid"= 1;
            dr[
"productname"= TextBox2.Text;
            dr[
"unitprice"= Convert.ToDecimal(TextBox3.Text);
            dr[
"discontinued"= 0;
            dstmp.Tables[
0].Rows.Add(dr);
            da.Update(dstmp);

 

 

posted @ 2009-12-21 09:55  PROS  阅读(577)  评论(0)    收藏  举报