コード例 #1
0
ファイル: CustomerMethods.cs プロジェクト: id18nvk/WebApi
        public List <CustomerDetails> GetCustomers(out string errormsg)
        {
            //SqlConnection
            SqlConnection dbConnection = new SqlConnection();

            //koppling
            dbConnection.ConnectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=BoardgamesDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";

            //sqlstring och lägg till en skribent i databasen
            String sqlstring = "SELECT * FROM Tbl_Customer";

            SqlCommand dbCommand = new SqlCommand(sqlstring, dbConnection);

            //Skapa en adapter
            SqlDataAdapter myAdapter = new SqlDataAdapter(dbCommand);
            DataSet        myDS      = new DataSet();

            List <CustomerDetails> CustomerList = new List <CustomerDetails>();

            try
            {
                dbConnection.Open();

                myAdapter.Fill(myDS, "Customer");
                int count = 0;
                int i     = 0;

                count = myDS.Tables["Customer"].Rows.Count;

                if (count > 0)
                {
                    while (i < count)
                    {
                        CustomerDetails cd = new CustomerDetails();
                        cd.Cu_Id        = Convert.ToInt16(myDS.Tables["Customer"].Rows[i]["Cu_Id"]);
                        cd.Cu_Firstname = myDS.Tables["Customer"].Rows[i]["Cu_Firstname"].ToString();
                        cd.Cu_Lastname  = myDS.Tables["Customer"].Rows[i]["Cu_Lastname"].ToString();
                        cd.Cu_Town      = myDS.Tables["Customer"].Rows[i]["Cu_Town"].ToString();
                        cd.Cu_Address   = myDS.Tables["Customer"].Rows[i]["Cu_Address"].ToString();
                        cd.Cu_Zipcode   = myDS.Tables["Customer"].Rows[i]["Cu_Zipcode"].ToString();
                        cd.Cu_Email     = myDS.Tables["Customer"].Rows[i]["Cu_Email"].ToString();
                        cd.Cu_Password  = myDS.Tables["Customer"].Rows[i]["Cu_Password"].ToString();

                        i++;
                        CustomerList.Add(cd);
                    }
                    errormsg = "";
                    return(CustomerList);
                }
                else
                {
                    errormsg = "no products, something went wrong";
                    return(null);
                }
            }
            catch (Exception e)
            {
                errormsg = e.Message;
                return(null);
            }
            finally
            {
                dbConnection.Close();
            }
        }