954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

insert array in database

Hi Guys ,
can any one tell me how to insert array in database using insert statement.
thanks

sdhawan
Junior Poster
122 posts since May 2010
Reputation Points: 8
Solved Threads: 0
 

What database are you using?

finito
Nearly a Posting Virtuoso
1,321 posts since May 2010
Reputation Points: 60
Solved Threads: 135
 

if u r using sqlserver, it should be easy.
Before sending it to database traverse the array and make a single string with array values in a "," or ":" then save the string as varchar into database. When u need to fetch the data u just have to break it into substrings..

mono_jit23
Junior Poster in Training
51 posts since Jun 2010
Reputation Points: 13
Solved Threads: 10
 

Saving arrays in a database as any kind of delimited field is usually not a good idea. The point of databases is normalization . Why don't you save the array as individual rows in a detail table and join it to the primary table?

The implementation of that depends on which rdbms you are using.

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

thanks for the reply guys .I got that part working but when i inserting values in databse i get this exception error Incorrect syntax near '/'.Can anyone help me with this.I am sure that i dont have this sysmbol anywhere in my string or text.Thanks

sdhawan
Junior Poster
122 posts since May 2010
Reputation Points: 8
Solved Threads: 0
 

can you post your code so we may analyse and tell you.

finito
Nearly a Posting Virtuoso
1,321 posts since May 2010
Reputation Points: 60
Solved Threads: 135
 

sorry i knew what the problem was the problem now is i am getting error Index was outside the bounds of the array.my code is as follows

private void saveDataInDatabase()
        {
            string[] key = null;
            for (int i = 0; i < allData.Count; i++)
            {
                
                TextProcItem item = (TextProcItem)allData[i];
                string text1 = item.text.ToString();
                text1 = text1.Replace("'", "''");
                string sent2 = item.sentences.ToString();
                sent2 = sent2.Replace("'", "''");
                if (item.keywords.Length > item.sentences.Length && item.keywords.Length > item.nouns.Length && item.keywords.Length > item.adjectives.Length && item.keywords.Length > item.verbs.Length)
                {
                    for (int j = 0; j < item.keywords.Length; j++)
                    {


                        //insert query procedure here
                        string myString;
                        SqlConnection cn = new SqlConnection(@"Server=208.124.179.198;Database=Content_text;UID=sa;Password=November17th;");
                        cn.Open();
                        myString = @"INSERT INTO contentTbl(Author,Title,text,badsent,all_sent,sentences,key_word,noun,verbs,adjectives)  Values('" + "me" + "','" + item.article + "','" + text1 + "','" + item.badSent + "','" + item.allSentNumber + "','" + sent2 + "','" + item.keywords[j].ToString() + "','" + item.nouns[j].ToString() + "','" + item.verbs[j].ToString() + "','" + item.adjectives[j].ToString() + "')";
                        SqlCommand myCmd = new SqlCommand(myString, cn);
                        myCmd.ExecuteNonQuery();

                        cn.Close();


                    }
                }
                else if (item.sentences.Length > item.keywords.Length && item.sentences.Length > item.nouns.Length && item.sentences.Length > item.adjectives.Length && item.sentences.Length > item.verbs.Length)
                {
                    string text2 = item.text.ToString();
                    text2 = text2.Replace("'", "''");
                    string sent3 = item.sentences.ToString();
                    sent3 = sent3.Replace("'", "''");
                    for (int j = 0; j < item.sentences.Length; j++)
                    {


                        //insert query procedure here
                        string myString;
                        SqlConnection cn = new SqlConnection(@"Server=208.124.179.198;Database=Content_text;UID=sa;Password=November17th;");
                        cn.Open();
                        myString = @"INSERT INTO contentTbl(Author,Title,text,badsent,all_sent,sentences,key_word,noun,verbs,adjectives)  Values('" + "me" + "','" + item.article + "','" + text2 + "','" + item.badSent + "','" + item.allSentNumber + "','" + sent3 + "','" + item.keywords[j].ToString() + "','" + item.nouns[j].ToString() + "','" + item.verbs[j].ToString() + "','" + item.adjectives[j].ToString() + "')";
                        SqlCommand myCmd = new SqlCommand(myString, cn);
                        myCmd.ExecuteNonQuery();

                        cn.Close();


                    }


                }
                else if (item.nouns.Length > item.keywords.Length && item.nouns.Length > item.sentences.Length && item.nouns.Length > item.adjectives.Length && item.nouns.Length > item.verbs.Length)
                {
                    string text3 = item.text.ToString();
                    text3 = text3.Replace("'", "''");
                    string art = item.article.ToString();
                    string sent = item.sentences.ToString();
                    sent = sent.Replace("'", "''");
                    
                    for (int j = 0; j < item.nouns.Length; j++)
                    {


                        //insert query procedure here
                        string myString;
                        SqlConnection cn = new SqlConnection(@"Server=208.124.179.198;Database=Content_text;UID=sa;Password=November17th;");
                        cn.Open();
                        myString = @"INSERT INTO contentTbl(Author,Title,text,badsent,all_sent,sentences,key_word,noun,verbs,adjectives)Values('" + "me" + "','" + art + "','" + text3 + "','" + item.badSent + "','" + item.allSentNumber + "','" + sent + "','" + item.keywords[j].ToString() + "','" + item.nouns[j].ToString() + "','" + item.verbs[j].ToString() + "','" + item.adjectives[j].ToString() + "')";
                        SqlCommand myCmd = new SqlCommand(myString, cn);
                        myCmd.ExecuteNonQuery();

                        cn.Close();


                    }


                }
                else if (item.adjectives.Length > item.keywords.Length && item.adjectives.Length > item.sentences.Length && item.adjectives.Length > item.nouns.Length && item.adjectives.Length > item.verbs.Length)
                {
                    string text4 = item.text.ToString();
                    text4 = text4.Replace("'", "''");
                    string sent4 = item.sentences.ToString();
                    sent4 = sent4.Replace("'", "''");
                    for (int j = 0; j < item.adjectives.Length; j++)
                    {


                        //insert query procedure here
                        string myString;
                        SqlConnection cn = new SqlConnection(@"Server=208.124.179.198;Database=Content_text;UID=sa;Password=November17th;");
                        cn.Open();
                        myString = @"INSERT INTO contentTbl(Author,Title,text,badsent,all_sent,sentences,key_word,noun,verbs,adjectives)  Values('" + "me" + "','" + item.article + "','" + text4 + "','" + item.badSent + "','" + item.allSentNumber + "','" + sent4 + "','" + item.keywords[j].ToString() + "','" + item.nouns[j].ToString() + "','" + item.verbs[j].ToString() + "','" + item.adjectives[j].ToString() + "')";
                        SqlCommand myCmd = new SqlCommand(myString, cn);
                        myCmd.ExecuteNonQuery();

                        cn.Close();


                    }


                }
                else if (item.verbs.Length > item.keywords.Length && item.verbs.Length > item.sentences.Length && item.verbs.Length > item.nouns.Length && item.verbs.Length > item.adjectives.Length)
                {
                    string text5 = item.text.ToString();
                    text5 = text5.Replace("'", "''");
                    string sent5 = item.sentences.ToString();
                    sent5 = sent5.Replace("'", "''");
                    for (int j = 0; j < item.verbs.Length; j++)
                    {


                        //insert query procedure here
                        string myString;
                        SqlConnection cn = new SqlConnection(@"Server=208.124.179.198;Database=Content_text;UID=sa;Password=November17th;");
                        cn.Open();
                        myString = @"INSERT INTO contentTbl(Author,Title,text,badsent,all_sent,sentences,key_word,noun,verbs,adjectives)  Values('" + "me" + "','" + item.article + "','" + text5 + "','" + item.badSent + "','" + item.allSentNumber + "','" + sent5 + "','" + item.keywords[j].ToString() + "','" + item.nouns[j].ToString() + "','" + item.verbs[j].ToString() + "','" + item.adjectives[j].ToString() + "')";
                        SqlCommand myCmd = new SqlCommand(myString, cn);
                        myCmd.ExecuteNonQuery();

                        cn.Close();


                    }


                }

            }
        }

i would really appriciate if someone can help me with this

sdhawan
Junior Poster
122 posts since May 2010
Reputation Points: 8
Solved Threads: 0
 

i got in in the insert statement i need to replace [j] with [i].thanks

sdhawan
Junior Poster
122 posts since May 2010
Reputation Points: 8
Solved Threads: 0
 

always nice to see when someone solves their problem. Please mark as solved if solved.

finito
Nearly a Posting Virtuoso
1,321 posts since May 2010
Reputation Points: 60
Solved Threads: 135
 

Sorry finito it is suppose to be j so i am back to square one, i am still getting error array index out of bound.Plz help :(

sdhawan
Junior Poster
122 posts since May 2010
Reputation Points: 8
Solved Threads: 0
 

do anyone knw how can i store an array in a string and insert that string in databse.So supose if i have string with values a,b,c ,d.... etc i dont want each value in different row of databse table, i want them ina single row.Can anyone help me with this

sdhawan
Junior Poster
122 posts since May 2010
Reputation Points: 8
Solved Threads: 0
 

you never told me which Sql server you are using?

finito
Nearly a Posting Virtuoso
1,321 posts since May 2010
Reputation Points: 60
Solved Threads: 135
 
do anyone knw how can i store an array in a string and insert that string in databse.So supose if i have string with values a,b,c ,d.... etc i dont want each value in different row of databse table, i want them ina single row.Can anyone help me with this


My answer is absolutely not. These are two completely different data structures. There are work around like putting it in a blob (serialization) or comma separating a text column but you don't to access the the individual values in your queries.NOTE: Use parametrized query.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You