1

I'm not really sure where the SqlConnection, SqlCommand and the Open()/Close() goes. I want to use just the single variable cmd throughout the program, hence not using the SqlCommand cmd = new SqlCommand('SELCT * FROM blabla); format.

EDIT: My code below results to the textbox having the text "System.Data.SqlClient.SqlCommand" when i click the button.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source=EDIOTH\SQLEXPRESS;
        Initial Catalog=Try; Integrated Security=SSPI");
        SqlCommand cmd = new SqlCommand();


        public Form1()
        {            

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            cmd.CommandText = "SELECT Pnt_Lname FROM PATIENT WHERE Pnt_ID = 1;";
            txtBox1.Text = cmd.ToString();
            con.Close();
        }
    }
}

3 Answers 3

1

you can create constant string to hold the connection string and then you can do as below in your button1_Click

you don't need to call the close method of sql connection when you use using block as below

using(SqlConnection con = new SqlConnection(connectionString))
using(SqlCommand cmd = con.CreateCommand())
{
   cmd.CommandText = "SELECT Pnt_Lname FROM PATIENT WHERE Pnt_ID = 1";
   con.Open();
   txtBox1.Text =cmd.ExecuteScalar() as string;
}

And also if you need to read Pnt_Lname from database you better use ExecuteScalar method

Sign up to request clarification or add additional context in comments.

2 Comments

Yes that sounds right, although I think I might be missing a library or something. Error 1 'System.Data.SqlClient.SqlConnection' does not contain a definition for 'ExecuteScalar' and no extension method 'ExecuteScalar' accepting a first argument of type 'System.Data.SqlClient.SqlConnection' could be found (are you missing a using directive or an assembly reference?)
@ShrummsDorke it should be cmd.ExecuteScalar()
0

You can use this structure. Use using to properly close and dispose of SqlConnection.

Also, you can define the connection string in your config file and use it from there.

using (SqlConnection conn = new SqlConnection(@"Data Source=EDIOTH\SQLEXPRESS;
    Initial Catalog=Try; Integrated Security=SSPI"))
{
    conn.Open();

    SqlCommand command = conn.CreateCommand();
    command.CommandText = "SELECT Pnt_Lname FROM PATIENT WHERE Pnt_ID = 1";

    txtBox1.Text = (String)command.ExecuteScalar();

}

Comments

0

In case this would be of help to anyone, this is the answer to my question:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Book
{
    public partial class frmBook : Form
    {
        SqlConnection con =
            new SqlConnection(@"Data Source=EDIOTH\SQLEXPRESS;
                Initial Catalog=XXDB; Integrated Security=SSPI");
        SqlCommand cmd;
        public frmBook()
        {
            InitializeComponent();
        }

        private void frmBook_Load(object sender, EventArgs e)
        {
            con.Open();
            cmd = new SqlCommand("SELECT min(Book_ID) FROM Book;",con);
            txtID.Text = cmd.ExecuteScalar().ToString();
            cmd = new SqlCommand("SELECT title FROM Book WHERE Book_ID = '" 
                + txtID.Text + "'", con);
            txtTitle.Text = cmd.ExecuteScalar().ToString();
            con.Close();
            btnSave.Enabled = false;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            int count = int.Parse(txtID.Text) + 1;
            con.Open();
            cmd = new SqlCommand("SELECT title FROM Book WHERE Book_ID = '"
                + count.ToString() +"'", con);
            txtTitle.Text = cmd.ExecuteScalar().ToString();
            txtID.Text = count.ToString();
            con.Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtID.Text = "";
            txtTitle.Text = "";
            txtAuthor.Text = "";
            btnNew.Enabled = false;
            btnSave.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            con.Open();
            cmd = new SqlCommand("INSERT INTO Book (Book_ID, Title, Author) " +
                "VALUES ('"+ txtID.Text +
                "','"+ txtTitle.Text +
                "','"+ txtAuthor.Text +"');", con);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Data saved!");
            btnSave.Enabled = false;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.