如何在c#windows应用程序中从数据库中检索2个表的数据,值将显示在相应的文本框中 [英] how to retrieve data of 2 tables from database in c# windows application and values will show in respective textbox

查看:66
本文介绍了如何在c#windows应用程序中从数据库中检索2个表的数据,值将显示在相应的文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button2_Click(object sender, EventArgs e)
       {
           using (SqlConnection con = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True"))
           using (SqlCommand cmd = new SqlCommand("select * from JUNIOR_STUDENT where UID=@UID", con))
           {
               cmd.Parameters.AddWithValue("@UId", txtUid.Text);
               con.Open();
               using (SqlDataReader reader = cmd.ExecuteReader())
               {
                   if (reader.Read())
                   {
                       txtName.Text = reader["STUDENT_NAME"].ToString();
                       txtRoll.Text = reader["ROLL_NO"].ToString();
                       txtStream.Text = reader["STREAM"].ToString();
                       txtGender.Text = reader["GENDER"].ToString();
                       cbxYearname.Text = reader["YEAR_ID"].ToString();
                       txtRemarks.Text = reader["REMARKS"].ToString();
                       mtxtAdmsndate.Text = reader["ADMISSION_DATE"].ToString();
                       txtAcademicYear.Text = reader["ACADEMIC_YEAR"].ToString();
                       txtAdmissiontype.Text = reader["ADMISSION_TYPE"].ToString();
                       cbxCategory.Text = reader["CATEGORY_ID"].ToString();
                       txtSubcategory.Text = reader["SUB_CATEGORY"].ToString();
                       cbxCourse.Text = reader["CLASS_ID"].ToString();
                       reader.Close();
                   }
// i hav no idea what i m doing...
                   using (SqlConnection cn = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True"))
                   using (SqlCommand cm = new SqlCommand("select * from PAYMENT where UID=@UID", con))
                   {
                       cmd.Parameters.AddWithValue("@UId", txtUid.Text);
                       cn.Open();
                       using (SqlDataReader read = cm.ExecuteReader())
                       {

                           if (read.Read())
                           {
                               textBox7.Text = reader["CHEQUE_NO"].ToString();
                               textBox8.Text = reader["BANK_NAME"].ToString();


                           }

                       }
                   }

                   MessageBox.Show("Student : " + txtName.Text + " Transfer of Subject Successfull ", "Student Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }
           }

       }
   }



someone pls help me 
thankss in advance

推荐答案

正如Wes所指出的,在你的第二套代码你实例化一个名为读的变量,但然后尝试从读者获取数据



你需要做的就是设置一个断点并逐行执行并观察它的作用。如果你这样做,你会学到很多东西。



另一种选择是在一个sql语句中返回2个表,然后从第一个表中读取后再用call reader.NextResult()。这将加载下一个表格。



参见 http://msdn.microsoft.com/en-us/library/haa3afyz(v = vs.110).aspx [ ^ ]



http:/ /msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.nextresult(v=vs.110).aspx [ ^ ]
As pointed out by Wes, in your second set of code you instantiate a variable named "read" but then try to get data from "reader"

All you need to do is put a breakpoint and step through line by line and watch what it does. You'll learn a lot if you do that.

The other option is to return 2 tables in one sql statement and then after you read from the first table you then call reader.NextResult(). That will load up the next table.

See http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.110).aspx[^]
and
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.nextresult(v=vs.110).aspx[^]


您已经在第一个命令中不必要地嵌套了第二个命令,这根本不需要。你也应该使用 SqlDataReader 对象读取而不是 reader in第二块代码。



按照以下建议操作......

You have unnecessarily nested the second command inside the first command, which is not required at all. Also you should use SqlDataReader Object read instead of reader in the second block of code.

Do as suggested below...
using (SqlConnection con = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True"))
{
   using (SqlCommand cmd = new SqlCommand("select * from JUNIOR_STUDENT where UID=@UID", con))
   {
       cmd.Parameters.AddWithValue("@UId", txtUid.Text);
       con.Open();
       using (SqlDataReader reader = cmd.ExecuteReader())
       {
           if (reader.Read())
           {
               txtName.Text = reader["STUDENT_NAME"].ToString();
               txtRoll.Text = reader["ROLL_NO"].ToString();
               txtStream.Text = reader["STREAM"].ToString();
               txtGender.Text = reader["GENDER"].ToString();
               cbxYearname.Text = reader["YEAR_ID"].ToString();
               txtRemarks.Text = reader["REMARKS"].ToString();
               mtxtAdmsndate.Text = reader["ADMISSION_DATE"].ToString();
               txtAcademicYear.Text = reader["ACADEMIC_YEAR"].ToString();
               txtAdmissiontype.Text = reader["ADMISSION_TYPE"].ToString();
               cbxCategory.Text = reader["CATEGORY_ID"].ToString();
               txtSubcategory.Text = reader["SUB_CATEGORY"].ToString();
               cbxCourse.Text = reader["CLASS_ID"].ToString();
               reader.Close();
           }
        } // Added.
    } // Added
} // Added

using (SqlConnection cn = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True"))
{
   using (SqlCommand cm = new SqlCommand("select * from PAYMENT where UID=@UID", con))
   {
       cmd.Parameters.AddWithValue("@UId", txtUid.Text);
       cn.Open();
       using (SqlDataReader read = cm.ExecuteReader())
       {
           if (read.Read())
           {
               textBox7.Text = read["CHEQUE_NO"].ToString();
               textBox8.Text = read["BANK_NAME"].ToString();
           }
       }
   }
}


这篇关于如何在c#windows应用程序中从数据库中检索2个表的数据,值将显示在相应的文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆