0

Encountered with a problem... maybe something in the syntax of the query, and the compiler doesn't throw any Exception. But the table do not receive information.

Anything you noticed that maybe wrong?

OleDbConnection conn;
OleDbCommand cmd;


public Commands(OleDbConnection con)
{
    conn = con;
}


public Commands()
{
    conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DB.accdb");
}

public void node_Join(int id, byte[] mac)
{
    try
    {
        conn.Open();
        cmd = new OleDbCommand(@"INSERT INTO Nodes ([J_ID],[Node ID],[Node MAC],[Line Quality],[Status]) values('" + Convert.ToString(id) + @"',0,'" + BitConverter.ToString(mac) + @"',0,'Join')", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
    }
    catch (Exception exc)
    {
        conn.Close();
        MessageBox.Show(exc.Message.ToString());
    }
}
8
  • 2
    Are you sure your connection string is right? What are the values of Convert.ToString(id) and BitConverter.ToString(mac)? Why do you think you should have these Commands methods? What are the types of your columns? And you should always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks. Commented Jan 19, 2014 at 11:04
  • Types: J_ID -> Number, Node ID -> Number, Node MAC - > String, Line Q -> Number, Status -> String. right now im just learning to work with sql and etc. so parameterized queries not so needed. Commented Jan 19, 2014 at 11:10
  • cue little bobby tables... Commented Jan 19, 2014 at 11:12
  • DataDirectory? Where is DataDirectory Commented Jan 19, 2014 at 11:13
  • DataDirectory is Main folder of the project. Commented Jan 19, 2014 at 11:15

1 Answer 1

1

It is not clear what you try to do and I feel like taking a risk to answer but anyway..

As I said in my comment, you should always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks.

And since your J_ID column is Number, there is no point to insert it a string with Convert.ToString(id). Using id (I assume it is an integer) will probably fine.

Also use using statement to dispose your OleDbConnection.

Try like this;

using(OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DB.accdb"))
{
  using(cmd = new OleDbCommand(@"INSERT INTO Nodes ([J_ID],[Node ID],[Node MAC],[Line Quality],[Status]) values(?, ?, ?, ?, ?", conn))
  {
    cmd.Parameters.AddWithValue("@id", id);
    cmd.Parameters.AddWithValue("@nodeid", 0);
    cmd.Parameters.AddWithValue("@nodemac", BitConverter.ToString(mac));
    cmd.Parameters.AddWithValue("@line", 0);
    cmd.Parameters.AddWithValue("@status", "Join");
    conn.Open();
    cmd.ExecuteNonQuery();
  }
}

Also think Steve's suggestion in his comment.

Sign up to request clarification or add additional context in comments.

4 Comments

Same problem. The table is still empty.
@VictorLev Updated my answer. Can you please try only my code and let me know it is working or not?
Found the problem, thanks to stackoverflow.com/users/242520/ta-speot-is Changed the "|DataDirectory|" in the the connection string to full path. Worked like charm. Your code is working perfectly and much more comfortable ... thanks for helping.
Also use using statement to dispose your OleDbConnection OleDbCommand is IDisposable too, because it's a DbCommand. Also conn.Close is redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.