简体   繁体   中英

How to include this code in a C# program

I am running into problems with my Database not opening because it is already, apparently, open.

Unable to copy file "j:...\\KAELC_DB.mdf" to "bin\\Debug\\KAELC_DB.mdf". The process cannot access the file 'j:...\\KAELC_DB.mdf' because it is being used by another process.

Unable to copy file "j:...\\KAELC_DB_log.ldf" to "bin\\Debug\\KAELC_DB_log.ldf". The process cannot access the file 'j:...\\KAELC_DB_log.ldf' because it is being used by another process.

I found a reply to an old question on StackExchange, linked to here https://stackoverflow.com/a/3998383 , by "Justin", which looks to resolve that problem (and I have also read in other places that "using" is one of the most efficient ways of programming in C#) but how do I use this in my code ?

I have created a small Project that does nothing but allow me to press a button in order to process a SQL statement, but I'm confused as to what "Justin" means by "Use the connection" ... how do I put SQL statements into this 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.SqlClient;

namespace MySqlTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Open SQL File
            using (SqlConnection conn = SqlHelper.GetConn())
            {
                // Use the connection <<< How ?!?!?
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Insert Record Into  SQL File

        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Read Record From SQL File

        }

        private void button4_Click(object sender, EventArgs e)
        {
            //Read All Records From SQL File

        }

        private void button5_Click(object sender, EventArgs e)
        {
            //Delete Record From DQL File
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //Close SQL File
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //Quit
            this.Close();
        }

        class SqlHelper
        {
            public static SqlConnection GetConn()
            {
                SqlConnection returnValue = new SqlConnection(@"Data Source=MEDESKTOP;AttachDbFilename=|DataDirectory|\SqlTestDB.mdf;Initial Catalog=MySqlDB;Integrated Security=True");
                returnValue.Open();
                return returnValue;
            }
        }
    }
}

if all you want to do is run an SQL command, use an SQLCommand object. ( MSDN docs here )

here is the sample code from the article...

private static void ReadOrderData(string connectionString)
{
    string queryString = 
        "SELECT OrderID, CustomerID FROM dbo.Orders;";
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(
            queryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        try
        {
            while (reader.Read())
            {
                Console.WriteLine(String.Format("{0}, {1}",
                    reader[0], reader[1]));
            }
        }
        finally
        {
            // Always call Close when done reading.
            reader.Close();
        }
    }
}

things to note:

  1. uses a SqlConnection object with a Using block
  2. uses an SqlCommand object
  3. uses an SqlDataReader object
  4. explicitly closes the SqlConnection with finished with it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM