如何在C#程序中包含此代码
我的数据库没有打开时遇到问题,因为它显然已经打开了 无法将文件“j:…\KAELC_DB.mdf”复制到“bin\Debug\KAELC_DB.mdf”。 进程无法访问文件“j:…\KAELC_DB.mdf”,因为它是 正在被另一个进程使用 无法将文件“j:…\KAELC\u DB\u log.ldf”复制到 “bin\Debug\KAELC\u DB\u log.ldf”。进程无法访问该文件 “j:…\KAELC_DB_log.ldf”,因为另一个进程正在使用它 我在StackExchange上找到了一个老问题的答案,链接到这里,是“Justin”,它似乎解决了这个问题(我在其他地方也读到,“使用”是C#中最有效的编程方式之一),但我如何在代码中使用它呢 我创建了一个小项目,它只允许我按下一个按钮来处理SQL语句,但我不明白“Justin”是什么意思,即“使用连接”。。。如何将SQL语句放入此代码中如何在C#程序中包含此代码,c#,sql-server,visual-studio-2010,C#,Sql Server,Visual Studio 2010,我的数据库没有打开时遇到问题,因为它显然已经打开了 无法将文件“j:…\KAELC_DB.mdf”复制到“bin\Debug\KAELC_DB.mdf”。 进程无法访问文件“j:…\KAELC_DB.mdf”,因为它是 正在被另一个进程使用 无法将文件“j:…\KAELC\u DB\u log.ldf”复制到 “bin\Debug\KAELC\u DB\u log.ldf”。进程无法访问该文件 “j:…\KAELC_DB_log.ldf”,因为另一个进程正在使用它 我在StackExchange
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;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Data.SqlClient;
名称空间MySqlTest
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
//打开SQL文件
使用(SqlConnection conn=SqlHelper.GetConn())
{
//使用连接如果您只想运行SQL命令,请使用SQLCommand对象。()
下面是文章中的示例代码
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();
}
}
}
注意事项:
将SqlConnection
对象与Using
块一起使用
使用SqlCommand
对象
使用SqlDataReader
对象
显式关闭SqlConnection
,并完成它
您是否尝试过查找有关SqlConnection
的任何文档?Justin的回答与您的问题无关。我猜想您在Visual Studio内部的生成过程中遇到了此错误?它并不是说打开文件有问题,而是说将其复制到您的bin\Debug
文件夹。这可能是因为,您在将u r mdf文件附加到sql server数据库,只需从sql server中删除该文件,然后将其置于调试状态folder@Douglas-我一直在使用不同的网站、书籍和视频来尝试学习C#,但似乎有几十种不同的方法来做每件事,每个人似乎都认为他们的方法是最好的!!!有人建议我使用一个站点/一本书并坚持使用它,所以也许我会这样做,但当我这样做时,我感到非常沮丧&发现其他地方有更简单/更好的方法!!!@Groo-是的,这是VS2010内部的调试运行,但问题是(据我所知)它无法复制它,因为它已经打开。如果我重新启动我的电脑,它工作正常,因此这显然与文件被保存/锁定以及贾斯汀的代码“暗示”有关这是可以避免的…但我需要帮助找出方法!!!为什么不将SqlDataReader
也放在using
块中?这样就不需要关闭它。你可以将数据读取器放在using块中。总是关闭连接(etc)是一个很好的做法:两条生活准则:人是愚蠢的,不信任任何人。我实际上不理解你的评论。正如@DBM提到的,这些类实现了IDisposable
(SqlConnection
,SqlCommand
和SqlDataReader),所有这些都应该使用`块放在
中。编写
try/finally块只是需要做更多的工作,可读性更低,更容易出错(例如,您忘记了处理SqlCommand
)。这些与“人都很愚蠢”或“不相信任何人”有什么关系?