c# - Import values from the database to a table or something better -
I have a question about using the database ...
string node = Session ["node"] ToString () .; SqlConnection Data Connection = New SqlConnection (); DataConnection.ConnectionString = ConfigurationManager.ConnectionStrings ["SumooHAgentDBConnectionString"]. ConnectionString; SqlCommandDataCom = New SqlCommand (); Data Commands Connection = data connection; DataCommand.CommandText = ("Select * from xyz where xyzID = xyzID and path type = 0") Data Commands. Parameter. Advant Value ("@xyzID", node); DataConnection.Open (); DataCommand.ExecuteNonQuery (); DataConnection.Close ();
Now the table has 5 columns which should return 5 rows.
Then I want to add these values to a table or a simple list so that I can use this value in my code later ...
What is the best way to do this? Line Thank you
You can add them to a DataTable
- you have 5 There are five columns in the line with a .ows
property, and each DataRow
in the .ows
property that holds those values. Almost like the database in memory.
To do this, simply use SqlDataAdapter
and its fill
method:
data for myTable = new Datatile (); SqlDataAdapter dap = New SqlDataAdapter (Data Command); Dap.Fill (mytable);
Or you can read the rows and values with the DataReader
and then take them, and each line will have any type of "pocket-it-id" And with those five types of values, call a constructor on that object type, and its resulting five objects are List & lt; WhateverItIs & gt; Paste in
and you originally created your small ORM (object-relational mapper). / P>
For this you will need to use the SqlDataReader
and do something like this:
SqlDataReader dr = dataCommand.ExecuteReader (); & Lt; Unit & gt; _list = new list & lt; Unit & gt; (); While (Dr. Reid ()) {string column1 = dr.GetString (0); ... String column 5 = Dr. GetString (4); Unit newEntity = new unit (column1, ......., column5); _list.Add (newEntity); }
Works best for you - Datatelle is less work in the initial loading phase, but later accessing data is a bit more cumbersome ( MyDataTable.Rows [3 ] Value [
Comments
Post a Comment