使用c#在SQL中插入数据表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/793506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:21:54  来源:igfitidea点击:

Datatable insertion in SQL using c#

c#sql

提问by Siddiqui

How can we insert the values of datatable in SQL using c#? And the second question is how we can copy the SQL table into datatable variable?

我们如何使用c#在SQL中插入数据表的值?第二个问题是我们如何将 SQL 表复制到数据表变量中?

采纳答案by TheTXI

UPDATE: One thing this answer didn't have in the past is links to information for SQL and database newbies, so I will put some relevant links here as well so that you (or anyone else) can brush up on their SQL and other database design skills.

更新:这个答案过去没有的一件事是 SQL 和数据库新手信息的链接,所以我也会在这里放一些相关的链接,以便您(或其他任何人)可以复习他们的 SQL 和其他数据库设计技巧。



UPDATE 2: Here is an example of filling a datatable:

更新 2:以下是填充数据表的示例:

//Namespace References
using System.Data;
using System.Data.SqlClient

/// <summary>
/// Returns a DataTable, based on the command passed
/// </summary>
/// <param name="cmd">
/// the SqlCommand object we wish to execute
/// </param>
/// <returns>
/// a DataTable populated with the data
/// specified in the SqlCommand object
/// </returns>
/// <remarks></remarks>

public static DataTable GetDataTable(SqlCommand cmd)
{
    try
    {
        // create a new data adapter based on the specified query.
        SqlDataAdapter da = new SqlDataAdapter();

        //set the SelectCommand of the adapter
        da.SelectCommand = cmd;

        // create a new DataTable
        DataTable dtGet = new DataTable();

        //fill the DataTable
        da.Fill(dtGet);

        //return the DataTable
        return dtGet;
      }
      catch (SqlException ex)
      {
          MessageBox.Show(ex.Message);
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message);
      }
  } 


A lot of this is taken from another answer I have written previously, but it goes into detail about your exact issues:

其中很多内容取自我之前写过的另一个答案,但它详细介绍了您的确切问题:

Original Answer:

原答案

This sounds like you more or less need a basic introduction to connecting and manipulating a database from C#. The above poster said to look into LINQ to SQL, but you can also look into the more basic underlying framework of ADO.NET which will get you to understand the basics of how it works.

这听起来您或多或少需要了解从 C# 连接和操作数据库的基本介绍。上面的海报说要研究 LINQ to SQL,但您也可以研究 ADO.NET 的更基本的底层框架,这将使您了解它的工作原理。

Also, you can use this site right herefor a number of different database tutorials for C#.

此外,您可以在此处使用此站点获取许多不同的 C# 数据库教程。

Edit: More information from C# Station, CodeProject, and Codersource

编辑:来自C# StationCodeProjectCodersource 的更多信息

Edit 2: If you are interested in things like Linq to SQL as others have mentioned above, here are some tutorials from C# Corner, and C-Sharp Online

编辑 2:如果您对上面其他人提到的 Linq to SQL 之类的东西感兴趣,这里有一些来自C# CornerC-Sharp Online 的教程

Edit 3: Others would also suggest things such as the ADO.NET Entity Framework. I would not necessarily suggest this for beginners who still need to grasp the fundamentals of working with a database. Here is some information from the MSDN Overview

编辑 3:其他人也会建议诸如 ADO.NET 实体框架之类的东西。对于仍然需要掌握使用数据库的基础知识的初学者,我不一定会建议这样做。以下是MSDN 概述中的一些信息

Simple Example(This is pulled directly from the C# Station link given above)

简单示例(这是直接从上面给出的 C# Station 链接中提取的)

Listing 1. Using a SqlConnection

清单 1. 使用 SqlConnection

using System;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Demonstrates how to work with SqlConnection objects
/// </summary>
class SqlConnectionDemo
{
    static void Main()
    {
        // 1. Instantiate the connection
        SqlConnection conn = new SqlConnection(
            "Data Source=(local);Initial Catalog=Northwind;
             Integrated Security=SSPI");

        SqlDataReader rdr = null;

        try
        {
            // 2. Open the connection
            conn.Open();

            // 3. Pass the connection to a command object
            SqlCommand cmd = 
                new SqlCommand("select * from Customers", conn);

            //
            // 4. Use the connection
            //

            // get query results
            rdr = cmd.ExecuteReader();

            // print the CustomerID of each record
            while (rdr.Read())
            {
                Console.WriteLine(rdr[0]);
            }
        }
        finally
        {
            // close the reader
            if (rdr != null)
            {
                rdr.Close();
            }

            // 5. Close the connection
            if (conn != null)
            {
                conn.Close();
            }
        }
    }
}

回答by lc.

I would read up on ADO.NETand LINQ to SQLfor starters.

对于初学者,我会阅读ADO.NETLINQ to SQL

回答by Jon Skeet

For both of these, you want a relevant data adapter, e.g. SqlDataAdapter.

对于这两种情况,您需要一个相关的数据适配器,例如SqlDataAdapter.

I suggest you find a good ADO.NET book or tutorial - it's bound to be in there. Alternatively, there are MSDN articles on DataAdapters/DataReadersand Retrieving and Modifying Data in ADO.NET.

我建议你找一本好的 ADO.NET 书籍或教程——它肯定会在那里。或者,还有关于DataAdapters/DataReadersRetrieving and Modifying Data in ADO.NET 的MSDN 文章。