0 Replies - 601 Views - Last Post: 11 July 2019 - 02:33 PM

#1 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15260
  • View blog
  • Posts: 61,138
  • Joined: 12-June 08

Basic SQL Adapter Setup

Posted 11 July 2019 - 02:33 PM

The basic steps of returning data from a database, and dumping the dataset into a more bare bones collection for linq and other set things.

            System.Data.DataSet ds;
            string sql = string.Empty;

            List<string> myList;

            try
            {
                //1.  Open db connection
                using (SqlConnection con = new SqlConnection(""))// '-- AppSettings.Get("db")
                {
                    con.Open();

                    sql = "";// '-- 2.0  SQL statement.
                    using (SqlDataAdapter adapt = new System.Data.SqlClient.SqlDataAdapter(sql, con))
                    {
                        // 2.1 adapter parameters
                        adapt.SelectCommand.Parameters.Add("", SqlDbType.VarChar).Value = "";

                        ds = new DataSet();

                        //2.2 - fill data set
                        adapt.Fill(ds);
                    }
                }

                //3. Convert dataset to collection.
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    myList = new List<string>();

                    foreach (DataRow tempRow in ds.Tables[0].Rows)
                    {
                        myList.Add((string)tempRow[""]);
                    }
                }

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }


Is This A Good Question/Topic? 0
  • +

Page 1 of 1