C# with MySQL INSERT parameters

后端 未结 9 1951
-上瘾入骨i
-上瘾入骨i 2020-11-28 10:47

Good day to all, I\'m using Visual C# 2010 and MySQL Version 5.1.48-community. I hope you can help me with this code. I don\'t find it working on me. What am I missing?

相关标签:
9条回答
  • 2020-11-28 11:29

    Use the AddWithValue method:

    comm.Parameters.AddWithValue("@person", "Myname");
    comm.Parameters.AddWithValue("@address", "Myaddress");
    
    0 讨论(0)
  • 2020-11-28 11:32

    You may use AddWithValue method like:

    string connString = ConfigurationManager.ConnectionStrings["default"].ConnectionString;
    MySqlConnection conn = new MySqlConnection(connString);
    conn.Open();
    MySqlCommand comm = conn.CreateCommand();
    comm.CommandText = "INSERT INTO room(person,address) VALUES(@person, @address)";
    comm.Parameters.AddWithValue("@person", "Myname");
    comm.Parameters.AddWithValue("@address", "Myaddress");
    comm.ExecuteNonQuery();
    conn.Close();
    

    OR

    Try with ? instead of @, like:

    string connString = ConfigurationManager.ConnectionStrings["default"].ConnectionString;
    MySqlConnection conn = new MySqlConnection(connString);
    conn.Open();
    MySqlCommand comm = conn.CreateCommand();
    comm.CommandText = "INSERT INTO room(person,address) VALUES(?person, ?address)";
    comm.Parameters.Add("?person", "Myname");
    comm.Parameters.Add("?address", "Myaddress");
    comm.ExecuteNonQuery();
    conn.Close();
    

    Hope it helps...

    0 讨论(0)
  • 2020-11-28 11:34

    try this it is working

     MySqlCommand dbcmd = _conn.CreateCommand();
        dbcmd.CommandText = sqlCommandString;
        dbcmd.ExecuteNonQuery();
        long imageId = dbcmd.LastInsertedId;
    
    0 讨论(0)
  • 2020-11-28 11:35
    comm.Parameters.Add("person", "Myname");
    
    0 讨论(0)
  • 2020-11-28 11:40

    What I did is like this.

    String person;
    String address;
    person = your value;
    address =your value;
    string connString = ConfigurationManager.ConnectionStrings["default"].ConnectionString;
        MySqlConnection conn = new MySqlConnection(connString);
        conn.Open();
        MySqlCommand comm = conn.CreateCommand();
        comm.CommandText = "INSERT INTO room(person,address) VALUES('"+person+"','"+address+"')";
        comm.ExecuteNonQuery();
        conn.Close();
    
    0 讨论(0)
  • 2020-11-28 11:42

    Three things: use the using statement, use AddWithValue and prefix parameters with ? and add Allow User Variables=True to the connection string.

     string connString = ConfigurationManager.ConnectionStrings["default"].ConnectionString;
     using (var conn = new MySqlConnection(connString))
     {
          conn.Open();
          var comm = conn.CreateCommand();
          comm.CommandText = "INSERT INTO room(person,address) VALUES(@person, @address)";
          comm.Parameters.AddWithValue("?person", "Myname");
          comm.Parameters.AddWithValue("?address", "Myaddress");
          comm.ExecuteNonQuery();
      }
    

    Also see http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx for more information about the command usage, and http://dev.mysql.com/doc/refman/5.1/en/connector-net-connection-options.html for information about the Allow User Variables option (only supported in version 5.2.2 and above).

    0 讨论(0)
提交回复
热议问题