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 1013211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:03:59+00:00 2026-05-16T10:03:59+00:00

I am wondering what the best / most efficient / common way is to

  • 0

I am wondering what the best / most efficient / common way is to add a row to an SQL Server table using C# and ADO.NET. I know of course that I can just create an SQL statement for that, but first, the destination table schema might vary, so I want to keep this flexible, and second, there are so much columns that I do not want to code and maintain this manually. So I currently use a SqlCommandBuilder that is automatically creating the proper insert statement for me, together with an SQLDataAdapter, like this:

var dataAdapter = new SqlDataAdapter("select * from sometable", _databaseConnection);
new SqlCommandBuilder(dataAdapter);
dataAdapter.Fill(dataTable);

// ... add row to dataTable, fill fields from some external file that 
// ... includes column names as well, 
//.... add some more field values not from the file, etc. ...

dataAdapter.Update(dataTable);

This seems pretty inefficient though to first grab all the records from the table even though I do not need them for anything (especially considering that there might even already be a million records in there). Using some select statement like select * from sometable where 1=2 would work, but it does not seem like a very clean approach. I imagine there is some different solution for this that I am just not aware of.

Thanks,

Timo

  • 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-16T10:03:59+00:00Added an answer on May 16, 2026 at 10:03 am

    I think the best way to insert rows is by using Stored Procedures through the ADO.NET command object.

    If you are inserting massive amounts of data and are using SQL Server 2008 you can pass DataTable objects to a stored procedure by using a User-Defined Table Types.

    In SQL:

    CREATE TYPE SAMPLE_TABLE_TYPE --
    AS
    field1 VARCHAR(255)
    field2 VARCHAR(255)
    
    CREATE STORED PROCEDURE insert_data
    AS
    @data Sample_TABLE_TYPE
    BEGIN
    INSERT INTO table1 (field1, field1) 
    SELECT username, password FROM @data;
    

    In .NET:

    DataTable myTable = new DataTable();
    myTable.Columns.Add(new DataColumn("field1", typeof(string));
    myTable.Columns.Add(new DataColumn("field1", typeof(string));
    SqlCommand command = new SqlCommand(conn, CommandType.StoredProcedure);
    command.Parameters.Add("@data", myTable);
    command.ExecuteNonQuery();
    

    If you data also contains updates you can use the new MERGE function used in SQL Server 2008 to efficiently perform both inserts and updates in the same procedure.

    However, if creating User-Defined Table Types and creating stored procedures is too much work, and you need a complete dynamic solution I would stick with what you have, with the recommendation of using the

    Where 1 = 0
    

    appended to your SQL text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering the best way to upload file to a web server in
I am just wondering what's the most efficient way to export mysql data to
I'm wondering if this is the most elegant and efficient way of doing this?
I'm wondering which is the best way to create two lookups table for square
I'm wondering what is the most efficient way to obtain autocomplete results from multiple
I was wondering if you could tell me what the most efficient way to
Basically, wondering what the best / most python-y way of doing this is; class
I was wondering what would be the most efficient way to make a map
I am wondering what the most efficient way is to make a CALayer with
I was wondering what the most computationally efficient Python way of cracking this problem

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.