实例:

InsertCommand属性---把数据集的新行保存到数据源中_sqlInsertCommand属性---把数据集的新行保存到数据源中_参数化_02View 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();
        }
    }
}

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

添加一个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");