Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
mohammad.mujtaba
Starting Member
2 Posts |
Posted - 2015-02-06 : 07:44:32
|
I wanted to insert values in columns as explained in below ex.I am having a table that contains Column1,Column2,Column3,......,Column10.Inside my for loop, i am getting Column1 value then Column2 then Column3 values and so on till Column10.My requirement is that on each iteration,I wanted to insert value of Column1 in field Column1, value of Column2 in field Column2 and so on.Thanks-Mujtabamdmujtaba786@yahoo.co.in |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2015-02-06 : 08:18:36
|
can you give sample code which you have now and explain us the requirement.....--Chandu |
|
|
mohammad.mujtaba
Starting Member
2 Posts |
Posted - 2015-02-07 : 04:43:07
|
int chunkSize = 1;string strEncryptedRand="ABCDEFGHIJKLMNOPQRSTUVWXYZ";for (int i = 0; i <26; i += chunkSize) { string res = strEncryptedRand.Substring(i, chunkSize); SqlCommand cmd = new SqlCommand(); SqlConnection conn=new SqlConnection(""); string insertStr = "Insert into table (Column1) values ('"+res+"')"; cmd = new SqlCommand(insertStr, conn); cmd.ExecuteNonQuery(); }I have 26 column in my table as Column1,Column2,.......,Column26.My requirement is that when i=0 then value of res should get inserted into Column1.When i=1 then value of res should get inserted into Column2.Like wise it will continue the insert operation till i=26 and value of res should get insert into Column26. |
|
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2015-02-07 : 07:00:17
|
I think you are looking for something like:int chunkSize=1;string strEncryptedRand="ABCDEFGHIJKLMNOPQRSTUVWXYZ";string insertcol="";string insertpar="";List<string> insertval=new List<string>();for (int i=1;i<=strEncryptedRand/chunkSize;i+=chunkSize) { insertcol+=(insertcol==""?"":",")+"column"+i.ToString(); insertpar+=(insertpar==""?"":",")+"@col"+i.ToString(); insertval.Add(strEncryptedRand.Substring(i-1,chunkSize));};string insertStr="insert into table ("+insertcol+") values ("+insertpar+")";using (SqlConnection conn=new SqlConnection(/*connection string goes here*/)) { SqlCommand cmd=new SqlCommand(insertStr,conn); for (int i=1;i<=strEncryptedRand/chunkSize;i+=chunkSize) { cmd.Parameters.AddWithValue("@"+i.ToString(),insertval[i-1]); }; cmd.ExecuteNonQuery();}; |
|
|
|
|
|
|
|