Jump to content

Sql Insert Command...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
avatarofvelius

avatarofvelius

    Newbie

  • Members
  • PipPip
  • 21 posts
I am just at a loss at the moment. ive been staring at this code for about 2 days and cant find any syntax errors in the statement. any help would be appreciated. both files are place in the direct path specified, thanks to anyone that helps.

the error is simply: syntax error in insert statement



using System;

using System.Text;

using System.IO;

using System.Data;

using System.Data.OleDb;


namespace ConsoleApplication1

{

    public class sqlcommands

    {

        public void insertflat()

        {

            FileStream fs = new FileStream("C:\\testnames.txt",

            FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);

            OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\people339.mdb");

            using (StreamReader sr = new StreamReader(fs))

            {

                string commanames;

                commanames = sr.ReadLine();

                char[] delimiterChars = { ' ', ',', '.', ':', '\t' };

                string[] parsednames = commanames.Split(delimiterChars);

                foreach (string s in parsednames)

                {

                    try

                    {

                        Console.WriteLine(insertparsed(s));

                         aConnection.Open();

                        OleDbCommand aCommand = new OleDbCommand(insertparsed(s), aConnection);

                        aCommand.ExecuteNonQuery();

                        aConnection.Close();

                        break;

                    }

                    catch (OleDbException e)

                    {

                        Console.WriteLine("Error {0}", e.Errors[0].Message);

                    }

                }

            }

        }

        public string insertparsed(string s)

        {

            string parsednames = s;

            string insertcom;

            // syntax error here.

            insertcom = "INSERT INTO Table1(names) VALUES('" + parsednames + "')";

            return insertcom;

        }

    }

    class Driver

    {

        static void Main(string[] args)

        {

            sqlcommands sql = new sqlcommands();


            sql.insertflat();


            Console.ReadLine();

        }

    }

}


Attached Files


Edited by avatarofvelius, 08 June 2008 - 04:45 PM.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What is the SQL statement it is generating? You will only be able to insert a single value with that statement structure.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
zeroradius

zeroradius

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,406 posts
Problem solved, AOV kept sugesting that we were using a C# reserved word (we wern't) but i decided to go through the reserved words for all 3 things we were using. The word 'names' (our veriable name) is a Jet 4.0 reserved word, thanks for taking a look at it.

(Sorry i couln't finish my module of the code AOV can't fined my pen drive but i did fined my SAaD book so i will have it for the final)

Edit: and the db conection was put in the wrong method by the group member that wrote the code.

Edited by zeroradius, 09 June 2008 - 05:31 PM.

Posted Image

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts

avatarofvelius said:

ive been staring at this code for about 2 days
What, solidly? Without breaks for eating, sleeping, going to the toilet? :)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
zeroradius

zeroradius

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,406 posts
It is a figure of speech xav
Posted Image

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
It was a joke, zero. :D
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#7
Somnath

Somnath

    Newbie

  • Members
  • PipPip
  • 26 posts
ok let's see ia will try it out and let u know

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
It's OK, zeroradius solved the problem - it was a reserved C# word.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
zeroradius

zeroradius

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,406 posts
not C#, Jet 4.0 thats what really got him he checked the C# reserved words but not the jet 4.0 wich is were the problem was
Posted Image

#10
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
I meant that. :)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums