Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Need help with Sqldatareader

Need help with Sqldatareader

Scheduled Pinned Locked Moved C#
databasesql-serverhelptutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    erikhjerpe
    wrote on last edited by
    #1

    Hi im new to Both C# and sql and exept from that my problem is: I´ve managed to connect to a mssql database (express edition) from my C# program and I have the sqldata reader to read my question to listboxes in my windowsForm. Ive understood from "Beginners guide to accessing SQL Server through C#" that once you got your connection running and the reader has read the table in the database You can rephrase your question without reading directly to the database. If that is correct, Is it possible to establish a connection to the sql database in "program.cs" and get your reader to read the whole table and then let different windowsForms make sql questions to that reader? Im hoping to gain less questions to the DB and its supposed to get the table every five minutes or something. I would be very happy if anyone could help me in this matter Here is an example of code in my Form: public partial class BinMonitor : Form { private SqlConnection conn; private SqlCommand cmd; private SqlDataReader reader; string Sql; public BinMonitor() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } // Gets the actual time and returns to a textbox private void timer1_Tick(object sender, EventArgs e) { textBox1.Text = DateTime.Now.ToLongTimeString(); } //gets values from DB once a minute private void timer2_Tick(object sender, EventArgs e) { listBox1.Items.Clear(); listBox2.Items.Clear(); listBox3.Items.Clear(); string connStr = "server=DBserver; user id=user; password=pwd; database=flights;Trusted_Connection=yes;"; SqlConnection conn = new SqlConnection(connStr); conn.Open(); reader = null; cmd = new SqlCommand("SELECT * FROM BinMonitor Where Departure !<" + "'" + textBox1.Text + "'" + "order by Departure ASC", conn); reader = cmd.ExecuteReader(); reader.Read(); while (reader.Read()) { listBox1.Items.Add(reader.GetString(0)); listBox2.Items.Add(reader.GetString(1)); listBox3.Items.Add(reader.GetString(2)); } } } } Lost in space

    C C 2 Replies Last reply
    0
    • E erikhjerpe

      Hi im new to Both C# and sql and exept from that my problem is: I´ve managed to connect to a mssql database (express edition) from my C# program and I have the sqldata reader to read my question to listboxes in my windowsForm. Ive understood from "Beginners guide to accessing SQL Server through C#" that once you got your connection running and the reader has read the table in the database You can rephrase your question without reading directly to the database. If that is correct, Is it possible to establish a connection to the sql database in "program.cs" and get your reader to read the whole table and then let different windowsForms make sql questions to that reader? Im hoping to gain less questions to the DB and its supposed to get the table every five minutes or something. I would be very happy if anyone could help me in this matter Here is an example of code in my Form: public partial class BinMonitor : Form { private SqlConnection conn; private SqlCommand cmd; private SqlDataReader reader; string Sql; public BinMonitor() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } // Gets the actual time and returns to a textbox private void timer1_Tick(object sender, EventArgs e) { textBox1.Text = DateTime.Now.ToLongTimeString(); } //gets values from DB once a minute private void timer2_Tick(object sender, EventArgs e) { listBox1.Items.Clear(); listBox2.Items.Clear(); listBox3.Items.Clear(); string connStr = "server=DBserver; user id=user; password=pwd; database=flights;Trusted_Connection=yes;"; SqlConnection conn = new SqlConnection(connStr); conn.Open(); reader = null; cmd = new SqlCommand("SELECT * FROM BinMonitor Where Departure !<" + "'" + textBox1.Text + "'" + "order by Departure ASC", conn); reader = cmd.ExecuteReader(); reader.Read(); while (reader.Read()) { listBox1.Items.Add(reader.GetString(0)); listBox2.Items.Add(reader.GetString(1)); listBox3.Items.Add(reader.GetString(2)); } } } } Lost in space

      C Offline
      C Offline
      chinaQI
      wrote on last edited by
      #2

      I am very happy to help you, I would not know that you want to express the link MYSQL or other intention. I hope you can more simple expression of what you mean. MSN : chenqi1000y@163.com

      E 1 Reply Last reply
      0
      • C chinaQI

        I am very happy to help you, I would not know that you want to express the link MYSQL or other intention. I hope you can more simple expression of what you mean. MSN : chenqi1000y@163.com

        E Offline
        E Offline
        erikhjerpe
        wrote on last edited by
        #3

        Thank you for helping! I was hoping to get an answer if its possible to make the sqlreader work for several Forms at the same time, and if so how?

        1 Reply Last reply
        0
        • E erikhjerpe

          Hi im new to Both C# and sql and exept from that my problem is: I´ve managed to connect to a mssql database (express edition) from my C# program and I have the sqldata reader to read my question to listboxes in my windowsForm. Ive understood from "Beginners guide to accessing SQL Server through C#" that once you got your connection running and the reader has read the table in the database You can rephrase your question without reading directly to the database. If that is correct, Is it possible to establish a connection to the sql database in "program.cs" and get your reader to read the whole table and then let different windowsForms make sql questions to that reader? Im hoping to gain less questions to the DB and its supposed to get the table every five minutes or something. I would be very happy if anyone could help me in this matter Here is an example of code in my Form: public partial class BinMonitor : Form { private SqlConnection conn; private SqlCommand cmd; private SqlDataReader reader; string Sql; public BinMonitor() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } // Gets the actual time and returns to a textbox private void timer1_Tick(object sender, EventArgs e) { textBox1.Text = DateTime.Now.ToLongTimeString(); } //gets values from DB once a minute private void timer2_Tick(object sender, EventArgs e) { listBox1.Items.Clear(); listBox2.Items.Clear(); listBox3.Items.Clear(); string connStr = "server=DBserver; user id=user; password=pwd; database=flights;Trusted_Connection=yes;"; SqlConnection conn = new SqlConnection(connStr); conn.Open(); reader = null; cmd = new SqlCommand("SELECT * FROM BinMonitor Where Departure !<" + "'" + textBox1.Text + "'" + "order by Departure ASC", conn); reader = cmd.ExecuteReader(); reader.Read(); while (reader.Read()) { listBox1.Items.Add(reader.GetString(0)); listBox2.Items.Add(reader.GetString(1)); listBox3.Items.Add(reader.GetString(2)); } } } } Lost in space

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          erikhjerpe wrote:

          If that is correct, Is it possible to establish a connection to the sql database in "program.cs" and get your reader to read the whole table and then let different windowsForms make sql questions to that reader?

          No. A reader is a one-time-forward-only view of the data that is being retrieved from the database. Once a row has been read from the reader you cannot go back and read it again. You may wish to look into DataAdapters in order to Fill a DataSet with the data which you can store in a central location and allow the forms to access.

          erikhjerpe wrote:

          Here is an example of code in my Form

          It is unwise to access data dirctly from a form.

          erikhjerpe wrote:

          cmd = new SqlCommand("SELECT * FROM BinMonitor Where Departure !<" + "'" + textBox1.Text + "'" + "order by Departure ASC", conn); reader

          This code is susceptable to SQL Injection Attacks. Please read up on SQL Injection Attacks and Tips on How to Prevent Them[^]


          Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

          E 1 Reply Last reply
          0
          • C Colin Angus Mackay

            erikhjerpe wrote:

            If that is correct, Is it possible to establish a connection to the sql database in "program.cs" and get your reader to read the whole table and then let different windowsForms make sql questions to that reader?

            No. A reader is a one-time-forward-only view of the data that is being retrieved from the database. Once a row has been read from the reader you cannot go back and read it again. You may wish to look into DataAdapters in order to Fill a DataSet with the data which you can store in a central location and allow the forms to access.

            erikhjerpe wrote:

            Here is an example of code in my Form

            It is unwise to access data dirctly from a form.

            erikhjerpe wrote:

            cmd = new SqlCommand("SELECT * FROM BinMonitor Where Departure !<" + "'" + textBox1.Text + "'" + "order by Departure ASC", conn); reader

            This code is susceptable to SQL Injection Attacks. Please read up on SQL Injection Attacks and Tips on How to Prevent Them[^]


            Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

            E Offline
            E Offline
            erikhjerpe
            wrote on last edited by
            #5

            Thank you very much for the tips, I certainly will look into DataAdapters, have a vague ideea that thats what I´m looking for. About Injection attacks Im´not afraid since the application is supposed for a closed network and without any input from users other than to load a form. (Trying to build a system of info displays). Anyway thanks a lot for your answers

            C 1 Reply Last reply
            0
            • E erikhjerpe

              Thank you very much for the tips, I certainly will look into DataAdapters, have a vague ideea that thats what I´m looking for. About Injection attacks Im´not afraid since the application is supposed for a closed network and without any input from users other than to load a form. (Trying to build a system of info displays). Anyway thanks a lot for your answers

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              erikhjerpe wrote:

              About Injection attacks Im´not afraid since the application is supposed for a closed network and without any input from users other than to load a form

              Statistically most system breaches are inside jobs.


              Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

              E 1 Reply Last reply
              0
              • C Colin Angus Mackay

                erikhjerpe wrote:

                About Injection attacks Im´not afraid since the application is supposed for a closed network and without any input from users other than to load a form

                Statistically most system breaches are inside jobs.


                Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website

                E Offline
                E Offline
                erikhjerpe
                wrote on last edited by
                #7

                You´re right, at the moment I´m taking my first trembling steps in programming, and it´s probably a good Idea to learn from the start to think about security. Thanks

                J 1 Reply Last reply
                0
                • E erikhjerpe

                  You´re right, at the moment I´m taking my first trembling steps in programming, and it´s probably a good Idea to learn from the start to think about security. Thanks

                  J Offline
                  J Offline
                  Justin Jones 0
                  wrote on last edited by
                  #8

                  We all have to start somewhere. Just a few tips of my own and I apologize if any are repeats. Think stored procedures. That will solve the Sql injection attack problem by itself. At the very least use parameterized queries. This will solve more than just security issues. Imagine the headache of having to debug your app at 2am because somebody entered "Bob O'Malley" into a textbox? put data connections, data readers, and commands into using statements. Especially the connections. Again, you don't want to be figuring out why your server came to a screeching halt at 2am because no connections ever closed. DataSets are great, you can also use the reader to load that data into a collection of objects, close your connections, then pass the collection around anywhere you need it. I know some of this may seem advanced if you're just getting started, but remember this is where you want to be going. It's never to early to start doing it right. Oh, one more thing that I just noticed: you don't need a priming read on your datareader. This will actually cause you to throw away the first row of data. HTH.

                  J Make the logo bigger

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups