无法在C#

时间:2017-10-09 13:14:50

标签: c# visual-studio-2015

大家好我想在这里问一下我的代码。 我带来了一些继承了彼此的课程。

数据库登录类:

public void DB_login(string username, string pwd)
    {
        string q_log = "select* from anggotaan where NIM ='" + username + "' and UserPassword = '" + pwd + "'";
        try
        {
            connect.Open();
            MySqlCommand cmd = new MySqlCommand(q_log, connect);
            MySqlDataReader rdlog = cmd.ExecuteReader();
            int count = 0;
            while (rdlog.Read())
            {
                count = +1;
            }
            if (count == 1)
            {
                rdlog.Close();
                MessageBox.Show("Selamat datang, " + username);
                this.ident = idx.nimreturn();
                hm.Show();
                connect.Close();
                ShowIdentitas(username,pwd);
                idx.Hide();
            }
            else
            {
                rdlog.Close();
                MessageBox.Show("Login Gagal");
            }
            connect.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

这是ShowData类,用于显示从DB到新表单的单个数据。

        public void ShowIdentitas(string username , string pwd)
    {
        MySqlDataReader see;
        string query = "SELECT `NIM`,`NamaLengkap`,`Prodi`,`Angkatan` FROM `anggotaan` WHERE NIM ='"+username+"'";
        query.ToString();
        StringBuilder bd = new StringBuilder();

        try
        {
            connect.Open();
            MySqlCommand lihat = new MySqlCommand(query, connect);
            see = lihat.ExecuteReader();
            while (see.Read())
            {
                bd.Append("NIM\t\t:" + see[0] + "\nNamaLengkap\t\t:" + see[1] + "\nProdi\t\t:" + see[2] + "\nAngkatan\t\t:" + see[3]);
            }
            see.Close();
            hm.ident.Text = bd.ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        connect.Close();            
    }

这是索引表单,用户可以在其中注册并登录。

namespace BukuAnggotaNew {
   public partial class index : Form
       {
    public string NIM, password, nama, prodi, angkatan,nimlog,passlog;
    DBOps DBO = new DBOps();
    public string ident;
    public index()
    {
        InitializeComponent();
    }

    private void dft_pass_TextChanged(object sender, EventArgs e)
    {
        this.password = dft_pass.Text;
    }

    private void loginNIM_TextChanged(object sender, EventArgs e)
    {
        this.nimlog = loginNIM.Text;
    }

    private void loginPass_TextChanged(object sender, EventArgs e)
    {
        this.passlog = loginPass.Text;
    }

    private void dft_nama_TextChanged(object sender, EventArgs e)
    {
        this.nama = dft_nama.Text;
    }

    private void years_TextChanged(object sender, EventArgs e)
    {
        this.angkatan = years.Text;
    }

    private void dft_Prodi_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dft_Prodi.SelectedIndex == -1)
        {
            MessageBox.Show("Pilih Prodi yang sesuai");

        }
        else {
            this.prodi = this.DBO.prodi = dft_Prodi.SelectedItem.ToString();

        }
    }

    private void daftarpage_Click(object sender, EventArgs e)
    {

        DBO.DBDaftar(NIM, password, nama, prodi, angkatan);
        dft_nama.Text="";
        dft_NIM.Text="";
        dft_pass.Text="";
        years.Text="";            
    }

    private void Login_Click(object sender, EventArgs e)
    {            
        DBO.DB_login(nimlog,passlog);
        DBO.ShowIdentitas();
    }

    private void dft_NIM_TextChanged(object sender, EventArgs e)
    {
        this.NIM = dft_NIM.Text;
    }
    public string nimreturn()
    {
        this.nimlog = loginNIM.Text;
        return nimlog;
    }
}

}

主页表单,用户身份必须显示。

namespace BukuAnggotaNew
  {
public partial class home : Form
{
    DBOps dba = new DBOps();
    public string homes;
    public home()
    {
        InitializeComponent();

    }

    private void label3_Click(object sender, EventArgs e)
    {
    }

    private void Ubah_Click(object sender, EventArgs e)
    {
    }

    private void button2_Click(object sender, EventArgs e)
    {                       
    }

    private void identity_Click(object sender, EventArgs e)
    {
    }
    private void identityShow()
    {
    }

    private void identitas_Click(object sender, EventArgs e)
    {
    }

    private void splitContainer5_Panel2_Paint(object sender, PaintEventArgs e)
    {
    }

    private void Hapus_Click(object sender, EventArgs e)
    {
    }

    private void Ubah_Click_1(object sender, EventArgs e)
    {
    }
}

}

运行VS后,它运行良好,但新表单只是打开并显示空白部分,其中必须显示用户身份。我需要帮助,有人可以教我修复此代码吗?这是我课上的最终项目。这是我第一次使用C#。谢谢

1 个答案:

答案 0 :(得分:0)

问题是两种呼叫方法,一种是在索引'课程(真的没必要)和DBOps'类。 方法调用Index类,在我检查两次后,它通过DBOps登录方法的方法调用产生问题

所以我改为:

在索引类中,该方法将更改为:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>

/* Use readable names */
#define NUM_OF_AUTOMOBIL 5

typedef enum {
    Bialy = 0,
    Czarny,
    Czerwony,
    Niebieski,
} Kolor;

/* Use predefined array of string.
 * Look at printf() to see how to use it to simplify your code
 */
const char *KolorStr[] = {
    "Bialy",
    "Czarny",
    "Czerwony",
    "Niebieski"
};

typedef enum {
    Podstawowe = 0,
    Standard,
    Premium
} Wyposazenie;

const char *WyposazenieStr[] = {
    "Podstawowe",
    "Standard",
    "Premium"
};

typedef struct Samochod {
    int waga;
    int cena;
    Kolor kolor;
    Wyposazenie wyposazenie;
} Samochod;

int losuj_liczbe(int min, int max)
{
    assert(max != min);

    if (max < min) {
        int temp = min - max;
        min = max;
        max = temp;
    }

    /* Use parenthes even if you completely sure of precedence, this allows
     * to avoid mistakes during code modification
     */
    return ((rand() % (max - min)) + min);
}


int main(int argc, char *argv[])
{
    Samochod automobil[NUM_OF_AUTOMOBIL];

    /* Init random generator once over all your program
     */
    srand(time(NULL));

    for (int i = 0; i < NUM_OF_AUTOMOBIL; i++) {
        automobil[i].waga = losuj_liczbe(500, 1500);
        automobil[i].cena = losuj_liczbe(20000, 30000);
        /* You can convert int into enum directly if
         * limit int with enum values only */
        automobil[i].kolor = losuj_liczbe(Bialy, Niebieski);
        automobil[i].wyposazenie = losuj_liczbe(Podstawowe, Premium);
    }

    /* Use loop over your array to show values
     */
    for (int i = 0; i < NUM_OF_AUTOMOBIL; i++) {
        /* Use \n to terminate your lines
         */
        printf("%d | %d | %s | %s\n",
                automobil[i].waga,
                automobil[i].cena,
                KolorStr[automobil[i].kolor],
                WyposazenieStr[automobil[i].wyposazenie]);
    }

    return 0;
}

在DBO Class中,该方法将与原样相同。