本文介绍了名称"MessageBox"在当前上下文中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Class1
   {


       public static void OpenConnection()
       {
           SqlConnection cn;
           cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename='D:\My_Project\My_Project\DATABASE\Soneri_Bank_loan_system.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True");
               try
               {
                   cn.Open();
               }
               catch (Exception e1)
               {

                   MessageBox.Show(e1.Message);
               }
           }

推荐答案

System.Windows.Forms

命名空间才能使用消息框

namespace to use message box


public class Class1
   {


       public static void OpenConnection()
       {
           SqlConnection cn;
           cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename='D:\My_Project\My_Project\DATABASE\Soneri_Bank_loan_system.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True");
               try
               {
                   cn.Open();
               }
               catch (Exception e1)
               {

                   MessageBox.Show(e1.Message);
               }
           }




这是您的表单类




this is your Form Class

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

    private void Form1_Load(object sender, System.EventArgs e)
    {
        Class1.OpenConnection();//this is your Static function that u have created in Class 1//
    }


    }
}


public class Class1
   {

public static SqlConnection cn;
	cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename='D:\My_Project\My_Project\DATABASE\Soneri_Bank_loan_system.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True");

       public static void OpenConnection()
       {

               try
               {
                   cn.Open();
               }
               catch (Exception e1)
               {

                   MessageBox.Show(e1.Message);
               }
           }
}



并在form.cs和class1.cs文件中导入System.Data.SqlClient,然后尝试



and import System.Data.SqlClient in form.cs and class1.cs file and try


这篇关于名称"MessageBox"在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:00