0

データベースから結果を取得するデータレイヤーと、ラベルコントロールにデータを表示するUIレイヤーの2つのレイヤーがあります。データレイヤーのコードは次のとおりです

public class Test_DAL
{
    public DataSet getEMP(string name1)
    {

        string conn = System.IO.File.ReadAllText("Connect.txt");
        //string conn="Data Source=.\\sqlexpress;Initial Catalog=pss;Integrated Security=True";
        //string conn = ConfigurationManager.ConnectionStrings["constr"].ToString();
        SqlConnection dbconn = new SqlConnection(conn);
        dbconn.Open();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = new SqlCommand("select * from pss where Emp_code='"+name1+"'", dbconn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        dbconn.Close();
        return ds;
    }

}

そしてUIレイヤーのコードです

    private void btn_search_Click(object sender, EventArgs e)
    {

        string empcode=txt_empcode.Text;
        Test_DAL obj1= new Test_DAL();
        obj1.getEMP(empcode);
        //dg.DataSource = obj1.getEMP(empcode).Tables[0];


    }

そして私の質問は、ラベルにデータを表示する方法です

事前にどうもありがとう

4

2 に答える 2

3

あなたの要件はあまり明確ではありません。あなたができる方法のアイデアを提供するだけです

private void btn_search_Click(object sender, EventArgs e)
    {

        string empcode=txt_empcode.Text;
        Test_DAL obj1= new Test_DAL();
        DataSet ds = obj1.getEMP(empcode);
        //displays the data of row 1 column 1 of table 1
        yourLabel.Text  = ds.Tables[0].Rows[0][0].ToString(); 


    }

注: これは最善の方法でも適切な方法でもありません。例外処理などはありません。

于 2013-12-30T10:20:27.957 に答える
0

戻り値をGetEmployee実際のEmployee型にします。

public Employee getEMP(string name1)
{
    // left out some of your code intentionally
    da.SelectCommand = new SqlCommand("select * from pss where Emp_code='"+name1+"'", dbconn);
    DataSet ds = new DataSet();
    da.Fill(ds);
    dbconn.Close();
    return new Employee(ds);
}

Employeeそして、次のようなクラスを定義します

public class Employee
{
    public string Name { get; set; }
    /* Other properties ... */ 

    public Employee(DataSet dataset)
    {
        Name = ds.Tables[0].Rows[0][0].ToString();
        // etc
    }
}

そうすれば、UI レイヤーは s のマジック ナンバーに煩わされることなく、実際のオブジェクト (エンティティ) と対話できますDataSet。たとえば、UI のデータ バインディングに依存します。

private void btn_search_Click(object sender, EventArgs e)
{
    string empcode=txt_empcode.Text;
    Test_DAL obj1= new Test_DAL();

    var employee = obj1.getEMP(empcode);

    dg.DataSource = employee;
}
于 2013-12-30T10:29:16.273 に答える