Solved

ADODB cn.Execute

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Web appliction 7 48
Red error squiggly on vb.net 7 27
Open a word document 23 33
Where to download and how to install sqldmo.dll 5 34
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 …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

11 Experts available now in Live!

Get 1:1 Help Now