How to execute an INSERT and UPDATE SQL commands from inside a CSharp program

  • Thread starter Thread starter Robert McCabe
  • Start date Start date
R

Robert McCabe

How do I have to code the execution of such an INSERT or UPDATE SQL statement ?

Given the follwoing code:

command = connection.CreateCommand();
command.CommandText = "INSERT ....";

A SELECT statement would continue with:

OracleDataReader rdr;

rdr = commnad.ExecuteReader();

But this doesn't work for INSERT and UPDATE commands.

How should I proceed in this case ?

Robert
 
How do I have to code the execution of such an INSERT or UPDATE SQL statement ?

Given the follwoing code:

command = connection.CreateCommand();
command.CommandText = "INSERT ....";

A SELECT statement would continue with:

OracleDataReader rdr;

rdr = commnad.ExecuteReader();

But this doesn't work for INSERT and UPDATE commands.

How should I proceed in this case ?

Robert

You just asked the same question someone asked just before you, and
was answered.
 
You just asked the same question someone asked just before you, and
was answered.- Hide quoted text -

- Show quoted text -

Try this:

private void InsertRecord(string nonQuery)
{
SqlConnection connectionObj = new SqlConnection();
SqlCommand command = new SqlCommand();
command.Connection = connectionObj;
command.Connection.ConnectionString =
ConfigurationManager.AppSettings["DBConnectionString"];//Specify your
connection string here
command.CommandText = nonQuery;
command.Connection.Open();

try
{
command.ExecuteNonQuery();
}
finally
{
command.Connection.Close();
}
}
 
On Jun 11, 11:20 am, (e-mail address removed) (Robert McCabe) wrote:
You just asked the same question someone asked just before you, and
was answered.- Hide quoted text -
- Show quoted text -

Try this:

private void InsertRecord(string nonQuery)
{
SqlConnection connectionObj = new SqlConnection();
SqlCommand command = new SqlCommand();
command.Connection = connectionObj;
command.Connection.ConnectionString =
ConfigurationManager.AppSettings["DBConnectionString"];//Specify your
connection string here
command.CommandText = nonQuery;
command.Connection.Open();

try
{
command.ExecuteNonQuery();
}
finally
{
command.Connection.Close();
}
}

You can also use Dataset, with TableAdapter. In this way all
coonections are made automatically, and you can define your own
methods. If you are intereseted in I will explain you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top