简体   繁体   English

如何在C#中使用数据表从SQL Server检索表中的数据

[英]How do I use the datatable to retrieve data in my table from SQL Server in c #

i want to retrieve data from my database to my window that displays all information about a student in the "Student View" where I choose a studentid and then fill all the information. 我想从数据库中检索数据到我的窗口,该窗口在“学生视图”中显示有关学生的所有信息,在这里我选择一个学生ID,然后填写所有信息。 Thanks in advance this is my code. 预先感谢,这是我的代码。

Solution 1, but not effective because i have many students and its not read from my database. 解决方案1,但没有效果,因为我有很多学生,并且没有从我的数据库中读取它。

public override DataTable getStudentData()
    {    
    DataTable dt = new DataTable();
    dt.Columns.Add("StudentID");
    dt.Columns.Add("FirstName");
    dt.Columns.Add("LastName");
    dt.Columns.Add("Gender");
    dt.Columns.Add("Streetadress");
    dt.Columns.Add("ZipCode");
    dt.Columns.Add("Birthdate");
    dt.Columns.Add("StudentType");
    dt.Columns.Add("City");
    dt.Columns.Add("Country");
    dt.Columns.Add("program");
    dt.Columns.Add("PgmStartYear");
    dt.Columns.Add("credits");
    dt.Rows.Add("studentid", "Firstname", "Lastname", "Gender", "Adress", "Zipcode", "Birthdate", "Studenttype ", "City", "Country", "Programname", Startyear, credits);

return dt;
}

Solution that i want to be possible is something like this: 我想成为可能的解决方案是这样的:

        DataTable dt = new DataTable();
        dt.Rows.Add(["studentid"].ToString());
        dt.Rows.Add(["firstname"].ToString());
        dt.Rows.Add(["lastname"].ToString());
        dt.Rows.Add(["gender"].ToString());
        dt.Rows.Add(["birthdate"].ToString());
        dt.Rows.Add(["streetadress"].ToString());
        dt.Rows.Add(["zipcode"].ToString());
        dt.Rows.Add(["country"].ToString());
        dt.Rows.Add(["city"].ToString());
        dt.Rows.Add(["studenttype"].ToString());
        dt.Rows.Add(["programname"].ToString());
        dt.Rows.Add(["year"].ToString());

You can use following function to retrieve data from SQL Server 您可以使用以下功能从SQL Server检索数据

using System.Data;
using System.Data.SqlClient;

public DataTable StudentView()
{
    SqlConnection sqlCon = new SqlConnection(@"Server= localhost\SQLINSTANCENAME; Database= DBNAME; Integrated Security=True;");

    SqlDataAdapter sqlDa = new SqlDataAdapter("QUERY", sqlCon);

    DataTable dtbl = new DataTable();
    sqlDa.Fill(dtbl);

    return dtbl;
}

Replace 更换

  • SQLINSTANCENAME with your own SQL Server instance name, eg SQLEXPRESS , SQL2008 具有您自己的SQL Server实例名称的SQLINSTANCENAME ,例如SQLEXPRESSSQL2008
  • DBNAME with your database name 具有您的数据库名称的DBNAME
  • QUERY with SELECT column-list FROM your-table QUERYSELECT column-list FROM your-table

You need some kind of a tutorial or an article about loading from Database using C#.... 您需要某种有关使用C#从数据库加载的教程或文章。

The suggested answer before is one way (and will work perfectly)... and you can find another way on this link : 之前建议的答案是一种方法(并且可以完美运行)...,您可以在此链接上找到另一种方法:

Although since your are new to C# and still learning, i highly recommend you to follow the "Solution 2" provided in page which uses stored procedures instead of direct SQL, since its the correct way to connect to database. 尽管由于您是C#的新手并且仍在学习,但我还是强烈建议您遵循页面中提供的“解决方案2”,该解决方案使用存储过程而不是直接SQL,因为它是连接数据库的正确方法。

Thank you all. 谢谢你们。 I have already connected to my database and try something like this: 我已经连接到数据库,然后尝试执行以下操作:

           try
            {
            SqlCommand read = new SqlCommand();
            read.CommandType = CommandType.Text;
            read.Connection = connect;
            read.CommandText = "SELECT * FROM [viewstudent]";
            SqlDataReader get;
            get = read.ExecuteReader();

            while (get.Read())
            {
                dt.Rows.Add(["studentid"].ToString(); get["firstname"].ToString(); get["lastname"].ToString(); get["gender"].ToString(); get["birthdate"].ToString(); get["streetadress"].ToString(); get["zipcode"].ToString(); get["country"].ToString();
                get["city"].ToString(); get["studenttype"].ToString(); get["programname"].ToString(); get["year"].ToString(); get["credits"].ToString();
            }

            MessageBox.Show("Good!");

        }
        catch (Exception e)
        {
            MessageBox.Show("Error try again!");
        }
        connect.Close();

        return dt;
    }      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#将数据从SQL Server检索到DataTable - how to retrieve data from sql server to DataTable using c# C#和SQL Server:如何通过使用C#调用存储过程来从SQL表中获取值? - C# and SQL Server: How do I get values from my SQL Table, by calling my stored procedure with C#? 我想从数据库中检索多个数据。 我正在使用C#和SQL Server - I want to retrieve multiple data from my data base. Im using C# and sql server 如何在C#中从SQL Server使用DataTable - How to use DataTable from SQL Server in C# 如何从集合中创建DataTable以将其用作SQL表参数? - How do I make a DataTable from a collection to use it as an SQL table parameter? 如何从桌面应用程序中Web服务器上托管的SQL Server数据库中检索数据? - How I Retrieve Data from SQL Server Database hosted on a Web Server in my desktop application? 从SQL Server到DataTable C#检索列名和类型 - Retrieve column names and types from SQL Server to DataTable C# 如何使用if语句使用C#从SQL Server检索一行数据 - how to use if statement to retrieve a row of data from sql server using c# 从SQL Server作为Excel DataTable检索varbinary数据 - Retrieve varbinary data from SQL Server as Excel DataTable 在SQL Server中检索表的DataTable信息 - Retrieve DataTable info for a table in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM