Update query with one unique value other one with varying values - c#

Want a query possibly a single query for the below conditions
To be updated value of column 3 and 4 is same for all 4 R2IGTNo column
Sometimes the blend will be up to 3 only, 4th row may not come
In C# application user will update for the first row (blend 1), other values have to be updated automatically since the values are the same.
I need a query, expert please help with this... Thanks

User picks row 1, this means your C# can know "Lot21009BB1C3" (putting text instead of image would have let me copy it, btw) but you write like the c# app doesn't know any other row..
Which means you can run a query:
UPDATE t SET HtrOnBy = #HtrOnBy, ChtrOnRemark = #ChtrOnRemark
WHERE R2IGTNo LIKE #R2IGTNo
And your c# shall put the following values:
#HtrOnBy = "Whatever it knows the first row HtrOnBy is"
#ChtrOnRemark = "Whatever it knows the first row ChtrOnRemark is"
#R2IGTNo = firstRowR2IGTNo.Remove(9) + "%"
We take the first 9 characters of R2IGTNo and add a percent. SQL will update all the rows that start with those, doesn't matter if it's 3 or 4 etc
If the number of chars varies, then you will have to get more smart about how you do your substring, such as looking for the first character after the numbers (eg regex)

Related

Multi column index search Microsoft.Isam.Esent

I am facing the following issue: I have an composite index on a database index1 {binaryColumn1, binaryColumn2}. I am using the following to set the index to use:
Api.JetSetCurrentIndex(_session, _table, index1);
to create the key:
Api.MakeKey(_session, _table, binaryValue, MakeKeyGrbit.NewKey);
and than try to perform the search with:
Api.TrySeek(_session, _table, SeekGrbit.SeekEQ);
This works and seek returns true correctly if index1 is only for 1 column. If I have multiple columns and try to search the value for a single column (ex. for binaryColumn1 = {0x01, 0x23}) it always returns false.
How can I search for this one value? (ps. I cannot change the index nor create new ones.) Is this possible?
Thank you
What you did would only work for {0x01, 0x00}. You can't do it with a single call, because the value of the second column will be mixing up the SeekEQ grbit.
You could do a SeekGE, but then you'll need to retrieve the column to make sure the value is actually correct (and not something like {0x22, 0x23}). You'll have to do something like:
SetCurrentIndex()
MakeKey( ..., binaryValue1, MakeKeyGrbit.NewKey | MakeKeyGrbit.FullColumnStartLimit); // Appends the rest of the search buffer with 0x00's.
Seek(GE);
MakeKey(... binaryValue1, MakeKeyGrbit.NewKey | MakeKeyGrbit.FullColumnEndLimit); // Appends the rest of the search buffer with 0xff's.
Api.JetSetIndexRange();
Then you can use Api.TryMoveNext() to iterate through all rows that have that column equal to binaryValue1.
Also look at the test code included with the project -- the HowDoI.cs file has lots of good examples.
Sorry for the pseudo-code; I don't have the source code handy at the moment.
-martin

Simple SUM Query needed for C#

I am a beginner programmer wanna ask about simple SUM query for C#. here is the case:
I have a table called "revenue", and that table consist of 5 columns. they are Bulan, Target, Realisasi, Target_YtD, and Realisasi_YtD. for column Bulan, I manually inserted 12 data. they are January, Februari, March, and so on...
For column Target and Realisasi also I inserted data manually with INT data type.
Now, I wanna add up the January's Target + February's Target + March's Target, and then the value of that calculation is gonna fill March's Target_YtD.
Can somebody tell me the query of that? I hope anyone can help me this time, I really appreciate that. Thanks
Do you mean something like this?
UPDATE
SET Target_YtD =
(SELECT SUM(Target) AS Total FROM revenue
WHERE Bulan IN ('JAnuary', 'February', 'March')
)
WHERE Bulan = 'March'
It depends on if you plan to calculate the colum at the time of input and save it to the database. Usually calculated columns are not in and of themselves columns in databases, but if that is what you are doing you can just use the c# concatenation function to populate that variable before doing your insert statement. If you're only populating the sum upon output to a c# app, same thing applies, or you could do a sum in your sql code. A few ways to get there, just depends what you're trying to do.

C# Fill Data Grid View after using input list

I really need your expert help :). Expanding on what I have learnt from querying data sets using adapters and filling a Grid View. I need some help on the following task.
I have a SQL Server Database which I am querying using C#. I already have solid working solutions of by a date range, a specific value. However, the business user would like to search by a list of values they provide as input into a form.
Similar to the below:
adapter.SelectCommand.Parameters.AddWithValue("#mindate", textBox1.Text);
The input will be taken from a text box or similar form based element. There should be no defined limit to the number of values e.g. I don't want to prevent the user from inputting 100 values for example.
By way of example.
User input: doc1.num1.value;doc2.num1.value;doc3.num1.value etc
Note: The document number field may contain a full stop. However, each value will be terminated by a ';'
In the above example, we would run the following query: select employee_id, docNumbers from tableName where docNumbers in (inputlist)
And the output would be:
Record 1: 1, doc1.num1.value
Record 2: 2, doc2.num1.value
Record 3: 3, doc3.num1.value
Thanks in advance guys and gals.
I guess what you are looking for is using an IN clause when doing your query.

Creating a monthly report and compare rows based on the sum

This is my first time using c# and sql server to code a simple monthly reporting application which will print the report to paper.
Everything just fine but there is a complicated problem for me when comparing two value.
Suppose there are 14 columns(originally 37 columns) and 12 rows for each month. The last column i am using for month name.
Columns are:
Name, col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,monthName.
Logically there will be 12 reports in a year.
col2+col5+col6
and
col7+col10+col11 sum of current month's
must be same as last month col4 and col9-12 rows
This is my main problem!
For example if monthName has January(as past month) and I am filling the current month February . There is a pushButton called "Verify".
If I click on verify then the February's 3 columns sum will be compared to January's 1 column sum as above.
I have tried some sql query like select * from table1 where sum(col4)=sum(col2+col5+col6) and sum(col9)=sum(col7+col10+col11)
But my logic and knowledge is completely flawed since it did not work as expected.
Secondly, I thought appending data from sql query results to List<> and comparing the col4/col9 would be an idea but again flawed as there is no gurantee that Name column's value would not be shuffled which will lead to compare to wrong data, also I am not confident at it.
I need help from c# and sql database expert to get an idea to do this as I am not sure what is my options!
Update: I am doing some experiment in visual studio. All codes are just ugly but i will upload it on dropbox if someone care to try it.
I think your best solution here is to keep the business logic and the database query separate. If the user clicks the "verify" button on the "February" row, you just submit a database query along the lines of SELECT * FROM table1 WHERE monthName = 'january' although:
Probably you should actually use a date field to identify your months).
How you actually retrieve the data will depend on what type of data access you are using (ADO.NET, Entity Framework, etc.)
Once you get back that result, you just use straightforward C# logic to do the verification, along the lines of:
int priorMonthSum1 = priorMonthResultset.col4;
int priorMonthSum2 = priorMonthResultset.col9 + priorMonthResultset.col10 + priorMonthResultset.col11 + priorMonthResultset.col12;
int currentMonthSum1 = currentMonthResultset.col2 + currentMonthResultset.col5 + currentMonthResultset.col6;
int currentMonthSum2 = currentMonthResultset.col7 + currentMonthResultset.col10 + currentMonthResultset.col1;
if(priorMonthSum1 != currentMonthSum1 || priorMonthSum2 != currentMonthSum2)
{
// handle mismatch (highlight red?)
}
else
{
// handle match (highlight green?)
}

C# datatable take rows one at a time

I have a datatable containing certain columns. I am able to display them in a repeater as a whole. However, what I want to do is to display the row, one by one. I want to create like a go next and show previous button. I did something like the following:
myDataTable.Rows.Cast<DataRow>().Take(1).CopyToDataTable();
This is giving me the first row. Now how can I use this concept to get the next row, (row 2), then row 3..... till row n. The rows being returned are different for different cases. It is an web application.
To get a different row, you just need to Skip some:
myDataTable.Rows.Cast<DataRow>().Skip(n).Take(1).CopyToDataTable();
where n is how many rows you want to skip. So, for the second record n would be 1.
I greatly disagree with the use of CopyDataDataTable(), but it would require a lot of knowledge of your code base to provide a better approach.
I would select it from the database instead, however, use Skip(n).Take(1):
var row3 = myDataTable.AsEnumerable().Skip(2).Take(1).CopyToDataTable();
Introduce the use of .Skip():
myDataTable.Rows.Cast<DataRow>().Skip(0).Take(1).CopyToDataTable();
Now you can simply track which record the user is currently viewing, and update the value sent to the .Skip() method. For example, if the user has pressed "next" 5 times, you've incremented the value 5 times:
myDataTable.Rows.Cast<DataRow>().Skip(5).Take(1).CopyToDataTable();
Keep a counter and use Skip(n-1).Take(1) for nth record.

Categories

Resources