コード例 #1
0
        private void _Salvar(Models.Autores entidade)
        {
            string stringConexao = @"Data Source=DESKTOP-DL249A7\SQLSERVER14;Initial Catalog=Biblioteca;user Id=sa;Password=24052716";
            using (var conn = new SqlConnection(stringConexao))
            {
                var lista = CriarListaAutores();
                if (entidade.id > 0)
                    lista = _Excluir(entidade.id);
                else
                    entidade.id = lista.Count == 0
                        ? 1
                        : lista.OrderByDescending(item => item.id).FirstOrDefault().id + 1;
                string sql = "INSERT INTO Autores (id,Nome,Sobrenome,DataNascimento) VALUES (@id,@Nome,@Sobrenome,@DataNascimento)";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", entidade.id);
                cmd.Parameters.AddWithValue("@Nome", entidade.Nome);
                cmd.Parameters.AddWithValue("@Sobrenome", entidade.Sobrenome);
                cmd.Parameters.AddWithValue("@DataNascimento", entidade.DataNascimento);
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw e;
                }

            }

        }
コード例 #2
0
        private List<Models.Autores> CriarListaAutores()
        {
            //var lista = Session["Empregados"] as List<Models.Empregados>;
            string stringConexao = @"@"Data Source=den1.mssql1.gear.host;Initial Catalog=biblioteca4;user Id=biblioteca4;Password=Ga6g!w8m88_L";
            string sql = "Select id,Nome,Sobrenome,DataNascimento From Autores";
            List<Models.Autores> lista = new List<Models.Autores>();
            using (var conn = new SqlConnection(stringConexao))
            {
                var cmd = new SqlCommand(sql, conn);
                Models.Autores p = null;
                try
                {
                    conn.Open();
                    using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (reader.Read())
                        {
                            p = new Models.Autores();
                            p.id = (int)reader["id"];
                            p.Nome = reader["Nome"].ToString();
                            p.Sobrenome = reader["Sobrenome"].ToString();
                            p.DataNascimento = (DateTime)reader["DataNascimento"];
                            lista.Add(p);
                        }
                    }
                }

                catch (Exception e)
                {
                    throw e;
                }
            }
            if (lista == null)
                lista = new List<Models.Autores>();

            return lista
                .OrderBy(item => item.id)
                .ToList();
        }
コード例 #3
0
        public ActionResult Salvar(Models.Autores Autor)
        {
            _Salvar(Autor);

            return Json(new { Sucesso = true });
        }