Question Store Procedure with SQL

Myat_Kaung

Member
Joined
Aug 22, 2022
Messages
6
Programming Experience
Beginner
Hello, I am very new to C# and Visual Studio Platform. And the problem I am facing now is that I take the data from SQl and based on that data I have to make a condition let's say if the data from SQL is greater than 5, then I will turn on the light. Could anyone help me how to create this kind of program? A simple program for that also works but with the stored procedure. Thank you so much.
 
If you can do a SQL query, determine if the value returned by that query is greater than 5, then changing the query to call a stored procedure is all you need to do. The rest of the C# code would remain the same. The only thing that changes slightly is what SQL statements you pass into the SqlCommand.

Right now, your original post makes it sound like you can't even do the SQL query. It's not clear why you are trying to do this so early in your C# learning journey, unless you oversold your program abilities to a client and now you are forced to go up the learning curve quickly.
 
yes, I know how the sql query works. But I don't know how to call stop procedure and where to put my logic whether it is after con.open() or con!=null)
 
I don't know how to call stop procedure
Probably because that's not a thing.
if the data from SQL is greater than 5
What does that actually mean? Are you saying that you want to do something if the result set of a query contains more than 5 rows? If that's the case then just do a COUNT first, instead of getting the data. If you're saying that you just want the first 5 rows then create a data reader and call Read five up to five times.

I imagine that English is not your first language but you need to provide a clearer explanation of what you're trying to achieve. Assume that too many words is better than not enough. Non-native English-speakers often try to be brief and end up leaving out loads of important detail. If you provide greater detail then we're more likely to be able to piece together a clear picture of what you need. For instance, don't just say "data from SQL". What data? What query? Be specific and explain step by step.
 
Probably because that's not a thing.

What does that actually mean? Are you saying that you want to do something if the result set of a query contains more than 5 rows? If that's the case then just do a COUNT first, instead of getting the data. If you're saying that you just want the first 5 rows then create a data reader and call Read five up to five times.

I imagine that English is not your first language but you need to provide a clearer explanation of what you're trying to achieve. Assume that too many words is better than not enough. Non-native English-speakers often try to be brief and end up leaving out loads of important detail. If you provide greater detail then we're more likely to be able to piece together a clear picture of what you need. For instance, don't just say "data from SQL". What data? What query? Be specific and explain step by step.
Thanks for the suggestion. So I want to get the value that is within the SQL table ( for example value of row 2 and col 2 from SQL table). I know that this will be done by SELECT sth. After I have got the value, inside the visual studio C#, I will have to write a program where if this value is greater than certain threshold, then I will trigger the light from the LED. For example, if value > 5 then trigger the light. However, I have some constraints. I have to use store procedure while taking the value from SQL database( which is I am not very sure). In addition, light triggering is another program. So I do not know how to link the light triggering source code with value from SQL program. Moreover, If I put if else condition to trigger the light, where should I put it inside the program.cs? This is the issue that I have been facing now. Pls let me know if I can provide more detailed explanation.
 
You're asking lots of unrelated questions here, which is obviously part of the problem: you don't have a clear idea of what the steps are that your application needs to perform. That should be done first, before thinking about writing any code. Retrieving data from a database is the same no matter what you want to do with the data, so if you have a question about querying the database then ask that, independent of anything to do with lights or anything else. If you have a question about how to do something based on a numeric value then ask that. Where the number came from is irrelevant to that. Where you put your code depends on where the task the code is performing fits into the rest of your application. If you have a clear idea of all the tasks your app needs to perform then that will tell you where the code for each one goes. Slow down, take a breath and take things step by step. There's no reason that you can't create a test app to do one specific thing and then, once you understand that, incorporate what you have learned into your main project. Trying to write code before you even know what it has to do is a recipe for disaster.
 
Back
Top Bottom