Create lines in the Axapta journal - c#

I do WCF-srvice on C# which must get data from Axapta's tables. I can create new AxaptaRecord, and create new record in table.
using (axRecord = axapta.CreateAxaptaRecord(tableName)) //this create new record
{
axRecord.set_Field("name", "firstname");
-//-
axRecord.Insert();
}
This code show, how I get data from this tables.
using (axRecord = axapta.CreateAxaptaRecord(tableName))
{
axRecord.ExecuteStmt("select * from %1");
while (axRecord.Found)
{
ToroEquipment temp=new ToroEquipment();
temp.num_journal=axRecord.get_Field("text").ToString();
lToroEq.Add(temp);
axRecord.Next();
}
}
Also, all tables can have linked table, which contain lines with other data.
I can read data from this lines. I use the above code with the modified query (I add condition with "where").
So, how can I insert data from this lines in C#?
Can you give me some examples of code for this?

It seems you are using the .NET Business Connector, not AIF.
So you can find an example to insert data using the .NET BC here: https://msdn.microsoft.com/en-us/library/aa868997.aspx
If you want to use WCF, you can activate the LedgerServices service group and add the LedgerGeneralJournalService document datasource and use it directly as WCF service.

Related

Add new column in datagridview1 with a completely new sort of SQL Server database data

I create a small program using a SQL Server database and C#.
I connected to the database and sort all data and display it in a datagridview1 without any problems.
My code:
enter image description here
i otrzymuje wynik:
enter image description here
Now my question: how can I add a new column in datagridview1 with a completely new sort of SQL Server database data?
obviously a sort order is part of a SQL expression and you do not mean that, as you would surely already know (?) instead change how it sorts, which is the collation of the column. This is thankfully fully supported:
https://learn.microsoft.com/en-us/sql/relational-databases/collations/set-or-change-the-column-collation?view=sql-server-ver15
There are a few ways how to do it:
Make a complex SQL Query, that will group the data as it's needed, using Joins and other ways of data union.
The easiest and the worst way how to do it - is to extend your data table manually:
dataTable.Columns.Add(new DataColumn("newColumn", typeof(string)));
This will automatically refresh DataGridView on the form. And then you will need to add new data to the new DataTable column manually.
I prefer to work with dataGridView based on models. You need to create a class - that will represent your model. Like class Person with properties: Name, Age, etc. And then set this list as data source.
List<Person> listOfPersons = new();
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = new BindingSource() { DataSource = listOfPersons};
The main problem - you will need to parse database data to the model format.
Actually, this is where ORM (Object-Relational Mapping) is helpful. There are a few ready solutions, like Dapper. It will simplify your life)

Fill existing SQLite database with data from JSON (or other easily-readable text format) in C#

My database is divided into two parts: language-independent part and localizable part (containing Unicode text strings). For our translation staff it's much more easier to work not with some DB viewing tool but with some text format like JSON (via some tool of course). So I'm looking for a best way to load JSON data into my SQLite database. For now I use the foolowing approach (assume that I already have empty SQLite database):
I deserealise JSON data into C# classes (via Newtonsoft.JSON.dll).
For each of them I manually create INSERT command with paremeters (via System.Data.SQLite.dll) and then fill that parameters with class fields' values
I execute that command.
Is that correct (easiest, reliable) aproach to fill SQLite database with JSON data? Maybe I've missed some useful API?
Any other easily-readable text format (with Unicode support!) which is better to work with in term of C# and SQLite is welcomed.
Try Sqlite.NET, a basic Sqlite client and ORM: https://github.com/praeclarum/sqlite-net
From their wiki:
Once you have defined your entity, you can automatically generate
tables in your database by calling CreateTable:
var db = new SQLiteConnection("foofoo");
db.CreateTable<Stock>();
db.CreateTable<Valuation>();
You can insert rows in the database using Insert. If the table
contains an auto-incremented primary key, then the value for that key
will be available to you after the insert:
public static void AddStock(SQLiteConnection db, string symbol) {
var s = db.Insert(new Stock() {
Symbol = symbol
});
Console.WriteLine("{0} == {1}", s.Symbol, s.Id);
}
Similar methods exist for Update and Delete.
You would get something like this:
AddStock(string jsonString){
var stock = JsonConvert.DeserializeObject<Stock>(jsonString);
db.Insert(stock);
}

Getting data out of tableadapters

I just can't understand this thing: I have an access DB I want to query using c#. I read there are this useful tableadapters that make most of the job for you.
Here is what I have done:
Added new data source to the project though the wizard
Added a table adapter via the toolbox. The table adapter has a query named GetData that I want to use to retrieve results.
I add these lines to the code to make my textbox show my first result
Database_CeciDataSetTableAdapters.BaseTableAdapter tableadapter = new Database_CeciDataSetTableAdapters.BaseTableAdapter();
textBox1.Text = tableadapter.GetData()[0].ToString();
Here is what I get in the textbox:
WordAddIn5.Database_CeciDataSet+BaseRow
Can anyone tell what am I doing wrong?

Reading from SQL Server - need to read from CSV

At the moment, I source my data from a SQL serve r(2008) database. The cyurrent method is to use a DataTable, which is then passed around and used.
if (parameters != null)
{
SqlDataAdapter _dataAdapter = new SqlDataAdapter(SqlQuery, CreateFORSConnection());
foreach (var param in parameters)
{
_dataAdapter.SelectCommand.Parameters.AddWithValue(param.Name, param.Value);
}
DataTable ExtractedData = new DataTable(TableName);
_dataAdapter.Fill(ExtractedData);
return ExtractedData;
}
return null;
But now, the user has said that we can also get data from txt files, which have the same structure as the tables in SQL Server. So, if I have a table called 'Customer', then I have a csv file with Customer. with the same column structure. The first line in the CSV is the column name, and matches my tables.
Would it be possible to read the txt file into a data table, and then run a SELECT on that data table somehow? Most of my queries are single table queries:
SELECT * FROM Table WHERE Code = 111
There is, however, ONE case where I do a join. That may be a bit more tricky, but I can make a plan. If I can get the txt files into data tables first, I can work with that.
Using the above code, can I not change the connection string to rather read from a CSV instead of SQL Server?
First, you'll need to read the CSV data into a DataTable. There are many CSV parsers out there, but since you prefer using ADO.NET, you can use the OleDB client. See the following article.
http://www.switchonthecode.com/tutorials/csharp-tutorial-using-the-built-in-oledb-csv-parser
Joining is a bit harder, since both sets of data live in different places. But what you can do is get two DataTables (one from each source), then use Linq to join them.
Inner join of DataTables in C#
You could read the text file into a List<string> (if there is just 1 column per file), and then use LINQ to query the list. For example:
var result = from entry in myList
where entry == "111"
select entry;
Of course, this example is kind of useless since all you get back is the same string you are searching for. But if there are multiple columns in the file, and they match the columns in your DataTable, why not read the file into the data table, and then use LINQ to query the table?
Here is a simple tutorial about how to use LINQ to query a DataTable:
http://blogs.msdn.com/b/adonet/archive/2007/01/26/querying-datasets-introduction-to-linq-to-dataset.aspx

C# Read rows from a table and store the values in a string[]

I am using C# and ASP.NEt to create a web application where I need to insert/view data. I created my own class that stores values for a "Ticket" or a support ticket. My question is, how can I grab the values from the SQL table with the tickets and store them in the Ticket class? I don't need the specifics, just what SQL class I should use and how to store them.
Thanks!
Several options: ADO.NET to query the data using the SqlConnection and SqlDataAdapter, filling the results for a DataTable, and copying the data row data to your Ticket class.
Or even better, use LINQ to SQL or ADO.NET Entity Framework. Plenty of examples of all of this online.
This tutorial should get you started:
http://www.csharp-station.com/Tutorials/ADODotNet/Lesson02.aspx
In the section that reads:
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
You will want replace the Console.WriteLine() with code to to create new instances of Ticket and add them to a ticket list.
You should be able to use a SqlDataReader to get your data, get your column data and place it into your string array.
Have you considered using an ORM? Entity Framework is one the most used.
See this tutorial for more information:
http://www.codeproject.com/Articles/363040/An-Introduction-to-Entity-Framework-for-Absolute-B

Categories

Resources