• We just launched and are currently in beta. Join us as we build and grow the community.

How to Save Data Using C# and SQL Server Database

AccDispenser

API Endpoint Tester
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
173
Likes
177
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, I will teach you how to save data in the database using C#.net and SQL Server 2005 Express Edition. With this, you can access and save the data in the Server database with ease. This is the basic implementation for a newbie in programming who plans to build a system in the future.
break
Let's get started:

1. Create a database and name it "userdb".
2. Do this following query for creating a table in the database that you have created.
  1. /****** Object: Table [dbo].[tbluser] Script Date: 06/23/2016 08:36:33 ******/
  2. SET

    ANSI_NULLS ON
  3. GO
  4. SET

    QUOTED_IDENTIFIER ON
  5. GO
  6. CREATE

    TABLE

    [

    dbo]

    .

    [

    tbluser]

    (
  7. [

    ID]

    [

    INT

    ]

    IDENTITY

    (

    1

    ,

    1

    )

    NOT

    NULL

    ,
  8. [

    Name]

    [

    nvarchar]

    (

    50

    )

    NULL

    ,
  9. [

    UNAME]

    [

    nvarchar]

    (

    50

    )

    NULL

    ,
  10. [

    PASS]

    [

    nvarchar]

    (

    MAX

    )

    NULL

    ,
  11. [

    UTYPE]

    [

    NCHAR

    ]

    (

    20

    )

    NULL

    ,
  12. CONSTRAINT

    [

    PK_tbluser]

    PRIMARY

    KEY

    CLUSTERED
  13. (
  14. [

    ID]

    ASC
  15. )

    WITH

    (

    PAD_INDEX =

    OFF,

    STATISTICS_NORECOMPUTE =

    OFF,

    IGNORE_DUP_KEY =

    OFF,

    ALLOW_ROW_LOCKS =

    ON

    ,

    ALLOW_PAGE_LOCKS =

    ON

    )

    ON

    [

    PRIMARY

    ]
  16. )

    ON

    [

    PRIMARY

    ]

3. Open Microsoft Visual Studio 2008, create a new windows form application and do the form just like this.

savedatasqlcsharpfig.1.png


4. Go to the Solution Explorer, click the “View Code” to fire the code editor.

savedatasqlcsharpfig.2.png


5. Declare and initialize the classes that are needed.

  1. //initialize all classes
  2. SqlConnection string_con =

    new

    SqlConnection(

    )

    ;
  3. SqlCommand sql_command =

    new

    SqlCommand(

    )

    ;

Note: Add "using System.Data.SqlClient;" above the namespace to access sql server library.

6. Establish a connection between SQL Server and C#.net on the first load of the form.
  1. private

    void

    Form1_Load(

    object

    sender, EventArgs e)
  2. {
  3. //bridge between sql server to c#
  4. string_con.

    ConnectionString

    =

    "Data Source=.\\

    SQLEXPRESS;Database=userdb;trusted_connection=true;"

    ;
  5. }

7. Go back to the Design Views, Double click the "Save" button and create a method for saving the data in the SQL database.

  1. private

    void

    btnSave_Click(

    object

    sender, EventArgs e)
  2. {
  3. try
  4. {
  5. //opening connection
  6. string_con .

    Open

    (

    )

    ;
  7. //create an insert query;
  8. string

    sql=

    "INSERT INTO tbluser (NAME,UNAME,PASS,UTYPE) VALUES('"

    +

    txtName.

    Text

    +

    "','"

    +

    txtUsername.

    Text

    +

    "','"

    +

    txtPassword.

    Text

    +

    "','"

    +

    cboType.

    Text

    +

    "')"

    ;
  9. //initialize a new instance of sqlcommand
  10. sql_command =

    new

    SqlCommand(

    )

    ;
  11. //set a connection used by this instance of sqlcommand
  12. sql_command .

    Connection

    =

    string_con ;
  13. //set the sql statement to execute at the data source
  14. sql_command .

    CommandText

    =

    sql;
  15. //execute the data.
  16. int

    result =

    sql_command.

    ExecuteNonQuery

    (

    )

    ;
  17. //validate the result of the executed query.
  18. if

    (

    result >

    0

    )
  19. {
  20. MessageBox.

    Show

    (

    "Data has been saved in the SQL database"

    )

    ;
  21. }
  22. else
  23. {
  24. MessageBox.

    Show

    (

    "SQL QUERY ERROR"

    )

    ;
  25. }
  26. //closing connection
  27. string_con .

    Close

    (

    )

    ;

  28. }
  29. catch

    (

    Exception ex)

    //catch exeption
  30. {
  31. //displaying errors message.
  32. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  33. }
  34. }

Output:

savedatasqlcsharpfig.3_0.png


For all students who need a programmer for your thesis system or anyone who needs a source code in any programming languages. You can contact me @ :

Email – [email protected]
Mobile No. – 09305235027 – TNT

 

452,292

323,526

323,535

Top