Store and reterive data in database

.net , cpp.net , db Turkey
  • 12 years ago

     Dear All,

    Now am fresher to enter Dotnet.I want to simple coding for how to store data in database? and How to reterive  stored data in database to output form? For example i want to create employee table.The following fields are contain the table(empid,empname,empaddress,etc).I want to Store data in a database and also i want to reterive stored data to output form

  • 12 years ago

    Hi Arun

    you've left out several pieces of information. What dbms are you connecting to (Oracle,SQL Server,Access) so I'm not going to give the line by line breakdown for every one of them. Use the code I give to help you understand how the Active Data Object (ADO.Net) works.

    code is in VB.Net

    'I'm assuming you're using a SQL Server dbms

    'Case 1 --Retrieving a single value

    Using System.Data.SqlClient

    Dim sqlConn As New SqlClient.SqlConnection 'You need a connection

    Dim sqlComm As New SqlClient.SqlCommand 'You 're also going to need a command object

    Dim ValuetobeRetrieved As String

    sqlConn.ConnectionString = "" ' Whatever connection string you're using ie. Data Source=[MachineName];Initial Catalog=[dbName];Integrated Security=[True/False--True=your application has security rights,False=Username and Password has to be supplied for your application to connect]

    sqlComm.CommandText = "SELECT [ValueToBeRetrieved] FROM [db_Table] WHERE [Insert Filter criteria] "sqlComm.Connection = sqlConn ' Set the commands connection

    sqlConn.Open() 'Self explanatory

    ValuetobeRetrieved= sqlComm.ExecuteScalar()

    sqlConn.Close()

    NOTES: always ensure that your data typing in .Net matches the value type in SQL (String = String etc..)

    Also explore and discover the following

    Datareader,DataSets,DataGrid, DataGridView

    Unfortunately I've run out of time so I cannot continue with Case 2 retrieving and displaying multiple values (a full row in a table for example), but with research and a little curiosity you can discover that for yourself. Have fun!!

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If debugging is the process of removing software bugs, then programming must be the process of putting them in.” - Edsger Dijkstra