繁体   English   中英

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

[英]Datatable insertion in SQL using c#

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

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


更新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);
      }
  } 

其中很多都来自我之前写过的另一个答案,但它详细介绍了您的具体问题:

原答案

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

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

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

编辑2 :如果您对其他人如上所述的Linq to SQL感兴趣,这里有一些来自C#CornerC-Sharp Online的教程

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

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

清单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();
            }
        }
    }
}

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

我建议你找一本好的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