public List <Beten> GetBetenWithDataSet(out string errormsg) { SqlConnection dbConnection = new SqlConnection(); dbConnection.ConnectionString = @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=FiskDB_Labb3;Integrated Security=True"; String sqlstring = "SELECT * FROM Beten"; SqlCommand dbCommand = new SqlCommand(sqlstring, dbConnection); SqlDataAdapter myAdapter = new SqlDataAdapter(dbCommand); DataSet myDS = new DataSet(); List <Beten> BetesLista = new List <Beten>(); try { dbConnection.Open(); myAdapter.Fill(myDS, "myFisk"); int count = 0; int i = 0; count = myDS.Tables["myFisk"].Rows.Count; if (count > 0) { while (i < count) { Beten pd = new Beten(); // pd.ID = Convert.ToInt32(myDS.Tables["myFisk"].Rows[i]["FiskID"]); pd.ID = Convert.ToInt32(myDS.Tables["myFisk"].Rows[i]["Betenr"]); pd.Typ = myDS.Tables["myFisk"].Rows[i]["Typ"].ToString(); pd.Color = myDS.Tables["myFisk"].Rows[i]["Color"].ToString(); i++; BetesLista.Add(pd); } errormsg = ""; return(BetesLista); } else { errormsg = "Det hämtas ingen FISK!"; return(null); } } catch (Exception e) { errormsg = e.Message; return(null); } finally { dbConnection.Close(); } }
public int UpdateBeten(Beten p1, int Bete_id, out string errormsg) { SqlConnection dbConnection = new SqlConnection(); dbConnection.ConnectionString = @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=FiskDB_Labb3;Integrated Security=True"; string sqlstring = "UPDATE Beten SET Typ = @typ, Color = @color WHERE Betenr = @id"; SqlCommand dbCommand = new SqlCommand(sqlstring, dbConnection); dbCommand.Parameters.Add("typ", SqlDbType.NVarChar, 50).Value = p1.Typ; dbCommand.Parameters.Add("color", SqlDbType.NVarChar, 50).Value = p1.Color; dbCommand.Parameters.Add("id", SqlDbType.Int).Value = Bete_id; try { dbConnection.Open(); int i = 0; i = dbCommand.ExecuteNonQuery(); if (i == 1) { errormsg = ""; } else { errormsg = "Gick ej att uppdatera Bete"; } return(i); } catch (Exception e) { errormsg = e.Message; return(0); } finally { dbConnection.Close(); } }
public int InsertBete(Beten b1, out string errormsg) { SqlConnection dbConnection = new SqlConnection(); dbConnection.ConnectionString = @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=FiskDB_Labb3;Integrated Security=True"; string sqlstring = "INSERT INTO Beten (Typ, Color) VALUES (@typ, @color)"; SqlCommand dbCommand = new SqlCommand(sqlstring, dbConnection); dbCommand.Parameters.Add("typ", SqlDbType.NVarChar, 50).Value = b1.Typ; dbCommand.Parameters.Add("color", SqlDbType.NVarChar, 50).Value = b1.Color; try { dbConnection.Open(); int i = 0; i = dbCommand.ExecuteNonQuery(); if (i == 1) { errormsg = ""; } else { errormsg = "Det skapas ej ett bete i databasen"; } return(i); } catch (Exception e) { errormsg = e.Message; return(0); } finally { dbConnection.Close(); } }