Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
is there have any method for connect c++ console application data to the sql data base. i have image processing program using c++ and opencv. now i want save some pictures to the local sql data base. how do that.

What I have tried:

i tried in c#. its working. i want this kind of example

C++
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Diagnostics;


namespace face_detetc_admin
{
    public partial class c_pass : Form
    {
        private SqlConnection DBConn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\aInven\Face_detect\database\db.mdf;Integrated Security=True");

        String data;

        public c_pass()
        {
            InitializeComponent();
        }

        private void updtbtn_Click(object sender, EventArgs e)
        {

            if (idtxtbx.Text.Length <= 0)
            {
                MessageBox.Show("User Id not found", "Error...");
            }
            else if (nwbx.Text != cnfmbx.Text)
            {
                MessageBox.Show("New password and old password not matched!", "Error...");
            }
            else if (nwbx.Text.Length <= 0 || cnfmbx.Text.Length <= 0)
            {
                MessageBox.Show("Some field are missing!", "Error...");
            }
            else
            {

                if ((MessageBox.Show("are you sure ?", "Confirmation...", MessageBoxButtons.YesNo) == DialogResult.Yes))
                {


                    try
                    {
                        var command = DBConn.CreateCommand();

                        DBConn.Open();
                        string strSQL = string.Format("Select * From Log where uname = '{0}'", idtxtbx.Text);
                        SqlCommand myCommand = new SqlCommand(strSQL, DBConn);

                        {
                            myCommand.Parameters.AddWithValue("uname", idtxtbx.Text);
                            using (SqlDataReader reader = myCommand.ExecuteReader())
                            {
                                while (reader.Read())
                                {

                                    data = reader["pword"].ToString();
                                }


                            }
                        }

                        if (oldbx.Text == data)
                        {

                            // MessageBox.Show("ddddd");5
                            // using (SqlCommand cmd = new SqlCommand("UPDATE Log  SET  pword = " + nwbx.Text + " where uname  = uname", DBConn))
                            using (SqlCommand cmd = new SqlCommand("UPDATE Log  SET pword = @pword  where uname  = @uname", DBConn))
                            {
                                cmd.Parameters.AddWithValue("@pword", nwbx.Text);


                                cmd.Parameters.AddWithValue("@uname", idtxtbx.Text);
                                cmd.ExecuteNonQuery();
                                MessageBox.Show("Password changed!!!", "Updated");
                                this.Hide();


                            }
                        }
                        else
                        {
                            MessageBox.Show("Not found. Try again!!!", "Error.....");
                        }

                        DBConn.Close();


                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show("failed" + ex.Message);

                    }
                }
            }
        }

        private void button1_Click(object sender, System.EventArgs e)
        {

            idtxtbx.Clear();
            oldbx.Clear();
            nwbx.Clear();
            cnfmbx.Clear();
        }

        private void button3_Click(object sender, System.EventArgs e)
        {
            if (utxtbx.Text.Length > 0 && ptxtbx.Text.Length > 0 && ptxtbx.Text == ctxtbx.Text)
            {
                if ((MessageBox.Show("are you sure add this informations?", "Confirmation...", MessageBoxButtons.YesNo) == DialogResult.Yes))
                {
                    try
                    {
                        SqlCommand MyCommand = new SqlCommand("INSERT INTO Log" + " VALUES('" + utxtbx.Text + "','" + ptxtbx.Text + "')", DBConn);

                        DBConn.Open();
                        MyCommand.ExecuteNonQuery();
                        DBConn.Close();
                        MessageBox.Show("Updated!!!");
                        utxtbx.Clear();
                        ptxtbx.Clear();
                        this.Dispose();



                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show("failed" + ex.Message);
                        DBConn.Close();
                    }

                }
            }
            else
            {
                MessageBox.Show("Error Input data", "Error...");

            }


        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            utxtbx.Clear();
            ptxtbx.Clear();
            ctxtbx.Clear();
        }

        private void c_pass_Load(object sender, System.EventArgs e)
        {

        }
    }
}
Posted
Updated 6-Jan-17 8:54am
v2
Comments
[no name] 6-Jan-17 14:49pm    
Yes in C++ you use ODBC. What does this C# code have to do with your question? And why did not google find this information for you?

1 solution

The difference is that you need to use for example ODBC from C++. Have a look at these:
- Data Access in Visual C++[^]
- Microsoft Aligning with ODBC | SQL Server Blog[^]
- Allocate Handles and Connect to SQL Server (ODBC)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900