locked
Sql connection RRS feed

  • Question

  • hello,

    I am just trying to read some data in formload but I seem to be missing something. here is the code. THanks. Ps. the datagridview is on a tabcontrol. I dont know if that matters.

    <code>

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.ComponentModel;

    using

     

    System.Data;

    using

     

    System.Drawing;

    using

     

    System.Linq;

    using

     

    System.Text;

    using

     

    System.Windows.Forms;

    using

     

    System.Data.Sql;

    using

     

    System.Data.SqlClient;

    namespace

     

    AquiredTaste

    {

     

    public partial class Form1 : Form

    {

     

    SqlConnection conn;

     

    SqlDataAdapter dataAdapter;

     

    DataSet dataSet;

     

    public Form1()

    {

    InitializeComponent();

    }

     

    private void Form1_Load(object sender, EventArgs e)

    {

    GetData();

    }

     

    private void GetData()

    {

     

    string klConnection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Michael\\Documents\\Visual Studio 2008\\Projects\\AquiredTaste\\AquiredTaste\\ATaste.mdf;Integrated Security=True;User Instance=True";

    conn =

    new SqlConnection(sklConnection);

     

    string klSQL = "SELECT * FROM [Mngment]";

    dataAdapter =

    new SqlDataAdapter(klSQL, conn);

    dataSet =

    new DataSet();

    dataAdapter.Fill(dataSet,

    "Mngment");

    conn.Close();

    bindingSource1.DataSource = dataSet;

     

    //bindingSource1.DataMember = "tClients";

    dgCorp1.DataSource = bindingSource1;

     

     

    }

     

     

    • Moved by Jackie-Sun Thursday, July 21, 2011 7:15 AM (From:ADO.NET DataSet)
    Monday, December 13, 2010 7:24 PM

Answers

  • Hey all,

    figured it out

    here is the code

    <code>

    conn =

    new SqlConnection(klConnection);

     

    string klSQL = "SELECT * FROM [Mngment]";

     

    conn.Open();

     

    string sqdl = "SELECT * FROM [Mngment]";

    dataAdapter =

    new SqlDataAdapter(sqdl, conn);

     

    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

    dataSet =

    new DataSet();

    dataAdapter.Fill(dataSet,

    "Mngment");

    conn.Close();

    bindingSource1.DataSource = dataSet;

    bindingSource1.DataMember =

    "Mngment";

    dgCorp1.DataSource = bindingSource1;

    </code>

     

    I must say it seems like a lot of code to pull data out of database and just bind it to a datagridview. Anyone got anything simpler?

    • Marked as answer by Codewarz Tuesday, December 14, 2010 12:38 AM
    Tuesday, December 14, 2010 12:38 AM

All replies

  • Hey all,

    figured it out

    here is the code

    <code>

    conn =

    new SqlConnection(klConnection);

     

    string klSQL = "SELECT * FROM [Mngment]";

     

    conn.Open();

     

    string sqdl = "SELECT * FROM [Mngment]";

    dataAdapter =

    new SqlDataAdapter(sqdl, conn);

     

    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

    dataSet =

    new DataSet();

    dataAdapter.Fill(dataSet,

    "Mngment");

    conn.Close();

    bindingSource1.DataSource = dataSet;

    bindingSource1.DataMember =

    "Mngment";

    dgCorp1.DataSource = bindingSource1;

    </code>

     

    I must say it seems like a lot of code to pull data out of database and just bind it to a datagridview. Anyone got anything simpler?

    • Marked as answer by Codewarz Tuesday, December 14, 2010 12:38 AM
    Tuesday, December 14, 2010 12:38 AM
  • Can't simplify that too much, but one thing you can do is to get rid of the conn.Open() and conn.Close(). Also, you don't need the CommandBuilder (unless you plan to do something else with it later, like for Updates).

    The reason you don't need the Open and Close, is that the dataAdapter.Fill() method automatically takes care of that for you ... opening the connection if necessary and closing it if it wasn't already open (IOW, if it was open before you called the .Fill() method, it leaves it open when it's done).


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, December 18, 2010 7:10 PM
  • Oh, one other thing ... in a real-world application, you really should not be doing any DataAccess in your UI (Form) ... the DataAccess should be separated out into different classes that live in different projects from your UI project, and your Form would call methods in that DataAccess class. But, if you're just learning, it's an OK way to start. Just wanted you to be aware of the "best practices" of keeping your DataAccess "layers" separate from your UI "layers" (projects).
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, December 18, 2010 7:14 PM