如何在c#windows应用程序中从数据库中检索数据 [英] how to retrieve data from database in c# windows application

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

问题描述

private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=ROHIT-PC\\SQLEXPRESS;Initial Catalog=FMS;Integrated Security=True");
          
             SqlCommand cmd = new SqlCommand("select * from JUNIOR_STUDENT where @UID='+ @ " + txtUid.Text.Trim() + "'",con);
             cmd.CommandType = CommandType.Text;
             cmd.Parameters.AddWithValue("@UId", txtUid.Text);
             cmd.Parameters.AddWithValue("@NAME", txtName.Text);
             cmd.Parameters.AddWithValue("@ROLL_NO", txtRoll.Text);
             cmd.Parameters.AddWithValue("@STREAM", txtStream.Text);
             cmd.Parameters.AddWithValue("@GENDER", txtGender.Text);
             cmd.Parameters.AddWithValue("@YEAR_ID", cbxYearname.Text);
             cmd.Parameters.AddWithValue("@REMARKS", txtRemarks.Text);
             cmd.Parameters.AddWithValue("@ADMISSION_DATE", mtxtAdmsndate.Text);
             cmd.Parameters.AddWithValue("@ACADEMIC_YEAR", txtAcademicYear.Text);
             cmd.Parameters.AddWithValue("@ADMISSION_TYPE", txtAdmissiontype.Text);
             cmd.Parameters.AddWithValue("@CATEGORY_ID", cbxCategory.Text);
             cmd.Parameters.AddWithValue("@SUB_CATEGORY", txtName.Text);
             cmd.Connection = con;
             con.Open();
             
             SqlDataReader reader = cmd.ExecuteReader();
              while (reader.Read())
              {
              txtName.Text=reader["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();
              
              
              reader.Close();
              con.Close();
              MessageBox.Show("Student : " + txtName.Text + " Transfer of Subject Successfull ", "Student Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
                  }

它没有显示我能做什么?

it show nothing wat can i do ??

推荐答案

你没有提供足够的信息你的问题。



读者是否包含任何内容?没有!查看您的查询:

You did not provide enough information about your issue.

Does the reader contains anything? No! Have a look at your query:
"select * from JUNIOR_STUDENT where @UID='+ @ " + txtUid.Text.Trim() + "'"



您的查询应该是:


Your query should be:

select [Name], RollNo, Stream, Gender, Year_ID, Remarks, Admission_Date, Academic_Year, Admission_tye, Category_ID, Sub_Category
from JUNIOR_STUDENT
where UID=@UID





最后,你只使用ine参数 @UID ,那么为什么要设置 [Name] Rollno 等等?



BTW:名字是保留字,用括号括起来!



更正的代码:



Finally, you are using only ine parameter @UID, so why do you set [Name], Rollno, etc?

BTW: Name is a reserved word, use it with brackets!

Corrected code:

SqlCommand cmd = new SqlCommand("select * from JUNIOR_STUDENT where UDI=@UID",con);
             cmd.CommandType = CommandType.StoredProcedure;
             cmd.Parameters.AddWithValue("@UId", txtUid.Text);
             con.Open();
             SqlDataReader reader = cmd.ExecuteReader();





这是一个基本想法:如何:使用Visual C#.NET在ASP.NET中调用SQL Server存储过程 [ ^ ]。这是真的,它是ASP.NET,但逻辑是一样的!



Here is a basic idea: HOW TO: Call SQL Server Stored Procedures in ASP.NET by Using Visual C# .NET[^]. It's true, it's ASP.NET, but the logic is the same!


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

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