Solved

ADODB cn.Execute

Posted on 2007-03-23
5
1,159 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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