一个关于附加SQL Server数据库文件的问题

yoshubom 2006-09-26 09:45:59
我使用如下代码将一个SQL数据库文件附加到SQL SERVER里

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.IO;
using System.Reflection;

namespace Example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//设置默认数据库文件路径
private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Text = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName) + "\\northwnd.mdf";
this.textBox2.Text = "Select * From Customers";
}
private SqlConnection MyConnection = new SqlConnection();
//附加数据库
private void button1_Click(object sender, EventArgs e)
{
string MyPath = this.textBox1.Text;
if (!File.Exists(MyPath))
{
MessageBox.Show("数据库文件不存在!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (this.MyConnection== null)
{
this.MyConnection= new SqlConnection();
}
else
this.MyConnection.Close();
try
{
SqlConnectionStringBuilder MyNewString = new SqlConnectionStringBuilder();
MyNewString.IntegratedSecurity = true;
MyNewString.DataSource = this.MyConnection.DataSource;
MyNewString.AttachDBFilename = null;//MyPath;
this.MyConnection.ConnectionString = MyNewString.ConnectionString;
MessageBox.Show("成功附加数据库文件!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception MyEx)
{
MessageBox.Show(MyEx.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//执行查询
private void button2_Click(object sender, EventArgs e)
{
try
{
this.MyConnection.Open();
string MySQL=this.textBox2.Text;
SqlCommand MyCommand = new SqlCommand(MySQL, this.MyConnection);
SqlDataReader MyReader = MyCommand.ExecuteReader();
DataTable MyTable = new DataTable();
MyTable.Load(MyReader);
this.dataGridView1.DataSource = MyTable;
this.MyConnection.Close();
}
catch (Exception MyEx)
{
if (this.MyConnection.State == ConnectionState.Open)
this.MyConnection.Close();
MessageBox.Show(MyEx.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}


现在问题是我的sql server 2000 多了一个没有名字的数据库,删都删不掉。咋办捏?
...全文
93 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
hzh_wen_x 2006-09-27
  • 打赏
  • 举报
回复
up

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧