[英]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
SQLINSTANCENAME
,例如SQLEXPRESS
, SQL2008
DBNAME
with your database name DBNAME
QUERY
with SELECT column-list FROM your-table
QUERY
用SELECT 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.