[英]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".
无法将文件“ j:... \\ KAELC_DB.mdf”复制到“ bin \\ Debug \\ KAELC_DB.mdf”。 The process cannot access the file 'j:...\\KAELC_DB.mdf' because it is being used by another process.
该进程无法访问文件'j:... \\ KAELC_DB.mdf',因为它正在被另一个进程使用。
Unable to copy file "j:...\\KAELC_DB_log.ldf" to "bin\\Debug\\KAELC_DB_log.ldf".
无法将文件“ j:... \\ KAELC_DB_log.ldf”复制到“ 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.
该进程无法访问文件'j:... \\ KAELC_DB_log.ldf',因为它正在被另一个进程使用。
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 ? 我在StackExchange上找到了一个对旧问题的回复,该链接由“ Justin”链接到此处https://stackoverflow.com/a/3998383 ,看起来可以解决该问题(而且我在其他地方也读到了“使用”是C#中最有效的编程方式之一),但是如何在代码中使用它呢?
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 ?!? 我创建了一个小项目,除了允许我按下按钮以处理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;
}
}
}
}
if all you want to do is run an SQL command, use an SQLCommand object. 如果您要做的只是运行SQL命令,请使用SQLCommand对象。 ( MSDN docs here )
( 此处为MSDN文档 )
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: 注意事项:
SqlConnection
object with a Using
block SqlConnection
对象和Using
块 SqlCommand
object SqlCommand
对象 SqlDataReader
object SqlDataReader
对象 SqlConnection
with finished with it SqlConnection
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.