如何将DataTable一次写入数据库,而不是遍历插入(一行一行插入)。
我的代码:
insert_sql(DataTable DT)
{
string SelectSQL="select * from tblName"; 
SqlCommand SelectCmd = new SqlCommand(SelectSQL, Con); string InsertSQL="insert into tblName (Input_Time,Location_ID) values (@Input_Time,@Location_ID)"; 
SqlCommand InsertCmd = new SqlCommand(InsertSQL, Con); 
InsertCmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Input_Time",System.Data.SqlDbType.Time)); 
InsertCmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Location_ID",System.Data.SqlDbType.NVarChar)); SqlDataAdapter Dad = new SqlDataAdapter(); 
Dad.SelectCommand = SelectCmd; 
Dad.InsertCommand = InsertCmd; 
Dad.Update(DT);
}
   运行上面,查看数据库,并没有把DT的内容inset到表 tblName中,SqlDataAdapter.Update() 请解释下这个到底是干了什么?   这个代码有错吗????

解决方案 »

  1.   


     OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = new OleDbCommand(queryString, connection);
            OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);        connection.Open();        DataSet customers = new DataSet();
            adapter.Fill(customers);        //code to modify data in dataset here        adapter.Update(customers);        return customers;
      

  2.   

    楼主的 
    Dad.SelectCommand = SelectCmd; 
    Dad.InsertCommand = InsertCmd;干嘛的 谁能帮我解释下不
      

  3.   

    update使用的前提是你从数据库查询出数据,然后你修改了这个数据,再Update提交。
    这是我以前写的一段代码
            private Boolean UpdateFromRead(DataSet ds)
            {
                //更新数据
                if (sqlconn.State != ConnectionState.Open)
                    sqlconn.Open();            DataSet dsOldData = new DataSet();
                string steelTbName = stlInfo;
                string typTbName = typInfo;
                string hotSteelInfo = HotInfo;
                SqlDataAdapter sqdSteel = new SqlDataAdapter("select * from " + steelTbName, sqlconn);
                sqdSteel.Fill(dsOldData, steelTbName);            SqlDataAdapter sqdType = new SqlDataAdapter("select * from " + typTbName, sqlconn);
                sqdType.Fill(dsOldData, typTbName);            SqlDataAdapter adptHotSteel = new SqlDataAdapter("select * from " + hotSteelInfo, sqlconn);
                adptHotSteel.Fill(dsOldData,hotSteelInfo);            UpdateAdapter(sqdSteel, ds.Tables[steelTbName], dsOldData.Tables[steelTbName]);            UpdateAdapter(sqdType, ds.Tables[typTbName], dsOldData.Tables[typTbName]);            UpdateAdapter(adptHotSteel, ds.Tables[hotSteelInfo], dsOldData.Tables[hotSteelInfo]);
                return true;
            }        private Boolean UpdateAdapter(SqlDataAdapter sda, DataTable sourceDt, DataTable WillUpdateDt)
            {
                SqlCommandBuilder cmdbuilder = new SqlCommandBuilder(sda);
                WillUpdateDt.Clear();
                for (int j = 0; j < sourceDt.Rows.Count; j++)
                {
                    WillUpdateDt.LoadDataRow(sourceDt.Rows[j].ItemArray, false);
                }
                sda.InsertCommand = cmdbuilder.GetInsertCommand();
                sda.UpdateCommand = cmdbuilder.GetUpdateCommand();
                
                sda.Update(WillUpdateDt);
                return true;
            }
      

  4.   

     string str="Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Userinfo.mdf;Integrated Security=True;User Instance=True";
            SqlConnection conn=new SqlConnection();
            conn.ConnectionString=str;
            string sqlstr = "insert into tblName select * from DT";                           
             SqlCommand comm = new SqlCommand(sqlstr,conn);   //使用command方法执行
             try
             {
                 conn.Open();
                 comm.ExecuteNonQuery();        //返回执行行数    在此为保存数据
                 Response.Write("插入成功!!");     
             }
             catch (SqlException ee)
             {
             }
             finally
             {
                 conn.Close();
             }
      

  5.   


          using(SqlConnection conn = new SqlConnection(""0)) 
          {  
             conn.Open();
             using (SqlBulkCopy sqlBC = new SqlBulkCopy(conn))
             {            sqlBC.BatchSize = 1000;
                sqlBC.BulkCopyTimeout = 60; 
                sqlBC.NotifyAfter = 10000; 
                sqlBC.DestinationTableName = "dbo.test"; 
                sqlBC.ColumnMappings.Add("id", "id");
                sqlBC.ColumnMappings.Add("name", "name"); 
                sqlBC.WriteToServer(dt);
             }
             conn.Dispose();
             }
             
      

  6.   

    问题以及解决 相关方法 可以看我的一篇BLOG  http://blog.csdn.net/xiven/archive/2009/11/20/4841897.aspx
      

  7.   

    public static int AddUserInfo(tbUser userInfo)
            {
                int result = 0;            string sql =
                    "INSERT tbUser (uname,pwd,lastlogin,loginip,islock,headimg,webstyle)" +
                    "VALUES ( @uname, @pwd, @lastlogin, @loginip, @islock, @headimg, @webstyle)";            try
                {
                    SqlParameter[] para = new SqlParameter[]
    {
    //new SqlParameter("@uid", userInfo.UId), //FK
    new SqlParameter("@uname", userInfo.UName), //FK
    new SqlParameter("@pwd", userInfo.Pwd), //FK
    new SqlParameter("@lastlogin", userInfo.LastLogin),
    new SqlParameter("@loginip", userInfo.LoginIP),
    new SqlParameter("@islock", userInfo.IsLock),
    new SqlParameter("@headimg", userInfo.HeadImg),
    new SqlParameter("@webstyle", userInfo.WebStyle)
    };                result = SqlHelper.ExecuteNonQuery(DataProvider.ConnectionString, CommandType.Text, sql, para);            }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    throw e;
                }
                return result;
            }