C # Connect database SQL and convert to list form

Source: Internet
Author: User
Tags connectionstrings

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.