向DataTable添加一行,随笔

本文介绍如何使用ADO.NET连接数据库并进行数据操作。通过创建SqlConnection连接数据库,利用SqlDataAdapter填充DataSet,并通过SqlCommandBuilder辅助更新数据。文章展示了如何检查数据存在性及在不存在的情况下向表中添加新数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace FindDataRow
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建数据库连接字符串
            SqlConnection thisConnection = new SqlConnection(@"Data Source=WENGJIXI;" +
            @"Initial Catalog=NorthWind;" +
            @"Integrated Security=true;");

            //创建DataAdapter对象,表示一组 SQL 命令和一个数据库连接,它们用于填充 DataSet 和更新数据源
            SqlDataAdapter thisAdapter = new SqlDataAdapter("select CustomerID,CompanyName from Customers", thisConnection);
            //创建一个SQL命名,关联DataAdapter对象。
            SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
           
            //创建DataSet对象
            DataSet thisDataSet=new DataSet();
            thisAdapter.Fill(thisDataSet, "Customers");
            Console.WriteLine("#在向Customers表添加一行前,表里面行的个数:{0}", thisDataSet.Tables["Customers"].Rows.Count);

            //下面的方法也可以用thisAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;替换
            DataColumn[] keys = new DataColumn[1];//因为主键可以有多列,所以用DataColumn数组。
            keys[0] = thisDataSet.Tables["Customers"].Columns["CustomerID"];
            thisDataSet.Tables["Customers"].PrimaryKey = keys;
            @writes:
            Console.WriteLine("请输入你的公司ID");
            string writeData = Console.ReadLine();
            //使用DataRow对象的Find()方法查询XICO是否存在行中。
            DataRow findRow = thisDataSet.Tables["Customers"].Rows.Find(writeData);
            if (findRow == null)
            {
                Console.WriteLine("数据没有在Customers表中,可以向其中添加数据");
                DataRow newRow = thisDataSet.Tables["Customers"].NewRow();
                newRow["CustomerID"] = writeData;
                Console.WriteLine("请输入你的公司名字");
                string companyName = Console.ReadLine();
                newRow["CompanyName"] = companyName;
                thisDataSet.Tables["Customers"].Rows.Add(newRow);
                if ((findRow = thisDataSet.Tables["Customers"].Rows.Find(writeData)) != null)
                    Console.WriteLine("成功向表添加一行数据");

            }
            else
            {
                Console.WriteLine("数据已经在Customers表中");
                goto writes;
            }

            //更新数据库
            thisAdapter.Update(thisDataSet, "Customers");
            Console.WriteLine("#在向Customers表添加一行后,表里面行的个数:{0}", thisDataSet.Tables["Customers"].Rows.Count);

            //关闭数据库连接
            thisConnection.Close();
            Console.ReadLine();
        }
    }
}

转载于:https://www.cnblogs.com/wengxiaoxi/archive/2012/03/10/2389656.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值