Пример #1
0
        private void InserirTeste_Click(object sender, EventArgs e)
        {
            IDbConnection conexao;
            StringConexao stringConexao = new StringConexao();

            //string stringConexao = @"Data Source = (localdb)\MSSQLLocalDB; Database = Teste; User ID = sa; Password = senha; TrustServerCertificate = True; Trusted_Connection = False; Connection Timeout = 300; Integrated Security = False; Persist Security Info = False; Encrypt = false; MultipleActiveResultSets = True;";
            conexao = new SqlConnection(stringConexao.getStringConexao());
            conexao.Open();
            var listaTeste = new List <Teste>();

            try
            {
                IDbCommand insertCmd = conexao.CreateCommand();
                insertCmd.CommandText = "insert into Teste values('Teste - '+ CONVERT(varchar, getdate(), 121))";
                IDataReader resultado = insertCmd.ExecuteReader();
            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                conexao.Close();
            }
        }
Пример #2
0
        private void TestarConexao_Click(object sender, EventArgs e)
        {
            IDbConnection conexao;
            StringConexao stringConexao = new StringConexao();

            //string stringConexao = @"Data Source = (localdb)\MSSQLLocalDB; Database = Teste; User ID = sa; Password = senha; TrustServerCertificate = True; Trusted_Connection = False; Connection Timeout = 300; Integrated Security = False; Persist Security Info = False; Encrypt = false; MultipleActiveResultSets = True;";
            conexao = new SqlConnection(stringConexao.getStringConexao());
            conexao.Open();
            var listaTeste = new List <Teste>();

            try
            {
                IDbCommand selectCmd = conexao.CreateCommand();
                selectCmd.CommandText = "Select * from Teste";

                IDataReader resultado = selectCmd.ExecuteReader();
                while (resultado.Read())
                {
                    var conexaoTeste = new Teste()
                    {
                        Texto = Convert.ToString(resultado["Texto"]),
                    };
                    listaTeste.Add(conexaoTeste);
                }

                ExibirMensagem.Controls.Clear();
                int          espaco         = 25;
                int          posicao        = 20;
                List <Label> ListaMensagens = new List <Label>();
                foreach (Teste obj in listaTeste)
                {
                    Label label = new Label();
                    label.Text = obj.Texto;
                    label.Top  = posicao;
                    this.Controls.Add(label);
                    posicao += espaco;
                    ExibirMensagem.Controls.Add(label);
                }
            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                conexao.Close();
            }
        }