1
Good friends, I made a program where I import data from an excel file in the sql database, this database is about values, and etc... And wanted to know if you have how to create a column where after being imported sql, automatically write in the next column if it is debit or credit Exmp:
Data Mov Valor em EUR Débito/Crédito
23-10-2019 -5000.00 Débito
3-1-2016 1234.50 Crédito
Do I have to program this in Asp.net or can I do it in sql? Every time I care in that table gives me the debit and credit
Code 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();
}
To be possible is more there are n ways to address this subject, seems to me a little broad this question.
– Amadeu Antunes
@Amadeuantunes As for example?
– Pedro Pereira
https://social.msdn.microsoft.com/Forums/Lync/en-US/cac57b8b-4175-4e59-ab19-efaf4dddaf18/c-serialize-deserialize-tofrom-excel-sheet?forum=vsto @Pedro Pereira
– Amadeu Antunes
@Amadeuantunes So I have to program on Asp.net right? The problem is I’m new at this I don’t know where to start
– Pedro Pereira
From what I understand, you need to import from excel data using Asp.net and then you want to store that data in the database, so you’re going to have to get that data out of the file and put it in a database table. @Pedro Pereira
– Amadeu Antunes
https://docs.microsoft.com/pt-br/sql/relational-databases/tables/specify-computed-columns-in-a-table?view=sql-server-2017 virtual column as I understand it, use a CASE
– Motta
@Amadeuantunes I’ve done it... I now wanted to make that after importing the data into the database, in the database appear a table to say if the value that was taken out in the column "Value in EUR" is debit or credit
– Pedro Pereira
so you already have the data imported in the c# side and you want to write in the database? @Pedro Pereira
– Amadeu Antunes
@Amadeuantunes I want after this, already in the database, SQL alone make a list of the values that are debited(negative) and credits (positive)
– Pedro Pereira
Sorry @Pedropereira still seems a bit vast, but you can do that on the c# side and project those values on the table. If the table is not created you create first. But if the table was to create dynamically you have to do it on the side of where you ask for the c# to create the table to sql and then put the data in this table
– Amadeu Antunes
@Amadeuantunes This is for a customer, it simply matters to the database and then a column is added to the database to tell if that value is a credit or debit... Can’t explain it better than that... I made an example and everything up there
– Pedro Pereira
@String value = value > 0? "Credit" "Debit"; string st = "update Data set Data_value = " + value Where costumized.table_name; https://stackoverflow.com/questions/15246182/sql-update-statement-in-c-sharp
– Amadeu Antunes