把添加的新行保存到数据库

// 把添加的新行保存到数据库
// PersistAdds.cs
using System;
using System.Data;
using System.Data.SqlClient;
namespace Ch13
{
    class PersistAdds
    {
        static void Main( string[] args)
        {
            string strConn = "server=.\\MSSQL2012;integrated security=true;database=Northwind";
            string qry = "select * from employees where employeeid=1" ;
            string ins = "insert into employees(firstname,lastname,titleofcourtesy,city,country)"
                + " values(@fname,@lname,@title,@city,@country)" ;
            SqlConnection conn = new SqlConnection(strConn);
            try
            {
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand(qry, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "emp" );
                DataTable dt = ds.Tables[ "emp" ];
               
                Console.WriteLine( "插入前" );
                foreach (DataRow row in dt.Rows)
                {
                    Console.WriteLine( "{0} {1} {2}"
                        , row[ "firstname" ].ToString().PadRight(15)
                        , row[ "lastname" ].ToString().PadRight(15)
                        , row[ "city" ]);
                }
                // 添加新行
                DataRow newrow = dt.NewRow();
                newrow[ "firstname" ] = "张" ;
                newrow[ "lastname" ] = "三" ;
                newrow[ "titleofcourtesy" ] = "先生" ;
                newrow[ "city" ] = "深圳" ;
                newrow[ "country" ] = "中国" ;
                dt.Rows.Add(newrow);
                // 此时数据并没有写入到数据库,而是内存表中
                Console.WriteLine( "插入后" );
                foreach (DataRow row in dt.Rows)
                {
                    Console.WriteLine( "{0} {1} {2}"
                        ,row[ "firstname" ].ToString().PadRight(15)
                        ,row[ "lastname" ].ToString().PadRight(15)
                        ,row[ "city" ]);
                }
                // 创建SqlCommand
                SqlCommand cmd = new SqlCommand(ins, conn);
                cmd.Parameters.Add( "@fname" ,   SqlDbType.NVarChar, 10, "firstname" );
                cmd.Parameters.Add( "@lname" ,   SqlDbType.NVarChar, 20, "lastname" );
                cmd.Parameters.Add( "@title" ,   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, "emp" );
                // 此时数据真正写入到数据库
            }
            catch( Exception ex)
            {
                Console.WriteLine( "出错了:" + ex);
            }
            finally
            {
                conn.Close();
                Console.ReadLine();
            }
        }
    }
}
----------
插入前
Nancy           Davolio         Seattle
插入后
Nancy           Davolio         Seattle
张               三               深圳


来自为知笔记(Wiz)


posted on 2013-08-24 00:10  伊利丹  阅读(206)  评论(0)    收藏  举报