C# 为什么数据库验证代码不能在c中执行

C# 为什么数据库验证代码不能在c中执行,c#,sql-server,C#,Sql Server,这是dll中的现有类,用于验证数据库是否存在 public class CheckDataBaseExists { public void CheckDataBase(string Server, string Database_name) { SqlConnection con = new SqlConnection(@"Data Source=" + Server + ";Initial Catalog=master;Integrated Security=T

这是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);
           }
           }


       }                 
    }
但发生的情况是,当消息出现时,确保没有数据库,问题是是否要创建一个数据库。 当我想要创建一个新的表单时,会出现创建的表单,然后什么都不会发生,并且仍然可见。如图所示


为什么要使用Info_消息事件来检查数据库

只需检查以下查询结果:

字符串查询=从sys.databases中选择count*,其中name='dbname'

按以下方式执行上述查询:

var count=cmd.ExecuteScalar


您还可以显示信息信息中的完整代码吗?这是很多代码。你能把你的问题缩小到更具体的范围吗?你认为哪一条线出了问题,你期望会发生什么?问题出在哪里还不清楚。消息框出现并且没有消失?数据库根本不是在服务器上创建的吗?您使用的是什么SQL程序Oracle、SQL Server等?我们可能还需要查看sql脚本。这很让人困惑,为什么要使用适配器/表进行不返回行的查询为什么要使用PRINT返回输出&可能是在_InfoMessage中捕获输出,而不是只运行SELECT查询?@JacobH SQL Server,SQL脚本包含大量由Microsoft SQL Server管理创建的代码Studio@AlexK. 但是,当被调用的dll包含cod时,直接放在项目中的代码工作得很好。如果MessageBox.Showe.Message、e.Source、MessageBoxButtons.YesNoCancel、MessageBoxIcon.Warning==DialogResult.Yesstring read\u DBsql=File.ReadAllTextAppDomain.CurrentDomain.BaseDirectory+@\Required\DB\DB.sql;read_DBsql=read_DBsql.ReplaceDataBaseName,ExcelTest;SqlConnection con=新的SqlConnectionData源;不公开;SqlCommand comm=con.CreateCommand;comm.CommandType=CommandType.Text;comm.CommandText=read_DBsql;comm.ExecuteNonQuery;con.关闭;如何关闭create_db表单。您的messagebox和create_db都显示为对话框,这可能是一个问题