InsertCommand属性---把数据集的新行保存到数据源中

实例:

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

namespace PersistAdds
{
    class Program
    {
        static void Main(string[] args)
        {
            string connString = @"server=.;
            integrated security=true;
            database =northwind";

            string qry = @"select * from employees where country='UK'";
            string ins = @"insert into employees(firstname,lastname,titleofcourtesy,city,country)
            values(@firstname,@lastname,@titleofcourtesy,@city,@country)";

            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            Console.WriteLine(conn.State);
            try
            {
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand(qry, conn);

                DataSet ds = new DataSet();
                da.Fill(ds, "employees");

                DataTable dt = ds.Tables["employees"];

                DataRow newRow = dt.NewRow();
                newRow["firstname"] = "tan";
                newRow["lastname"] = "ding";
                newRow["titleofcourtesy"] = "Sir";
                newRow["city"] = "luoding";
                newRow["country"] = "UK";
                dt.Rows.Add(newRow);

                //display rows
                foreach (DataRow row in dt.Rows)
                {
                    Console.WriteLine("{0} {1} {2}", row["firstname"].ToString().PadRight(15), row["lastname"].ToString().PadLeft(25), row["city"]);
                }
                SqlCommand cmd = new SqlCommand(ins, conn);

                cmd.Parameters.Add("@firstname", SqlDbType.NVarChar, 10, "firstname");
                cmd.Parameters.Add("@lastname", SqlDbType.NVarChar, 20, "lastname");
                cmd.Parameters.Add("@titleofcourtesy", SqlDbType.NVarChar, 25, "titleofcourtesy");
                cmd.Parameters.Add("@city", SqlDbType.NVarChar, 15, "city");
                cmd.Parameters.Add("@country", SqlDbType.NVarChar, 15, "country");
                da.InsertCommand = cmd;
                da.Update(ds, "employees");
            }
            catch (SqlException e)
            {
                Console.WriteLine("Error :" + e.ToString());
            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();
        }
    }
}

添加一个INSERT语句,并把原查询字符串变量的名称sql改为ins,以便与这个语句区分开来。

            string ins = @"insert into employees(firstname,lastname,titleofcourtesy,city,country)
            values(@firstname,@lastname,@titleofcourtesy,@city,@country)";

在try块中,更新注释被一些代码替换了。使用insertSQL变量ins

 SqlCommand cmd = new SqlCommand(ins, conn);

接着配置命令参数。提供了值的5列分别映射为指定的命令参数。没有提供主键值,因为它由SQL Server生成,其他列可置定,所以不必为它们提供值。注意,所有的值都是当前值,所以不必指定sourceVersion属性。.

      cmd.Parameters.Add("@firstname", SqlDbType.NVarChar, 10, "firstname");
                cmd.Parameters.Add("@lastname", SqlDbType.NVarChar, 20, "lastname");
                cmd.Parameters.Add("@titleofcourtesy", SqlDbType.NVarChar, 25, "titleofcourtesy");
                cmd.Parameters.Add("@city", SqlDbType.NVarChar, 15, "city");
                cmd.Parameters.Add("@country", SqlDbType.NVarChar, 15, "country");

最后,用命令设置数据适配器的InsertCommand属性,在Employees表中插入新行,该命令是在调用Update方法时数据适配器执行的SQL。接着,在数据适配器上调用Update方法,把变化保存到数据库中。这里只添加了一行,但因为SQL是参数化的,所以数据适配器会在employees数据表中查找所有新行,并把对它们的所有插入操作都提交给数据库。

                da.InsertCommand = cmd;
                da.Update(ds, "employees");

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值