Solved

ADODB cn.Execute

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why use this lambda? 12 95
Consume a webservice via VB in Visual Studio 2015 3 44
Web page design problem 3 41
PowerShell: Adding ToGB to a script 4 72
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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