CommandText property initialization problem
P粉668804228
P粉668804228 2024-04-04 22:43:24
0
1
389

I'm trying to materialize data into 2 (later 3) tables simultaneously using a C# console application. I want to implement first name, last name and user ID in table "Users", the user ID will be automatically incremented.

The same userID should also be implemented into table "profile" along with porilfeID (again done automatically via auto-increment) and profileName.

But somewhere it throws an error that the command text is not initialized correctly and I can no longer figure out what I'm doing wrong.

class SQLCreate
{
    public void create(int entries)
    {
        string ConnectionString = "server=localhost;uid=root;pwd=;database=databaseassignment;";

        MySqlConnection conn;
        MySqlCommand cmd;
        MySqlDataAdapter adapter;

        conn = new MySqlConnection();

        int entryValue = entries;
        conn.ConnectionString = ConnectionString;

        try
        {
            Stopwatch stopw = new Stopwatch();
            stopw.Start();
            conn.Open();

            cmd = new MySqlCommand();
            adapter = new MySqlDataAdapter();

            cmd.Connection = conn;

            for (int i = 0; i < entryValue; i++)
            {
          
                MySqlCommand cmd1 = new MySqlCommand("INSERT INTO user (firstName, lastName) VALUES (@firstName, @lastName)", conn);
                //MySqlCommand cmd1 = new MySqlCommand("INSERT INTO user (firstName, lastName) VALUES (@firstName, @lastName)", conn);
    
    
                cmd1.Parameters.AddWithValue("@firstName", "John");
                cmd1.Parameters.AddWithValue("@lastName", "Doe");
    
                cmd1.CommandType = CommandType.Text;
    
                int userId = Convert.ToInt32(cmd1.ExecuteScalar());
    
                MySqlCommand cmd2 = new MySqlCommand("INSERT INTO profile (userId, profileName) VALUES (@userId, @profileName)", conn);
    
                cmd2.Parameters.AddWithValue("@userId", userId);
                cmd2.Parameters.AddWithValue("@profileName", "John Doe");
                    
                cmd2.CommandType = CommandType.Text;
    
                cmd2.ExecuteNonQuery();
    
                string firstName = Faker.Name.First();
                string lastName = Faker.Name.Last();
                string profileName = Faker.Name.First();
    
                cmd.Parameters.Add("@firstName", MySqlDbType.String);
                cmd.Parameters["@firstName"].Value = firstName;
    
                cmd.Parameters.Add("@lastName", MySqlDbType.String);
                cmd.Parameters["@lastName"].Value = lastName;
    
                cmd.Parameters.Add("@profileName", MySqlDbType.String);
                cmd.Parameters["@profileName"].Value = profileName;
    
                cmd.ExecuteNonQuery();
            }
            conn.Close();
            stopw.Stop();
            Console.WriteLine(" Time elapsed: {0} ", stopw.Elapsed);
        } catch (MySql.Data.MySqlClient.MySqlException ex) {
            Console.WriteLine(ex.Message);
        }
    }
}
}

P粉668804228
P粉668804228

reply all(1)
P粉337385922

You create cmd = new MySqlCommand(); but never set its .CommandText property. Calling cmd.ExecuteNonQuery(); will fail because there is no CommandText to execute.

Set cmd.CommandText or change the constructor to cmd = new MySqlCommand("text here", conn);.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!