Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OleDBCommand and simple query (Newbie) 1

Status
Not open for further replies.

JBaileys

Technical User
Jun 23, 2003
244
US

I am new to C#. I have established a connection to a SQL Server data source...
SqlConnection conn = new SqlConnection("server="+strDBServer+";database="+strDBName+";trusted_connection=yes");

All I want to do is execute a simple query against the db.

For example: "Update products set value = value * 1.1"

I have tried the oledbcommand with zero results.

OleDbCommand OleDb = new OleDbCommand();

OleDb.CommandText = "Update products...";

OleDb.Open();
results = OleDb.ExecuteNonQuery();
 
you're nearly there.

you have to open the connection object.

the connection is using SqlConnection, but the other commands are OleDb. i'd change them all to be the same (the native sql server objects will be a gajillion times faster than oledb) for consistency's sake.

Code:
SqlConnection cnx = new SqlConnection("cnx...");
SqlCommand cmd = new SqlCommand("sql...", cnx);
cnx.Open();
Int32 results = cmd.ExecuteNonQuery();
cnx.Close();

hope this helps,


mr s. <;)

 

Yes - thank you. I solved the problem with this code...

SqlConnection conn = new SqlConnection("server="+strDBServer+";database="+strDBName+";trusted_connection=yes");

sql = "Update <table> set column = 'xyz'";
SqlCommand UpdatePwd = new SqlCommand(sql, conn);
UpdatePwd.ExecuteNonQuery();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top