Skip to main content
removeu 122 caracteres do conteúdo
Fonte Link
Amadeu Antunes
  • 3,4mil
  • 3
  • 31
  • 66
 protected void Upload_Click(object sender, EventArgs e)
        {
            string excelPath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
            


            string filepath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.FileName);
            string filename = Path.GetFileName(filepath);
            FileUpload1.SaveAs(excelPath);
            string ext = Path.GetExtension(filename);
           
                String strConnection = @"Data Source=PEDRO-PC\SQLEXPRESS;Initial Catalog=costumizado;Persist Security Info=True;User ID=sa;Password=1234";
            string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0 Xml;HRD=YES;IMEX=1;\"";
            
            OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
 
            OleDbCommand cmd = new OleDbCommand("Select * from [rptListaMovs_4$]", excelConnection);

 
            excelConnection.Open();



 
                cmd.ExecuteNonQuery();

 
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("Select * from [rptListaMovs_4$]", strConnection);
 
                OleDbDataReader dReader;
                dReader = cmd.ExecuteReader(); 

            using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
            {
                sqlBulk.DestinationTableName = "Dados";
                sqlBulk.ColumnMappings.Add("Data Mov", "Data Mov.");
                sqlBulk.ColumnMappings.Add("Data Valor", "Data Valor");
                sqlBulk.ColumnMappings.Add("Descrição do Movimento" , "Descrição do Movimento");
                sqlBulk.ColumnMappings.Add("Valor em EUR", "Valor em EUR");
 
                
                sqlBulk.WriteToServer(dReader);
             }
                excelConnection.Close();
            
            
          
        }
 protected void Upload_Click(object sender, EventArgs e)
        {
            string excelPath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
            


            string filepath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.FileName);
            string filename = Path.GetFileName(filepath);
            FileUpload1.SaveAs(excelPath);
            string ext = Path.GetExtension(filename);
           
                String strConnection = @"Data Source=PEDRO-PC\SQLEXPRESS;Initial Catalog=costumizado;Persist Security Info=True;User ID=sa;Password=1234";
            string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0 Xml;HRD=YES;IMEX=1;\"";
            
            OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
 
            OleDbCommand cmd = new OleDbCommand("Select * from [rptListaMovs_4$]", excelConnection);

 
            excelConnection.Open();



 
                cmd.ExecuteNonQuery();

 
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("Select * from [rptListaMovs_4$]", strConnection);
 
                OleDbDataReader dReader;
                dReader = cmd.ExecuteReader();
            using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
            {
                sqlBulk.DestinationTableName = "Dados";
                sqlBulk.ColumnMappings.Add("Data Mov", "Data Mov.");
                sqlBulk.ColumnMappings.Add("Data Valor", "Data Valor");
                sqlBulk.ColumnMappings.Add("Descrição do Movimento" , "Descrição do Movimento");
                sqlBulk.ColumnMappings.Add("Valor em EUR", "Valor em EUR");
 
                
                sqlBulk.WriteToServer(dReader);
            }
                excelConnection.Close();
            
            
          
        }
 protected void Upload_Click(object sender, EventArgs e)
 {
            string excelPath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
            string filepath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.FileName);
            string filename = Path.GetFileName(filepath);
            FileUpload1.SaveAs(excelPath);
            string ext = Path.GetExtension(filename);
            String strConnection = @"Data Source=PEDRO-PC\SQLEXPRESS;Initial Catalog=costumizado;Persist Security Info=True;User ID=sa;Password=1234";
            string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0 Xml;HRD=YES;IMEX=1;\"";
            OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
            OleDbCommand cmd = new OleDbCommand("Select * from [rptListaMovs_4$]", excelConnection);
            excelConnection.Open();
            cmd.ExecuteNonQuery();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("Select * from [rptListaMovs_4$]", strConnection);
            OleDbDataReader dReader;
            dReader = cmd.ExecuteReader(); 

            using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
            {
                sqlBulk.DestinationTableName = "Dados";
                sqlBulk.ColumnMappings.Add("Data Mov", "Data Mov.");
                sqlBulk.ColumnMappings.Add("Data Valor", "Data Valor");
                sqlBulk.ColumnMappings.Add("Descrição do Movimento" , "Descrição do Movimento");
                sqlBulk.ColumnMappings.Add("Valor em EUR", "Valor em EUR");
                sqlBulk.WriteToServer(dReader);
             }
             excelConnection.Close();
}
adicionou 2021 caracteres ao conteúdo
Fonte Link

Codigo asp.net:

 protected void Upload_Click(object sender, EventArgs e)
        {
            string excelPath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
            


            string filepath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.FileName);
            string filename = Path.GetFileName(filepath);
            FileUpload1.SaveAs(excelPath);
            string ext = Path.GetExtension(filename);
           
                String strConnection = @"Data Source=PEDRO-PC\SQLEXPRESS;Initial Catalog=costumizado;Persist Security Info=True;User ID=sa;Password=1234";
            string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0 Xml;HRD=YES;IMEX=1;\"";
            
            OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);

            OleDbCommand cmd = new OleDbCommand("Select * from [rptListaMovs_4$]", excelConnection);


            excelConnection.Open();




                cmd.ExecuteNonQuery();


                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("Select * from [rptListaMovs_4$]", strConnection);

                OleDbDataReader dReader;
                dReader = cmd.ExecuteReader();
            using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
            {
                sqlBulk.DestinationTableName = "Dados";
                sqlBulk.ColumnMappings.Add("Data Mov", "Data Mov.");
                sqlBulk.ColumnMappings.Add("Data Valor", "Data Valor");
                sqlBulk.ColumnMappings.Add("Descrição do Movimento" , "Descrição do Movimento");
                sqlBulk.ColumnMappings.Add("Valor em EUR", "Valor em EUR");

                
                sqlBulk.WriteToServer(dReader);
            }
                excelConnection.Close();
            
            
          
        }

Codigo asp.net:

 protected void Upload_Click(object sender, EventArgs e)
        {
            string excelPath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
            


            string filepath = Server.MapPath("~/Nova pasta/") + Path.GetFileName(FileUpload1.FileName);
            string filename = Path.GetFileName(filepath);
            FileUpload1.SaveAs(excelPath);
            string ext = Path.GetExtension(filename);
           
                String strConnection = @"Data Source=PEDRO-PC\SQLEXPRESS;Initial Catalog=costumizado;Persist Security Info=True;User ID=sa;Password=1234";
            string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0 Xml;HRD=YES;IMEX=1;\"";
            
            OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);

            OleDbCommand cmd = new OleDbCommand("Select * from [rptListaMovs_4$]", excelConnection);


            excelConnection.Open();




                cmd.ExecuteNonQuery();


                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("Select * from [rptListaMovs_4$]", strConnection);

                OleDbDataReader dReader;
                dReader = cmd.ExecuteReader();
            using (SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection))
            {
                sqlBulk.DestinationTableName = "Dados";
                sqlBulk.ColumnMappings.Add("Data Mov", "Data Mov.");
                sqlBulk.ColumnMappings.Add("Data Valor", "Data Valor");
                sqlBulk.ColumnMappings.Add("Descrição do Movimento" , "Descrição do Movimento");
                sqlBulk.ColumnMappings.Add("Valor em EUR", "Valor em EUR");

                
                sqlBulk.WriteToServer(dReader);
            }
                excelConnection.Close();
            
            
          
        }
Fonte Link

Como posso Criar uma coluna no SQL SERVER onde, dependendo do valor, apareça débito ou crédito?

Boas Amigos, Fiz um programa onde importo dados de um ficheiro excel na base de dados do sql, essa base de dados é sobre valores, e etc... E queria Saber se tem como criar uma coluna onde depois de ser importado o sql, automaticamente, escreva na coluna ao lado se é débito ou crédito Exmp:

Data Mov    Valor em EUR   Débito/Crédito
23-10-2019    -5000.00     Débito
3-1-2016      1234.50      Crédito

Tenho que programar isso no asp.net ou dá para fazer isso no sql? Toda vez que importo naquela tabela me dá o debito e o credito