Web Config configuration
<connectionStrings>
<add name= "sqlconnstring" connectionstring= "Data source= database account; Initial catalog= database; User id= username; password= password "providername=" System.Data.SqlClient "/>
</connectionStrings>
String connectionString = configurationmanager.connectionstrings["sqlconnstring"]. ToString ();//Connection string
SqlConnection cn = new SqlConnection (connectionString);//Database connection
String sql = "Select Cameraid,cameraname,camerano,defence,deviceid,devicename,deviceserial,isencrypt,isshared, Picurl,status,videolevel from Video ";
var dataset = Query (sql, connectionString);
var lstvideo = Datasettoilist<camera. Videolist> (DataSet, 0);
<summary>////Execute Query statement, return DataSet///</summary>//<param name= "sqlstr ing "> Query statements </param>//<returns>DataSet</returns> public static DataSet query (ST Ring SQLString, String connectionString) {using (SqlConnection connection = new SqlConnection (connectio nstring)) {DataSet ds = new DataSet (); try {connection. Open (); SqlDataAdapter command = new SqlDataAdapter (SQLString, connection); Command. Fill (ds, "DS"); } catch (System.Data.SqlClient.SqlException E) {throw new Exception (e.me Ssage); ITNB. Base.Error.showError (E.message.tostring ()); } return DS; }}///<summary>//DataSet for generic collection///</summary>//<typeparam name= "T" ></typeparam>///<param name= "P_dataset" >dataset</param >//<param name= "P_tableindex" > Data table Index to convert </param>///<returns></returns> public static list<t> datasettoilist<t> (DataSet p_dataset, int p_tableindex) {if (p_data Set = = NULL | | P_DataSet.Tables.Count < 0) return null; if (P_tableindex > P_dataset.tables.count-1) return null; if (P_tableindex < 0) P_tableindex = 0; DataTable p_data = P_dataset.tables[p_tableindex]; Return value initialization list<t> result = new list<t> (); for (int j = 0; J < P_Data.Rows.Count; J + +) {T _t = (t) activator.createinstance (typeof (T)); propertyinfo[] Propertys = _t.gettype (). GetProperties (); foreach (PropertyInfo pi in Propertys) {for (int i = 0; i < P_Data.Columns.Count; i++) { The property is assigned the same value as the field name (the enumeration type cannot be converted and is carried out separately) if (pi. Name.equals (P_data.columns[i]. ColumnName)) {//database null value is handled separately if (P_DATA.R Ows[j][i]! = DBNull.Value) pi. SetValue (_t, p_data.rows[j][i], NULL); else Pi. SetValue (_t, NULL, NULL); Break }}} result. ADD (_t); } return result; }
C # Connect database SQL and convert to list form