insert values to the table

  • Thread starter Thread starter Raymond Chiu
  • Start date Start date
R

Raymond Chiu

Dear all,

I have the situations that I want to clear the table and insert values into
the table.
What the coding should be in VB.net?
If I have the oracle command of query, how can I do?
How can I specify the table of inserting?

Please help!

Thanks for your help,

Raymond
 
First declare a connection with a valid connection string...

OracleConnection cn = new OracleConnection("YourConnectstring here");

Next create an insert command or use a stored procedure:

string sql = "INSERT INTO myTable (Field1, Field2) VALUES (@Field1,
@Field2)";

next create a command
OracleCommand cmd = new OracleCommand(sql, cn);
Open your connection (you'll want to wrap this in a try/catch finally, I'm
not doing it for brevity).
if(cn.State !=ConnectionState.Open){cn.Open();}

Add your parameters:

cmd.Parameters.Add("@Field1", OracleDbType.Varchar, 50).Value = "Whatever";
cmd.Paremeters.Add("@Field2", OracleDbType.Varchar, 50).Value = "Something
else";

//I can't remember if it's OracleDbType but I'm pretty sure it is.. check
the documentation
next fire the command :
int i = cmd.ExecuteNonQuery();//wrap this too since an excpetion here will
be fundamentally different than an exception with Connection.Open.

Now close the connection (this should be in a finally)
if(cn.State !=ConnectionState.Closed){cn.Close();}

If you use a stored proc, then use the stored proc name where I used the
variable sql.. cmd = new SqlCommand("StoredProcName", cn);
then set the CommandType of the command to CommandType.StoredPRocedure

You can also do this with the dataadapter, and the configuation wizard if
you want to update multiple rows with DataAdapter.Update... in that case,
run the wizard and just callupdate.
 
Back
Top