Solved

ADODB cn.Execute

Posted on 2007-03-23
5
1,154 Views
Last Modified: 2013-12-17
Experts,

Please help me to correct this code.
Codes to insert new record.
=====================================================================================
            string oConn, StrSql;
            ADODB.Connection cn = new ADODB.Connection();

            oConn = "Driver={MySQL ODBC 3.51 Driver};Server=xxx.xxx.xxx.xxx;Port=3306;Option=131072;Stmt=;Database=database;Uid=ADMIN;Pwd=pwrd";
            StrSql = "insert into names (code, name) values ('00001', 'This is a test.')";

            cn.Open(oConn, null, null, 0);
            cn.Execute(StrSql, ?, ?);

            cn.Close();
=====================================================================================

Thanks,

0
Comment
Question by:marvelsoft
  • 2
5 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 20 total points
ID: 18778089
cn.Open(oConn, null, null, 0);
            cn.Execute(StrSql);
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 18779640
1) Why are you using ADODB instead of ADO.NET?
2) You must include System.Reflection.Missing for optional parameters:

System.Reflection.Missing missing = System.Reflection.Missing.Value;
cn.Open(oConn, null, null, 0);
cn.Execute(StrSql, ref missing, ref missing);
0
 

Author Comment

by:marvelsoft
ID: 18790284
It is just happens to that my old programs is written in VB6 w/ ADODB, but Im started converting it to VS2005.
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 30 total points
ID: 19126709
If you're converting it to VS2005, I would get rid of the ADODB stuff and use ADO.NET instead.  

Here's the ADO.NET equivalent of your code:

=====================================================================================
            string oConn, StrSql;
            oConn = "Driver={MySQL ODBC 3.51 Driver};Server=xxx.xxx.xxx.xxx;Port=3306;Option=131072;Stmt=;Database=database;Uid=ADMIN;Pwd=pwrd";
            OdbcConnection cn = new OdbcConnection(oConn);

            StrSql = "insert into names (code, name) values ('00001', 'This is a test.')";
            ObcsCommand cmd = new OdbcCommand(StrSql,cn);

            cn.Open();
            cmd.ExecuteNonQuery();

            cn.Close();
=====================================================================================

I didn't put any error handling in there because I wanted to show you the straight translation.  In reality you should put the connection in a using{} block to make sure it gets closed if there is an exception.  
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now