Solved

ADODB cn.Execute

Posted on 2007-03-23
5
1,172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 143

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
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 …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

617 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