gpt4 book ai didi

c# - 使用 Visual Studio 在运行时创建本地数据库

转载 作者:太空狗 更新时间:2023-10-29 23:37:34 25 4
gpt4 key购买 nike

初学者的问题 - 我可以就在运行时以编程方式创建本地数据库文件寻求建议吗?我希望稍后能够使用 Windows 资源管理器以与文本和其他文件相同的方式重命名、删除它们等,并将它们复制到其他计算机。

这是使用带有 C# 的 Visual Studio Community 15,安装了 SQL Server Data Tools 14.0.50616.0。计算机装有 Microsoft SQL Server 2014。

例如,我删除了程序的多余部分以保留下面的代码,它使用带有 3 个按钮的 Windows 窗体应用程序(btnCreateDbbtnDeleteDbbtnDoesDbExist) 和一个用于数据库名称的组合框 cbxDb。它在现有文件夹 C:\DbTemp 中创建数据库。

它显然会创建和删除一个新的数据库和生成文件,例如文件夹中的 mydb1.mdfmydb1.ldf,并声明它们存在。但是,如果我使用资源管理器删除这两个文件,则在尝试删除或创建数据库时会抛出异常; btnDoesDbExist 表明它仍然存在。

为什么文件被Windows Explorer 删除后,数据库仍然存在? btnDoesDatabaseExist 下的代码没有引用文件的路径,所以它一定看到了其他东西,但是在哪里?这是程序用户创建、删除和检测这些数据库的正确方法吗?

using System;
using System.Data;
using System.Windows.Forms;

//my additions
using System.Data.SqlClient;

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

public static string form1ConnectionString = "Data Source = (LocalDB)\\MSSQLLocalDB; Integrated Security = True; Connect Timeout = 30; ";
private string form1DatabasePath = "C:\\DbTemp";

private void btnCreateDb_Click(object sender, EventArgs e)
{
string nameToCreate = cbxDb.Text;
SqlConnection myConn = new SqlConnection(form1ConnectionString);

string str = "CREATE DATABASE " +nameToCreate+ " ON PRIMARY " +
"(NAME = " +nameToCreate+ "_Data, " +
"FILENAME = '" +form1DatabasePath+ "\\" +nameToCreate+ ".mdf', " +
"SIZE = 4MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
"LOG ON (NAME = " +nameToCreate+ "_Log, " +
"FILENAME = '" +form1DatabasePath+ "\\" +nameToCreate+ ".ldf', " +
"SIZE = 1MB, " +
"MAXSIZE = 5MB, " +
"FILEGROWTH = 10%)";

SqlCommand myCommand = new SqlCommand(str, myConn);

try
{
myConn.Open();
myCommand.ExecuteNonQuery();
MessageBox.Show("DataBase '" + nameToCreate + "' was created successfully");
}
catch (System.Exception ex)
{
MessageBox.Show("Exception in CreateDatabase " + ex.ToString(), "Exception in CreateDatabase", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
if (myConn.State == ConnectionState.Open)
{
myConn.Close();
}
}
}


private void btnDeleteDb_Click(object sender, EventArgs e)
{
string nameToDelete = cbxDb.Text;
string myConnectionString = form1ConnectionString + "AttachDBFileName = " + form1DatabasePath + "\\" + nameToDelete + ".mdf ";
string str = "USE MASTER DROP DATABASE " + nameToDelete;

SqlConnection myConn = new SqlConnection(myConnectionString);
SqlCommand myCommand = new SqlCommand(str, myConn);
myConn.Open();

try
{
myCommand.ExecuteNonQuery();
MessageBox.Show("DataBase '" + nameToDelete + "' was deleted successfully");

}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString(), "Exception in DeleteDatabase '" +nameToDelete+ "'", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
if (myConn.State == ConnectionState.Open)
{
myConn.Close();
}
}
}

private void btnDoesDbExist_Click(object sender, EventArgs e)
{
string nameToTest = cbxDb.Text;
using (var connection = new SqlConnection(form1ConnectionString))
{
using (var command = new SqlCommand(string.Format(
"SELECT db_id('" +nameToTest+ "')", nameToTest), connection))
{
connection.Open();

if ((command.ExecuteScalar() != DBNull.Value))
{
MessageBox.Show("DataBase '" +nameToTest+ "' exists");
}
else
{
MessageBox.Show("Database '" +nameToTest+ "' does not exist");
}
}
}

}
}

感谢大家的回复,不胜感激。

我现在明白我使用了错误的数据库,所以我尝试使用 SQL Server Compact 代替。已卸载、重新下载并重新安装 SQL Server Compact,包括 SP1。还从 https://visualstudiogallery.msdn.microsoft.com/0e313dfd-be80-4afb-b5e9-6e74d369f7a1 下载并安装了 SQL Server Compact/SQLite Toolbox。但是当我键入 using System.Data.SqlServerCe 时,Visual Studio 始终显示错误。此外,当我键入 SqlCeEngineSqlCecommand 时,我假设出于同样的原因。

在 Visual Studio 中,SQL Server Data ToolsSQL Server Compact & SQLite Toolbox 显示为已安装的产品,但不是 SQL Server Compact .我是否需要将它安装到 Visual Studio 中,如果需要,如何安装?

最佳答案

References 下的 Solution Explorer 中,检查是否列出了 System.Data.SqlServerCe。如果没有,请右键单击 References,然后单击 Add Reference -> Browse 按钮并选择文件 System.Data.SqlServerCe.dll,可能在 C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Desktop 中。 System.Data.SqlServerCe 现在应该出现在 References 下。

下面的程序似乎可以工作,而且简单得多。感谢大家的帮助。

using System;
using System.Data;
using System.Windows.Forms;
//my additions
using System.Data.SqlServerCe;
using System.IO;

namespace DataProg15
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string form1DatabasePath = "C:\\DbTemp\\dbtemp1.sdf";
public static string form1ConnectionString = "Data Source = " +form1DatabasePath;

private void btnCreateDb_Click(object sender, EventArgs e)
{
SqlCeEngine engine = new SqlCeEngine(form1ConnectionString);
try
{
engine.CreateDatabase();
MessageBox.Show("DataBase '" +form1DatabasePath+ "' was created successfully");
}
catch (System.Exception ex)
{
MessageBox.Show("Exception in CreateDatabase " + ex.ToString(), "Exception in CreateDatabase", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
engine.Dispose();
}
}

private void btnDeleteDb_Click(object sender, EventArgs e)
{
if (File.Exists(form1DatabasePath))
{
try
{
File.Delete(form1DatabasePath);
MessageBox.Show("DataBase '" +form1DatabasePath+ "' was deleted successfully");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString(), "Exception in DeleteDatabase '" +form1DatabasePath+ "'", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}

private void btnDoesDbExist_Click(object sender, EventArgs e)
{
if (File.Exists(form1DatabasePath))
{
MessageBox.Show("DataBase '" +form1DatabasePath+ "' exists");
}
else
{
MessageBox.Show("DataBase '" +form1DatabasePath+ "' does not exist");
}
}
}
}

关于c# - 使用 Visual Studio 在运行时创建本地数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35292586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com