简体   繁体   English

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

[英]Datatable insertion in SQL using c#

How can we insert the values of datatable in SQL using c#? 我们如何使用c#在SQL中插入datatable的值? And the second question is how we can copy the SQL table into datatable variable? 第二个问题是我们如何将SQL表复制到datatable变量中?

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#. 这听起来像或多或少需要从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. 上面的海报说看看LINQ to SQL,但你也可以看看ADO.NET的更基本的底层框架,它将让你了解它的工作原理。

Also, you can use this site right here for 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. 编辑3 :其他人也会建议诸如ADO.NET实体框架之类的东西。 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 以下是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();
            }
        }
    }
}

For both of these, you want a relevant data adapter , eg SqlDataAdapter . 对于这两者,您需要一个相关的数据适配器 ,例如SqlDataAdapter

I suggest you find a good ADO.NET book or tutorial - it's bound to be in there. 我建议你找一本好的ADO.NET书或教程 - 它一定会在那里。 Alternatively, there are MSDN articles on DataAdapters/DataReaders and Retrieving and Modifying Data in ADO.NET . 或者,有关于DataAdapters / DataReaders在ADO.NET中检索和修改数据的 MSDN文章。

我会读取ADO.NETLINQ to SQL作为初学者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM