搜尋

首頁  >  問答  >  主體

CommandText屬性初始化問題

我正在嘗試使用 C# 控制台應用程式同時將資料實現到 2 個(後來是 3 個)表中。我想在表格「用戶」中實現名字、姓氏和用戶ID,用戶ID將自動遞增。

同樣的 userID 也應該與 porilfeID(再次透過自動增量自動完成)和 profileName 一起實現到表「profile」。

但在某個地方它會拋出命令文字未正確初始化的錯誤,我無法再弄清楚我做錯了什麼。

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粉668804228P粉668804228226 天前605

全部回覆(1)我來回復

  • P粉337385922

    P粉3373859222024-04-05 18:22:36

    您建立 cmd = new MySqlCommand(); 但從未設定其 .CommandText 屬性。呼叫 cmd.ExecuteNonQuery(); 將會失敗,因為沒有 CommandText 可以執行。

    設定 cmd.CommandText 或將建構函式變更為 cmd = new MySqlCommand("text here", conn);

    回覆
    0
  • 取消回覆