Home > Net >  Using the data from the hash table bulk insert appear "have been added, the key words in the di
Using the data from the hash table bulk insert appear "have been added, the key words in the di

Time:09-30

I want to realize this function: asp.net has a button on a page, click after using the transaction automatically insert multiple database records, I use the parameterized statements, the code is as follows:
 
Hashtable ht=new Hashtable ();//create a Hashtable instance
For (int I=0; I & lt; The Month; I++)
{
String sy="insert into ht_ysk (HTBH ysrq, ysje, czy, addtime) values (@ HTBH @ ysrq, @ ysje, @ czy, @ addtime)";
Var pay=new List ()
{
Du. CreateDbParameter (" @ HTBH ", _htbh),
Du. CreateDbParameter (" @ ysrq ", d1. AddMonths (I)),
Du. CreateDbParameter (" @ ysje _yzj),
Du. CreateDbParameter (" @ czy _czy),
Du. CreateDbParameter (" @ addtime ", a DateTime. Now)
};
Ht. The Add (sy, pay);
}
String CJG=du. ExecuteSqlTran (ht);
If (CJG=="ok")
{
Response. Write (" & lt; script> Alert (' batch add success! '); The location. The href='https://bbs.csdn.net/topics/ht_ysk_list.aspx? Id="+ _htbh +" '; </script>" );
The Response. The End ();
}
The else
{
Response. Write (" & lt; script> Alert (' failure, "+ CJG +"); The location. The href='https://bbs.csdn.net/topics/ht_ysk_list.aspx? Id="+ _htbh +" '; </script>" );
The Response. The End ();
}


Execution report errors: have been added, the key word in the dictionary: "insert into ht_ysk (HTBH ysrq, ysje, czy, addtime) values (@ HTBH @ ysrq, @ ysje, @ czy, @ addtime)" to add the key word: "insert into ht_ysk (HTBH ysrq, ysje, czy, addtime) values (@ HTBH @ ysrq, @ ysje, @ czy, @ addtime)"

How to solve?

CodePudding user response:

String sy as key every time is the same, of course, a hash table will throw an error,
In fact you demand has nothing to do with a hash table, used the wrong data containers,


In a similar List Ok, MyCommand may also be Tuple And so on,
Struct MyCommand
{
Public string Cmd.
Public List The Parameters;
}

CodePudding user response:

Your key is the same, this time should not use hash table to store, as long as needed to determine the unique value of the collection is not possible, such as:
The Dictionary; HashMap, but you can write, will you this piece of code in object
 
String sy="insert into ht_ysk (HTBH ysrq, ysje, czy, addtime) values (@ HTBH @ ysrq, @ ysje, @ czy, @ addtime)";
Var pay=new List ()
{
Du. CreateDbParameter (" @ HTBH ", _htbh),
Du. CreateDbParameter (" @ ysrq ", d1. AddMonths (I)),
Du. CreateDbParameter (" @ ysje _yzj),
Du. CreateDbParameter (" @ czy _czy),
Du. CreateDbParameter (" @ addtime ", a DateTime. Now)
};

And then stored in the List Inside,
Or do you use the upstairs said Tuple, but the Tuple must be. Only.net framework of 4.0 support
  • Related