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. Thanks in advance this is my code.
Solution 1, but not effective because i have many students and its not read from my database.
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
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
DBNAME
with your database name QUERY
with SELECT column-list FROM your-table
You need some kind of a tutorial or an article about loading from Database using 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.
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.