繁体   English   中英

如何在C#程序中包含此代码

[英]How to include this code in a C# program

我的数据库没有打开,因为它显然已经打开,因此遇到了问题。

无法将文件“ j:... \\ KAELC_DB.mdf”复制到“ bin \\ Debug \\ KAELC_DB.mdf”。 该进程无法访问文件'j:... \\ KAELC_DB.mdf',因为它正在被另一个进程使用。

无法将文件“ j:... \\ KAELC_DB_log.ldf”复制到“ bin \\ Debug \\ KAELC_DB_log.ldf”。 该进程无法访问文件'j:... \\ KAELC_DB_log.ldf',因为它正在被另一个进程使用。

我在StackExchange上找到了一个对旧问题的回复,该链接由“ Justin”链接到此处https://stackoverflow.com/a/3998383 ,看起来可以解决该问题(而且我在其他地方也读到了“使用”是C#中最有效的编程方式之一),但是如何在代码中使用它呢?

我创建了一个小项目,除了允许我按下按钮以处理SQL语句外,什么也不做,但是我对“ Justin”对“使用连接”的含义感到困惑...我该如何放置SQL声明成这段代码?!?

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

如果您要做的只是运行SQL命令,请使用SQLCommand对象。 此处为MSDN文档

这是文章中的示例代码...

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

注意事项:

  1. 使用SqlConnection对象和Using
  2. 使用SqlCommand对象
  3. 使用SqlDataReader对象
  4. 用完成后显式关闭SqlConnection

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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