G Gr
G Gr

Reputation: 6080

add data to sql database? Using odbc

High Im unsure how to add insert data to mysql using odbc?

I have a table called User and I would like to add generic details to it, Name, location etc etc

  {
      string = textbox1.text("Name");

      OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite; User=root; Password=;");
      cn.Open();
OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Name)");

      cmd.Parameters.Insert(Name into @name);

    }

    }
}

Upvotes: 0

Views: 3807

Answers (1)

Abe Miessler
Abe Miessler

Reputation: 85056

I'm not quite sure what you are asking but I see a few problems with your code:

  string tbValue = textbox1.text("Name"); //added variable name

  OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite; User=root; Password=;");
  cn.Open();
  OdbcCommand cmd = 
        new OdbcCommand("INSERT INTO User (Name) VALUES (?)"); // fixed incomplete insert statement.

  cmd.Parameters.Add(tbValue); //add the parameter to the command
  cmd.ExecuteNonQuery(); //actually run the sql

There may be other problems but give this a try for starters and let us know if you have any specific problems.

Upvotes: 2

Related Questions