SQL Server数据库访问Db

问题描述:

我尝试将mssql表(10行)中的所有值插入到Access db表(10行)中,

我的问题是,代码可以正常工作,但是它只插入第一个记录(将值重复10次),
请帮助,因为我错了!


I am try to insert all the value from mssql table (10 rows) to Access db table(10 rows),

my problem is that, code is working fine, but it is inserting only the 1st records(dublicate values 10-times),
please help as were am i getting wrong !


string sqlDbpath = "|DataDirectory|";//
            string sqlstring = "Provider=Microsoft.JET.OLEDB.4.0;" + "data source=\"" + sqlDbpath + "MasterDb.mdb\"";
            SqlConnection cn = new SqlConnection(Constring);
            SqlCommand cmd = cn.CreateCommand();
            // cmd.CommandText = @"select SLNo,GameId,GameTimeID,GameTime,GameDate,GameResult,GameResultTime from tbl_game_time_details";
            cmd.CommandText = @"select GameId,GameTimeID,GameTime,GameResult from tbl_game_time_details";
            SqlDataReader dr;
            cn.Open();
            DataTable dt = new DataTable();
            dr = cmd.ExecuteReader();
            dt.Load(dr);
            cn.Close();
            using (OleDbConnection acn = new OleDbConnection(sqlstring))
            {
                try
                {
                    acn.Open();
                    using (OleDbCommand acmd = new OleDbCommand())
                    {
                        acmd.Connection = acn;
                        // acmd.CommandText = @"insert into game(ID,gameid,gametimeid,gametime,GameDate,gameresult,gameresulttime) values(@ID,@gameid,@gametimeid,@gametime,@gamedate,@gameresult,@gameresulttime)";
                        acmd.CommandText = @"insert into game(gameid,gametimeid,gametime,gameresult) values(@gameid,@gametimeid,@gametime,@gameresult)";
                        //MessageBox.Show("Updating Local Database Started...");
                        foreach (DataRow row in dt.Rows)
                        {
                            //Console.WriteLine("{0},{1},{2},{3}{4},{5}", row["SLNo"], row["GameId"], row["GameTimeID"], row["GameTime"], row["GameDate"], row["GameResult"], row["GameResultTime"]);
                            // acmd.Parameters.AddWithValue("@ID", row["SLNo"].ToString());
                            acmd.Parameters.AddWithValue("@gameid", row["GameId"].ToString());
                            acmd.Parameters.AddWithValue("@gametimeid", row["GameTimeID"].ToString());
                            acmd.Parameters.AddWithValue("@gametime", row["GameTime"].ToString());
                            //  acmd.Parameters.AddWithValue("@gamedate", row["GameDate"].ToString());
                            acmd.Parameters.AddWithValue("@gameresult", row["GameResult"].ToString());
                            //acmd.Parameters.AddWithValue("@gameresulttime", row["GameResultTime"].ToString());
                            acmd.ExecuteNonQuery();
                        }
                        acn.Close();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }


在此先感谢您.


Thank''s In Advance.

只是一个建议,但执行命令后可能需要清除Parameter集合.
之后:
Just a suggestion, but you may need to clear the Parameter collection after executing the command.
After:
acmd.ExecuteNonQuery();



看起来当您再次执行命令时,它使用添加的第一个参数和第一个值,然后其余的将被忽略(因为它们具有重复的参数名称.

斯图尔特(Stuart)



It looks like when you are executing the command again, it''s using the first parameters you added, with the first values, then the rest are being ignored (as they have duplicated parameter names.

Stuart


谢谢你
斯图尔特

acmd.ExecuteNonQuery();
acmd.Dispose();
thank you
Stuart

acmd.ExecuteNonQuery();
acmd.Dispose();