簡體   English   中英

使用VS2012建立SQL連接和創建SQL命令的正確語法是什么?

[英]What is the correct syntax for establishing an SQL connection and creating SQL Commands with VS2012?

我不太確定SqlConnectionSqlCommandOpen() / Close()去向。 我只想在整個程序中使用單個變量cmd,因此不使用SqlCommand cmd = new SqlCommand('SELCT * FROM blabla); 格式。

編輯:我下面的代碼導致當我單擊按鈕時文本框具有文本“ System.Data.SqlClient.SqlCommand”。

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();
        }
    }
}

您可以創建常量字符串來保存連接字符串,然后可以在button1_Click執行以下操作

當您使用using塊時,您不需要調用sql連接的close方法,如下所示

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;
}

而且,如果您需要從數據庫中讀取Pnt_Lname ,則最好使用ExecuteScalar方法

您可以使用此結構。 使用using正確關閉和處置SqlConnection

另外,您可以在配置文件中定義連接字符串,然后從那里使用它。

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();

}

萬一這對任何人都有幫助,這就是我的問題的答案:

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();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM