none
string data -- How can i insert into sql server table. RRS feed

  • Question

  • I am able to get my string as :

    Tag:0310 1000 0000 0000 0000 0000, Disc:2012/10/16 05:32:10.631, Last:2012/10/16 05:33:01.461, Count:414, Ant:0, Proto:2
    Tag:0311 1000 0000 0000 0000 0000, Disc:2012/10/16 05:54:10.631, Last:2012/10/16 05:54:01.461, Count:414, Ant:0, Proto:1
    Tag:0410 1000 0000 0000 0000 0000, Disc:2012/10/16 05:43:10.631, Last:2012/10/16 05:43:01.461, Count:414, Ant:1, Proto:2

    etc....

    if i had 3 tags near to my reader i will get 3 rows of data. If i had 10 tags i will get 10 rows of data. I am able to read this data into the string. Now How can i insert this data into the sql server table.


    Subhash

    Friday, October 19, 2012 4:49 PM

Answers

  • In your old thread, you already got your answer, you Split() the string, maybe use SubString(), and then insert into the database(Assuming that you want insert a new row for each value in these *tags*). If this is one string, just pass it as a parameter and then insert.

    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    • Marked as answer by Kris_sp2013 Sunday, September 14, 2014 9:57 AM
    Friday, October 19, 2012 5:01 PM

All replies

  • If the data is a single string you need to split it into rows, them split into columns to match the table in SQL Server.

    Well that's what it looks like. But it is hard to tell since you don't say how the string is constructed nor what columns the table has. Maybe you don't need to do any splitting just collect the data and post it?


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Friday, October 19, 2012 4:54 PM
  • In your old thread, you already got your answer, you Split() the string, maybe use SubString(), and then insert into the database(Assuming that you want insert a new row for each value in these *tags*). If this is one string, just pass it as a parameter and then insert.

    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    • Marked as answer by Kris_sp2013 Sunday, September 14, 2014 9:57 AM
    Friday, October 19, 2012 5:01 PM
  • I want to split each string then need to save in sql server.want to insert Tag and date time values.

    Tag:0310 1000 0000 0000 0000 0000, Disc:2012/10/16 05:32:10.631, Last:2012/10/16 05:33:01.461, Count:414, Ant:0, Proto:2
    Tag:0311 1000 0000 0000 0000 0000, Disc:2012/10/16 05:54:10.631, Last:2012/10/16 05:54:01.461, Count:414, Ant:0, Proto:1
    Tag:0410 1000 0000 0000 0000 0000, Disc:2012/10/16 05:43:10.631, Last:2012/10/16 05:43:01.461, Count:414, Ant:1, Proto:2

    etc..

    now i want to insert tagno : 0311 1000 0000 0000 0000 000,disc:2012/10/16 05:54:10.631 values into sqlserver



    Subhash

    Saturday, October 20, 2012 1:42 AM
  • Where exactly in the process did you need assistance? The breaking down of your data into parts that can be put into the database? 

    Do you have the database already, and if so, what do your tables look like?

    Are you just looking for a possible way of getting that data into the database programatically, like, 

    SqlConnection conn = new SqlConnection(GetConnectionString()); string sql = "INSERT INTO TagTable (TagNo) VALUES (@Val1)";

    try { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@Val1", @"0311 1000 0000 0000 0000 000,disc:2012/10/16 05:54:10.631"); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:"; msg += ex.Message; } finally { conn.Close(); }

    Can you clarify a bit?


    Saturday, October 20, 2012 6:36 AM
  • send data to sql as xml format.

    CREATE PROCEDURE AddEmployee
       
    @empXml xml
    AS

    INSERT INTO Employee
    (
        ID
    ,
        EmpName
    ,
        Sal
    )
    VALUES
    (
       
    @empXml.value('(/Record/Emp/ID)[1]', 'int'),
       
    @empXml.value('(/Record/Emp/EmpName)[1]', 'varchar(max)'),
       
    @empXml.value('(/Record/Emp/Sal)[1]', 'int')
    )


    http://stackoverflow.com/questions/4758992/how-to-insert-xml-data-into-table-in-sql-server-2005

    Saturday, October 20, 2012 6:56 AM
  • Hi.. Thanks for your kind help.

    When i am spiting the string 

    Tag:0310 1000 0000 0000 0000 0000, Disc:2012/10/16 21:42:27.372, Last:2012/10/16 21:57:10.139, Count:347, Ant:1, Proto:2
    Tag:0082 0000 0000 0000 0000 0000, Disc:2012/10/16 21:43:27.418, Last:2012/10/16 21:57:10.236, Count:605, Ant:0, Proto:2

    1st time  tag reading was fine. reading as : 0310 1000 0000 0000 0000 0000

    2nd time i am getting tagno as  : o:2Tag:0082 3000 0000 0000 0000 0000 . But it should read as   0082 0000 0000 0000 0000 0000.

    finally i want to insert Tagno : 03101 time : 2012/10/16 21:42.

    tagno : 00823, Time : 2012/10/16 21:43.

    need some validation : if the same tag read in a span of 5min , it should not be saved in the database.

    Pls help me out.

     

    Subhash

    Monday, October 22, 2012 6:43 AM
  • Hi.. Thanks for your kind help.

    When i am spilting the string 

    Tag:0310 1000 0000 0000 0000 0000, Disc:2012/10/16 21:42:27.372, Last:2012/10/16 21:57:10.139, Count:347, Ant:1, Proto:2
    Tag:0082 0000 0000 0000 0000 0000, Disc:2012/10/16 21:43:27.418, Last:2012/10/16 21:57:10.236, Count:605, Ant:0, Proto:2

    1st time  tag reading was fine. reading as : 0310 1000 0000 0000 0000 0000

    2nd time i am getting tagno as  : o:2Tag:0082 3000 0000 0000 0000 0000 . But it should read as   0082 0000 0000 0000 0000 0000.

    finally i want to insert  Tagno : 03101 time : 2012/10/16 21:42. tagno : 00823, Time : 2012/10/16 21:43.

    need some validation : if the same tag read in a span of 5min , it should not be saved in the database.

    Pls help me out.


    Subhash

    Monday, October 22, 2012 6:44 AM