Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7543211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:22:03+00:00 2026-05-30T08:22:03+00:00

I need a sample C# (console application) code witch connects to an SQL Server

  • 0

I need a sample C# (console application) code witch connects to an SQL Server Express database
and inserts a few variables into a table “laptops”

  • SQL Server Express is @ localhost
  • user name is database
  • and password is testdatabase

What is the proper way to do that ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:22:04+00:00Added an answer on May 30, 2026 at 8:22 am

    Basic ADO.NET 101:

    • set up a connection
    • set up a command to do something
    • execute that command

    Step 1: setting up a connection

    You need to know the connection string to your database. Check out http://www.connectionstrings.com for a ton of examples.

    In your case, you say it’s a local SQL Server Express instance – but unfortunately, you didn’t mention what your database is called….. your connection string will be something like:

    server=(local)\SQLEXPRESS;database=YourDatabaseName;user id=database;pwd=testdatabase
    

    Step 2: setting up a command

    You can have various commands – to select data, to delete that, or to insert data. Whatever you do – I would recommend to always use parametrized queries to avoid SQL injection.

    So your code here would look something like:

    string connectionString = "server=(local)\SQLEXPRESS;database=YourDatabaseName;user id=database;pwd=testdatabase";
    
    string insertStmt = "INSERT INTO dbo.Laptops(Name, Model, ScreenSize) " + 
                        "VALUES(@Name, @Model, @Screensize)";
    
    using(SqlConnection conn = new SqlConnection(connectionString))
    using(SqlCommand cmd = new SqlCommand(insertStmt, conn))
    {
        // set up the command's parameters
        cmd.Parameters.Add("@Name", SqlDbType.VarChar, 100).Value = "ASUS SX30";
        cmd.Parameters.Add("@Model", SqlDbType.VarChar, 50).Value = "Ultralight";
        cmd.Parameters.Add("@Screensize", SqlDbType.Int).Value = 15;
    
        // open connection, execute command, close connection
        conn.Open();
        int result = cmd.ExecuteNonQuery();
        conn.Close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do you guys came across any sample application (i need source code of the
BeautifulSoup newbe... Need help Here is the code sample... from mechanize import Browser from
I am creating simple console application where I need to load RTF file to
I need to implement a TCP client application. The client and the server send
Can anyone help me to connect SQL Server 2008 to Visual C++ win32 console
Simple problem in Delphi. I've created a console application and I need to change
Someone have any sample of Comet app .net? I need one sample how to
I need to create next and previous link urls here's a sample <a href=/calendar/2009/10/>previous</a>
Hi I need to downsample a wav audio file's sample rate from 44.1kHz to
I need to setup NACHOS java version in Linux and run some simple sample

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.