这是dll中的一个现有类,用于验证数据库是否存在
public class CheckDataBaseExists
{
public void CheckDataBase(string Server, string Database_name)
{
SqlConnection con = new SqlConnection(@"Data Source=" + Server + ";Initial Catalog=master;Integrated Security=True");
con.Open();
con.InfoMessage += connection_InfoMessage;
SqlCommand comm = new SqlCommand(@"if DB_ID('" + Database_name + "') is null print '" + Database_name + " is not exist !\r\nCreate new database ?'", con);
SqlDataAdapter adp = new SqlDataAdapter(comm);
DataTable set = new DataTable();
adp.Fill(set);
con.Close();
}
public static void connection_InfoMessage( object sender, SqlInfoMessageEventArgs e)
{
if( MessageBox.Show(e.Message, e.Source, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) == DialogResult.Yes)
{
try
{
如果数据库不存在,则输入服务器和数据库名称的形式
#region create form
Label creating = new Label();
creating = new System.Windows.Forms.Label();
creating.SuspendLayout();
//
// creating
//
creating.AutoSize = true;
creating.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
creating.Font = new System.Drawing.Font("Tahoma", 12F);
creating.Location = new System.Drawing.Point(25, 3);
creating.Name = "creating";
creating.Size = new System.Drawing.Size(150, 19);
creating.TabIndex = 0;
creating.Text = "Creating database...";
Form create_db = new Form();
create_db.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
create_db.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
create_db.BackColor = System.Drawing.Color.White;
create_db.ClientSize = new System.Drawing.Size(200, 25);
create_db.ControlBox = false;
create_db.TopMost = true;
create_db.Controls.Add(creating);
create_db.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
create_db.MaximizeBox = false;
create_db.MinimizeBox = false;
create_db.Name = "Create";
create_db.ShowIcon = false;
create_db.ShowInTaskbar = false;
create_db.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
create_db.Text = "Creating Databse...";
create_db.ResumeLayout(false);
create_db.PerformLayout();
create_db.Controls.Add(creating);
create_db.ShowDialog();
#endregion
读取包含数据库名称和服务器的文件
string[] read_file = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + @"\" + "ConReq.HP");
string db_src = read_file[2],
db_nm = read_file[0].Substring(7, read_file[0].Length - 7),
db_server = read_file[1].Substring(7, read_file[1].Length - 7),
location = read_file[3];
SQL文件包含使用要创建的数据库名称创建数据库的命令
string read_DBsql = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + @"\Required\DB\DB.sql");
read_DBsql = read_DBsql.Replace("DataBaseName", db_nm);
创建连接并执行命令
SqlConnection con = new SqlConnection("Data Source=" + db_server + ";Initial Catalog=master;Integrated Security=True;MultipleActiveResultSets=True");
con.Open();
SqlCommand comm = con.CreateCommand();
comm.CommandType = CommandType.Text;
comm.CommandText = read_DBsql;
comm.ExecuteNonQuery();
con.Close();
}
catch (Exception db_e)
{
MessageBox.Show(db_e.Message, db_e.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
但是发生的是,当出现一条消息时,请确保没有数据库,问题是您是否要创建一个数据库。 当我想创建一个新表单时,出现创建的表单,则什么也没有发生,并且仍然可见。如图所示
答案 0 :(得分:1)
为什么要使用Info_Message事件进行最简单的操作(检查数据库)
只需检查结果是否在查询以下:
字符串查询=“从sys.databases中选择count(*),其中name ='dbname'”
执行上述查询:
var count = cmd.ExecuteScalar();
还可以请您显示用Info_Message编写的完整代码