ASP.NET C# – SQL Server – Insert Data to DB from a web page

hello on this video I’m going to show you how to write information to a database using and C#. Before
we even get the code, let’s take a look on the database. So here I have a database, and I already created the user but let’s take a look on the table that we’re gonna be inserting data
into. So, I am going to script the table and select to new query editor and I want to execute this query. So, I have some data over there three rows of data and now I’m going to create a
script again but this time as insert to new query editor. And I’m just going to test the queryexecute the query. It says successfully executed. Now I’m gonna run this select again just make sure we have one extra row here and sure enough I
do so that tells me that this query here is good. So, I’m going to copy this query here, and I want to use this on my code so on Visual Studio, I’m going to create a projects and I’m gonna create the web form. Name it index on index right click at the start page and on the code behind let’s write the code to insert some values. I just inserted line #3 so this is my connection string the next steps to get eh SQL commands and this was a copy paste from the test I was doing but now I want to use this query, the insert one. I am going to change the value a little bit So I can confirm later that I am inserting something elseand then finallyso let’s run this program and
check if marry and mary street actually went to the database, and this is what I got, some blank page which is fine because we don’t have anything on the page however, on the database, let’s run to select query again and sure enough Mary and Mary street is there so let’s talk about what I have here it’s very simple step one is to create a connection string, then a commands and then execute the command, and make sure you don’t forget to close your connection this connection string is very unsafe because the password is here and the ID is here as well so, you should encrypt this, or even better you should use trusted connection the SQL command is the same think. It’s very unsafe you should not use these. You should user parameters instead or even better you should be using stored procedure but the reason it did this is
I want to do it very simple for learning purposes only so anyway don’t forget this is not safe
you would never do this on a read production environment. So let’s change the value just one more time. I am going to putrun again, I see a blank page which is fine I did not put anything on the page. Go back to the database, execute
the query and the new row is right there so thanks for watching this video, this is very
easy and you can do it

One Reply to “ASP.NET C# – SQL Server – Insert Data to DB from a web page”

Leave a Reply

Your email address will not be published. Required fields are marked *