CommandText 속성 초기화 문제
P粉668804228
P粉668804228 2024-04-04 22:43:24
0
1
394

C# 콘솔 응용 프로그램을 사용하여 데이터를 2개(나중에 3개) 테이블로 동시에 구체화하려고 합니다. "Users" 테이블에 이름, 성, 사용자 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粉668804228
P粉668804228

모든 응답(1)
P粉337385922

실행 준비가 된 cmd = new MySqlCommand(); 但从未设置其 .CommandText 属性。调用 cmd.ExecuteNonQuery(); 将失败,因为没有 CommandText을 만듭니다.

설정cmd.CommandText 或将构造函数更改为 cmd = new MySqlCommand("text here", conn);.

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!